图像
v-img
组件包含支持丰富媒体的功能。与 vuetify-loader 结合使用,您可以添加动态渐进图像以提供更好的用户体验。
v-img
组件使用 v-intersect 指令,该指令要求 IE11和 Safari 使用 Polyfill 。 如果检测到不支持此功能的浏览器,则图像仍会照常加载。
用例
v-img
组件用于显示具有延迟加载和占位符的响应图像。
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
示例
下面是一些简单到复杂的例子。
包含和覆盖
如果提供的长宽比与实际图像不匹配,则默认行为是填充尽可能多的空间,裁剪图像的边。启用 contain
属性可以防止这种情况,但会导致两边出现空白。
template
<template>
<v-container fluid>
<v-row justify="space-around">
<v-col cols="5">
<div class="title mb-1">Default (cover)</div>
<div class="subheading">Matching</div>
<v-img src="https://picsum.photos/510/300?random" aspect-ratio="1.7"></v-img>
<div class="subheading pt-4">Too high</div>
<v-img src="https://picsum.photos/510/300?random" aspect-ratio="2"></v-img>
<div class="subheading pt-4">Too low</div>
<v-img src="https://picsum.photos/510/300?random" aspect-ratio="1.4"></v-img>
</v-col>
<v-col cols="5">
<div class="title mb-1">Contain</div>
<div class="subheading">Matching</div>
<v-img src="https://picsum.photos/510/300?random" aspect-ratio="1.7" contain></v-img>
<div class="subheading pt-4">Too high</div>
<v-img src="https://picsum.photos/510/300?random" aspect-ratio="2" contain></v-img>
<div class="subheading pt-4">Too low</div>
<v-img src="https://picsum.photos/510/300?random" aspect-ratio="1.4" contain></v-img>
</v-col>
</v-row>
</v-container>
</template>
高度
v-img
将自动增长到其 src
的大小,保持正确的长宽比。您可以限制这与 height
和 max-height
属性。
template script
<template>
<v-container
class="fill-height"
fluid
style="min-height: 434px"
>
<v-fade-transition mode="out-in">
<v-row v-if="show" key="0">
<v-col cols="6">
<v-card>
<v-img
src="https://picsum.photos/350/165?random"
height="125"
class="grey darken-4"
></v-img>
<v-card-title class="title">height</v-card-title>
</v-card>
</v-col>
<v-col cols="6">
<v-card>
<v-img
src="https://picsum.photos/350/165?random"
height="125"
contain
class="grey darken-4"
></v-img>
<v-card-title class="title">height with contain</v-card-title>
</v-card>
</v-col>
<v-col cols="6">
<v-card>
<v-img
src="https://picsum.photos/350/165?random"
max-height="125"
class="grey darken-4"
></v-img>
<v-card-title class="title">max-height</v-card-title>
</v-card>
</v-col>
<v-col cols="6">
<v-card>
<v-img
src="https://picsum.photos/350/165?random"
max-height="125"
contain
class="grey darken-4"
></v-img>
<v-card-title class="title">max-height with contain</v-card-title>
</v-card>
</v-col>
</v-row>
<v-row v-else key="1" justify="center">
<v-btn text @click="show = true">Load images</v-btn>
</v-row>
</v-fade-transition>
</v-container>
</template>
<script>
export default {
data: () => ({
show: false,
}),
}
</script>
固定比例
如果你想改变图像的长宽比,你可以设置一个固定的长宽比。
template script style
<template>
<v-container fluid>
<v-slider v-model="width" min="200" max="500" step="1"></v-slider>
<v-navigation-drawer
:width="width"
:value="true"
stateless
>
<v-img :aspect-ratio="16/9" src="https://cdn.vuetifyjs.com/images/parallax/material.jpg">
<v-row align="end" class="lightbox white--text pa-2 fill-height">
<v-col>
<div class="subheading">Jonathan Lee</div>
<div class="body-1">heyfromjonathan@gmail.com</div>
</v-col>
</v-row>
</v-img>
<v-list>
<template v-for="(item, i) in items">
<v-divider v-if="item.divider" :key="i"></v-divider>
<v-list-item v-else :key="item.title" @click>
<v-list-item-action>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-action>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</template>
</v-list>
</v-navigation-drawer>
</v-container>
</template>
<script>
export default {
data: () => ({
width: 300,
items: [
{ icon: 'inbox', title: 'Inbox' },
{ icon: 'star', title: 'Starred' },
{ icon: 'send', title: 'Sent mail' },
{ icon: 'drafts', title: 'Drafts' },
{ divider: true },
{ icon: 'mail', title: 'All mail' },
{ icon: 'delete', title: 'Trash' },
{ icon: 'error', title: 'Spam' },
],
}),
}
</script>
<style scoped>
.v-navigation-drawer {
transition: none !important;
}
.lightbox {
box-shadow: 0 0 20px inset rgba(0, 0, 0, 0.2);
background-image: linear-gradient(to top, rgba(0, 0, 0, 0.4) 0%, transparent 72px);
}
</style>
Business Support
Support your business and Vuetify at the same time with support directly from the author.
ads by Vuetify
]($ddeb3f83df3c619a.md)
占位符
v-img
有一个特殊的 placeholder
插槽,占位符显示图像的加载。注意:下面的例子有一个错误的 src,它不会为你加载占位符。
template
<template>
<v-row align="center" justify="center">
<v-img
src="https://bad.src/not/valid"
lazy-src="https://picsum.photos/id/11/100/60"
aspect-ratio="1"
class="grey lighten-2"
max-width="500"
max-height="300"
>
<template v-slot:placeholder>
<v-row
class="fill-height ma-0"
align="center"
justify="center"
>
<v-progress-circular indeterminate color="grey lighten-5"></v-progress-circular>
</v-row>
</template>
</v-img>
</v-row>
</template>
渐变
gradient
属性可用于将简单的渐变叠加层应用于图像。 应该将更复杂的渐变作为类写在内容插槽上。
template style
<template>
<v-container fluid>
<v-row>
<v-col cols="6" sm="4">
<v-img
src="https://cdn.vuetifyjs.com/images/parallax/material2.jpg"
gradient="to top right, rgba(100,115,201,.33), rgba(25,32,72,.7)"
></v-img>
</v-col>
<v-col cols="6" sm="4">
<v-img src="https://cdn.vuetifyjs.com/images/parallax/material2.jpg">
<div class="fill-height bottom-gradient"></div>
</v-img>
</v-col>
<v-col cols="6" sm="4">
<v-img src="https://cdn.vuetifyjs.com/images/parallax/material2.jpg">
<div class="fill-height repeating-gradient"></div>
</v-img>
</v-col>
</v-row>
</v-container>
</template>
<style scoped>
.bottom-gradient {
background-image: linear-gradient(to top, rgba(0, 0, 0, 0.4) 0%, transparent 72px);
}
.repeating-gradient {
background-image: repeating-linear-gradient(-45deg,
rgba(255,0,0,.25),
rgba(255,0,0,.25) 5px,
rgba(0,0,255,.25) 5px,
rgba(0,0,255,.25) 10px
);
}
</style>
栅格
您可以使用 v-img
来制作图片库。
template
<template>
<v-row>
<v-col cols="12" sm="6" offset-sm="3">
<v-card>
<v-container fluid>
<v-row>
<v-col
v-for="n in 9"
:key="n"
class="d-flex child-flex"
cols="4"
>
<v-card flat tile class="d-flex">
<v-img
:src="`https://picsum.photos/500/300?image=${n * 5 + 10}`"
:lazy-src="`https://picsum.photos/10/6?image=${n * 5 + 10}`"
aspect-ratio="1"
class="grey lighten-2"
>
<template v-slot:placeholder>
<v-row
class="fill-height ma-0"
align="center"
justify="center"
>
<v-progress-circular indeterminate color="grey lighten-5"></v-progress-circular>
</v-row>
</template>
</v-img>
</v-card>
</v-col>
</v-row>
</v-container>
</v-card>
</v-col>
</v-row>
</template>