微信小程序预览图片和视频

官方APIwx.previewMedia

  1. 重装数据结构

  2. wx.previewMedia 中的 sources 是个数组,然后里面是:
    [ {
    url:item.videoPath,
    type:‘video’,
    poster:item.picUrl
    }
    {
    url:item.picUrl,
    type:‘image’,

    }]

Page({

  /**
   * 页面的初始数据
   */
  data: {
    "bannerList": [
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/4e4f26f7-5dd5-482f-91c8-5da73ca3593b",
          "bannerId": 444,
          "createDate": 1618824081000,
          "bannerType": 1,
          "videoPath": "http://test-qiniuyun.sdjmm.net/2bc38330-7259-4fbd-bdda-aa1077177d04"
      },
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/925fecd0-7e5a-44c8-9b93-1967b9837c45",
          "bannerId": 445,
          "createDate": 1618824141000,
          "bannerType": 1,
          "videoPath": "http://test-qiniuyun.sdjmm.net/60b96b94-6e63-4cf6-bed4-91480d969982"
      },
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/5c70ec55-e4d6-41a6-9fc0-c8443baf79b3",
          "bannerId": 446,
          "createDate": 1618824155000,
          "bannerType": 0,
          "videoPath": null
      },
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/be557182-2a1f-48b7-83f1-479ba41e4e47",
          "bannerId": 447,
          "createDate": 1618824164000,
          "bannerType": 0,
          "videoPath": null
      },
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/80722199-b74c-49ce-ad21-2164c4f489f5",
          "bannerId": 448,
          "createDate": 1618824183000,
          "bannerType": 0,
          "videoPath": null
      },
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/ccacfade-104d-434d-9e4b-27c20ff9e204",
          "bannerId": 449,
          "createDate": 1618824197000,
          "bannerType": 0,
          "videoPath": null
      },
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/f45fb35b-06db-4f48-ae1a-2bee6e8059be",
          "bannerId": 450,
          "createDate": 1618824212000,
          "bannerType": 0,
          "videoPath": null
      },
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/7b06f276-7422-469d-bb2d-9e10901e2bb3",
          "bannerId": 451,
          "createDate": 1618824222000,
          "bannerType": 0,
          "videoPath": null
      },
      {
          "picUrl": "http://test-qiniuyun.sdjmm.net/3f6ab5b7-3724-4d8d-bffc-58bcfe4b49d7",
          "bannerId": 452,
          "createDate": 1618824231000,
          "bannerType": 0,
          "videoPath": null
      }
  ]
  },
  pic_preview(e) {
    // 版本处理
    let index = e.currentTarget.dataset.index
    let bannerList = this.data.bannerList
   let bannerArr = bannerList.map(item => {
        if(item.bannerType == 0) {
          return {
            url:item.picUrl,
            type:'image'
          }   
        }
        if(item.bannerType == 1) {
          return {
            url:item.videoPath,
            type:'video',
            poster:item.picUrl
          }        
        }
    })
    if (wx.canIUse('previewMedia')) {
      wx.previewMedia({
        sources: bannerArr,
        current: index, // 当前显示图片的http链接
      })
    }else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      })
    }

  },



})
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月落星河°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值