Aspect Ratio
为媒体预留指定宽高比例,并在比例变化时平滑调整。
适用场景
- 当界面需要以下行为时使用 Aspect Ratio:为媒体预留指定宽高比例,并在比例变化时平滑调整。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/aspect-ratio.json https://orvui.efu.me/r/tabs.json
首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
预览
packages/vue/src/motion/aspect-ratio/demo.vue
<script setup lang="ts">
import { computed, ref, useId } from "vue";
import { AspectRatio, AspectRatioImage } from "@/components/orvui/motion/aspect-ratio";
import { Tabs, TabsList, TabsTrigger } from "@/components/orvui/motion/tabs";
const labels = ["21:9", "16:9", "3:2", "4:3", "1:1", "4:5", "3:4", "2:3", "9:16"];
const selected = ref("16:9"), id = useId();
const ratio = computed(() => { const [width, height] = selected.value.split(":").map(Number); return width! / height!; });
const panelId = `${id}-preview`;
</script>
<template>
<div class="mx-auto w-full max-w-xl min-w-0 px-4 py-6"><Tabs v-model="selected">
<TabsList aria-label="Aspect ratio" class="mx-auto bg-muted"><TabsTrigger v-for="label in labels" :key="label" :value="label"
:id="`${id}-${label}`" :aria-controls="panelId" :tabindex="selected === label ? 0 : -1" indicator-class-name="bg-background"
class="min-h-9 px-3 text-xs [&_[data-tabs-label]]:text-foreground">{{ label }}</TabsTrigger></TabsList>
<div :id="panelId" role="tabpanel" :aria-labelledby="`${id}-${selected}`" :tabindex="0"
class="mt-6 flex h-[25rem] w-full items-center justify-center rounded-2xl outline-none focus-visible:ring-2 focus-visible:ring-ring">
<AspectRatio :ratio="ratio" class="bg-muted" :style="{ maxWidth: `${Math.min(440, 360 * ratio)}px`, borderRadius: '16px' }">
<AspectRatioImage src="https://images.unsplash.com/photo-1470770841072-f978cf4d019e?auto=format&fit=crop&w=1000&q=80"
alt="A lakeside cabin beneath mountains, reflected in the water" :width="1000" :height="667" />
</AspectRatio>
</div>
</Tabs></div>
</template>
packages/vue/src/motion/aspect-ratio/src/AspectRatio.vue
状态与事件
此示例组合没有声明成对的受控属性与 update 事件。使用已列出的属性、原生事件和插槽组合内容;不要为它添加未公开的 v-model。
API 参考
AspectRatio
ratio?number1animated?booleantruecontentClassName?string—插槽
default() => VNodeChild#defaultAspectRatioImage
widthnumber—heightnumber—altstring—srcstring—draggable?booleanfalse无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Aspect Ratio?
- 绑定数值比例,例如 :ratio="16 / 9",并在默认插槽放入媒体。
- 如何连接到应用状态?
- 通过default 插槽提供内容,应用副作用由调用方管理。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
更新于
来源与许可 ↗MIT · 0.1.1