vant-swipe自适应图片高度+图片预览

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>
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用vant-uploader和vue-cropper来实现裁剪图片并上传,你可以按照以下步骤进行操作: 1. 首先,安装vant和vue-cropper插件。你可以使用npm或yarn来安装它们。 2. 在你的Vue组件中,引入vant-uploader和vue-cropper组件。 3. 在模板中,使用vant-uploader组件来实现图片上传功能。设置上传的action属性为你的上传接口地址,并设置on-success事件来处理上传成功后的逻辑。 4. 在on-success事件中,获取到上传成功后的图片地址,并将其传递给vue-cropper组件。 5. 在vue-cropper组件中,设置裁剪框的样式和裁剪比例等属性。使用v-model指令来绑定裁剪后的图片数据。 6. 在提交按钮的点击事件中,将裁剪后的图片数据上传到服务器。 下面是一个简单的示例代码: ```vue <template> <div> <van-uploader action="/upload" :on-success="handleUploadSuccess" ></van-uploader> <vue-cropper v-if="showCropper" :src="cropperSrc" :output-size="{ width: 200, height: 200 }" :output-type="'jpeg'" :fixed-box="true" :fixed-number="\[1, 1\]" v-model="croppedImage" ></vue-cropper> <button @click="handleSubmit">提交</button> </div> </template> <script> import { VanUploader } from 'vant'; import VueCropper from 'vue-cropper'; export default { components: { VanUploader, VueCropper, }, data() { return { showCropper: false, cropperSrc: '', croppedImage: '', }; }, methods: { handleUploadSuccess(response) { // 获取上传成功后的图片地址 const imageUrl = response.data.imageUrl; // 显示裁剪组件 this.showCropper = true; // 设置裁剪组件的图片地址 this.cropperSrc = imageUrl; }, handleSubmit() { // 提交裁剪后的图片数据到服务器 // this.croppedImage 包含裁剪后的图片数据 }, }, }; </script> ``` 请注意,以上代码只是一个简单的示例,你需要根据你的实际需求进行适当的修改和调整。同时,你还需要在后端实现相应的上传和裁剪功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值