当前位置:网站首页>vant-swipe自适应图片高度+图片预览
vant-swipe自适应图片高度+图片预览
2022-08-02 22:42:00 【GG·bond】
vant-swipe自适应图片高度+图片预览
1.vant-swipe自适应图片高度
当轮播图的图片高度不相同时,如果不设置高度,van-swipe-item则会采取最高的图片的高度作为高度,则其他图片高度低于这个高度,则会底部空白,影响美观
解决办法:利用Swipe Props 的 height(滑块高度) 和change 事件 每次切换图片时,重新获取图片高度,赋值给滑块高度
//获取
async mounted(){
let res= await this.getImgSize(this.imgs[0])
this.height=res.height/(res.width/375)
},
methods:{
// 通过图片的url获取图片尺寸
getImgSize (url) {
return new Promise((resolve, reject) => {
let img = new Image()
img.src = url
img.onload = () => {
resolve({
width: img.width,
height: img.height
})
}
})
}
//swipe的切换监听事件
async onChange(index) {
let res= await this.getImgSize(this.imgs[index])
// 图片固定宽度100vw
this.height=res.height/(res.width/375)
},
}
2.图片预览
使用vant的 ImagePreview函数,需要用到swipe的autoplay,当autoplay=0时,停止播放;当图片进入预览时,关闭自动播放,结束预览时,跳到对应的图片(需要暂时关闭轮播动画,不然会一闪而过),开始自动播放
//预览
showImage(i){
// 预览关闭轮播图滚动
this.autoplay=0
ImagePreview({
images: this.imgs,
//开始的图片位置
startPosition: i,
overlayStyle:{
background: '#000'
},
//关闭预览图时-该图为轮播图起始图
onClose:res=> {
//轮播图的方法,跳到对应的图片
this.$refs.vantSwiper.swipeTo(res.index,{
immediate:true //关闭轮播切换效果
})
this.autoplay=2000
},
});
},
3.全部代码
<template>
<div class="swiper">
<van-swipe class="my-swipe" :autoplay="autoplay" indicator-color="white" :height="height" duration="1000" ref="vantSwiper" @change="onChange">
<van-swipe-item v-for="(item,i) in imgs" :key="i" >
<img :src="item" alt="" @click="showImage(i)">
</van-swipe-item>
</van-swipe>
</div>
</template>
<script>
import { ImagePreview } from 'vant';
export default {
data(){
return{
imgs:[
require('@/assets/swiper/1.jpg'),
require('@/assets/swiper/2.jpg'),
require('@/assets/swiper/3.jpg'),
require('@/assets/swiper/4.jpg'),
require('@/assets/swiper/5.jpg')
],
autoplay:2000,
height:null
}
},
async mounted(){
let res= await this.getImgSize(this.imgs[0])
this.height=res.height/(res.width/375)
},
methods:{
async onChange(index) {
let res= await this.getImgSize(this.imgs[index])
// 图片固定宽度100vw
this.height=res.height/(res.width/375)
},
//预览
showImage(i){
// 预览关闭轮播图滚动
this.autoplay=0
ImagePreview({
images: this.imgs,
//开始的图片位置
startPosition: i,
overlayStyle:{
background: '#000'
},
//关闭预览图时-该图为轮播图起始图
onClose:res=> {
//轮播图的方法,跳到对应的图片
this.$refs.vantSwiper.swipeTo(res.index,{
immediate:true //关闭轮播切换效果
})
this.autoplay=2000
},
});
},
// 获取图片尺寸
getImgSize (url) {
return new Promise((resolve, reject) => {
let img = new Image()
img.src = url
img.onload = () => {
resolve({
width: img.width,
height: img.height
})
}
})
}
}
}
</script>
<style lang="less" scoped>
.van-swipe-item{
img{
width: 100%;
}
}
.my-swipe{
position: relative;
font-size: 0;
}
.custom-indicator {
position: absolute;
right: 5px;
bottom: 5px;
padding: 2px 5px;
font-size: 12px;
background: rgba(0, 0, 0, 0.1);
}
</style>
边栏推荐
- 【Unity】Unity开发进阶(六)UnityEvent使用与源码解析
- TCP三次握手与四次挥手
- Shunted Self-Attention via Multi-Scale Token Aggregation
- MDL 内存描述符链表
- TDengine 在中天钢铁 GPS、 AIS 调度中的落地
- No-code development platform form styling steps introductory course
- JS Date 时间戳 getTune data.parse 倒计时小程序
- 21天学习挑战赛(1)设备树的由来
- airflow db init 报错
- If the watermark according to how to realize the function
猜你喜欢
随机推荐
最近公共祖先(LCA)学习笔记 | P3379 【模板】最近公共祖先(LCA)题解
创建型模式 - 单例模式Singleton
GameStop NFT 市场分析
软件测试到底自学还是报班?
刚安装完win10专业工作站版,系统变量中Path默认值有哪些?重新建一个“PATH”变量名,会覆盖掉原先的“Path”。
Mysql查看慢查询日志
辅助脚本开发之旅
CentOS7 安装MySQL 图文详细教程
mPEG-Cholesterol,mPEG-CLS,甲氧基-聚乙二醇-胆固醇可用于脂质体制备
谷粒商城-day13-es和商品上架
在软件测试行业近20年的我,再来和大家谈谈今日的软件测试
The CTF command execution subject their thinking
Ruoyi integrates minio to realize distributed file storage
Token、Redis实现单点登录
C语言函数详解(2)【函数参数——实际参数(实参)&形式参数(形参)】
Web APIs BOM- 操作浏览器-Window对象
脂溶性胆固醇-聚乙二醇-叠氮,Cholesterol-PEG-Azide,CLS-PEG-N3
Word operation: adjust the English font individually
Matplotlib drawing core principles explain (more detailed)
无代码开发平台表单样式设置步骤入门课程