H5 查看大图。缩放图片

之前前前后后找了很多类似的JS库,都非常的优秀,比如 viewerjs 或者其他。

然而这些JS库都太厉害了。封装的功能也是太多了,最重要的是这个库放大的图片。
并不能满屏,只能以实际上传的图片大小来显示

最后还是用了swiper。最后实现的效果:

  • 可以通过双击放大图片
  • 在移动端可以通过手指缩放放大/缩小图片

附上简单的源代码,写的比较简陋,仅供参考,可以稍作修改

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>


  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.2/css/swiper.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.2/js/swiper.min.js"></script>

  <style>
    #origin-img {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: #000;
    }

    #origin-img .swiper-slide img {
      width: 100%;
      vertical-align: middle;
    }

    .swiper-pagination {
      top: 1em;
      bottom: auto;
      color: #fff;
    }

    .upload {
      position: absolute;
      bottom: 0;
      line-height: 3em;
      background: rgba(0, 0, 0, .5);
      z-index: 5;
      color: #fff;
      text-indent: 1em;
      width: 100%;
    }
  </style>
</head>

<body>

  <div class="thumb" thumblist="list1">
    <img src="https://www.swiper.com.cn/demo/meituan1/uploads/s1_1.jpg" tppabs="https://www.swiper.com.cn/demo/meituan1/uploads/s1_1.jpg">
    <img src="https://www.swiper.com.cn/demo/meituan1/uploads/s1_2.jpg" tppabs="https://www.swiper.com.cn/demo/meituan1/uploads/s1_2.jpg">
    <img src="https://www.swiper.com.cn/demo/meituan1/uploads/s1_3.jpg" tppabs="https://www.swiper.com.cn/demo/meituan1/uploads/s1_3.jpg">
    <img src="https://www.swiper.com.cn/demo/meituan1/uploads/s1_4.jpg" tppabs="https://www.swiper.com.cn/demo/meituan1/uploads/s1_4.jpg">
  </div>

  <div class="thumb" thumblist="list2">
    <img src="https://www.swiper.com.cn/demo/meituan1/uploads/s2_1.jpg" tppabs="https://www.swiper.com.cn/demo/meituan1/uploads/s2_1.jpg">
    <img src="https://www.swiper.com.cn/demo/meituan1/uploads/s2_2.jpg" tppabs="https://www.swiper.com.cn/demo/meituan1/uploads/s2_2.jpg">
  </div>

  <div class="thumb" thumblist="list3">
    <img src="https://www.swiper.com.cn/demo/meituan1/uploads/s3_1.jpg" tppabs="https://www.swiper.com.cn/demo/meituan1/uploads/s3_1.jpg">
  </div>

  <!-- Swiper -->
  <div class="swiper-container" id="origin-img">
    <div class="swiper-wrapper"></div>
    <div class="swiper-pagination"></div>
    <div class="upload">图片描述</div>
  </div>


  <script>
    imgsdata = {
      "list1": ["https://www.swiper.com.cn/demo/meituan1/uploads/s1_1.jpg" /*tpa=*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s1_2.jpg" /*tpa=*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s1_3.jpg" /*tpa=*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s1_4.jpg" /*tpa=*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s1_5.jpg" /*tpa=*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s1_6.jpg" /*tpa=*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s1_7.jpg" /*tpa=https://www.swiper.com.cn/demo/meituan1/uploads/s1_7.jpg*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s1_8.jpg" /*tpa=https://www.swiper.com.cn/demo/meituan1/uploads/s1_8.jpg*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s1_9.jpg" /*tpa=https://www.swiper.com.cn/demo/meituan1/uploads/s1_9.jpg*/
      ],
      "list2": ["https://www.swiper.com.cn/demo/meituan1/uploads/s2_1.jpg" /*tpa=https://www.swiper.com.cn/demo/meituan1/uploads/s2_1.jpg*/ ,
        "https://www.swiper.com.cn/demo/meituan1/uploads/s2_2.jpg" /*tpa=https://www.swiper.com.cn/demo/meituan1/uploads/s2_2.jpg*/
      ],
      "list3": ["https://www.swiper.com.cn/demo/meituan1/uploads/s3_1.jpg" /*tpa=https://www.swiper.com.cn/demo/meituan1/uploads/s3_1.jpg*/ ],
    }

    var swiper = new Swiper('.swiper-container', {
      zoom: true,
      width: window.innerWidth,
      virtual: true,
      spaceBetween: 20,
      pagination: {
        el: '.swiper-pagination',
        type: 'fraction',
      },
      on: {
        click: function () {
          $('#origin-img').fadeOut('fast');
          this.virtual.slides.length = 0;
          this.virtual.cache = [];
          swiperStatus = false;

        },
      },

    });

    $('.thumb img').click(function () {
      clickIndex = $(this).index();

      imglist = $(this).parent().attr('thumblist');
      imgs = imgsdata[imglist];
      for (i = 0; i < imgs.length; i++) {
        swiper.virtual.appendSlide('<div class="swiper-zoom-container"><img src="' + imgs[i] + '" /></div>');
      }
      swiper.slideTo(clickIndex);
      $('#origin-img').fadeIn('fast');
      swiperStatus = true;

    })

    //切换图状态禁止页面缩放	
    document.addEventListener('touchstart', function (event) {
      if (event.touches.length > 1 && swiperStatus) {
        event.preventDefault();
      }
    })
    var lastTouchEnd = 0;
    document.addEventListener('touchend', function (event) {
      var now = (new Date()).getTime();
      if (now - lastTouchEnd <= 300) {
        event.preventDefault();
      }
      lastTouchEnd = now;
    }, false)

    document.addEventListener('touchmove', function (e) {
      if (swiperStatus) {
        e.preventDefault();
      }
    })
  </script>
</body>

</html>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值