美文网首页
photoswipe放大图片的使用

photoswipe放大图片的使用

作者: 池鱼_故渊 | 来源:发表于2019-10-14 15:51 被阅读0次

简介

官网地址: https://photoswipe.com/

一款可以可以放大图片的插件。操作简单

引入依赖包

npm install photoswipe

使用

const PhotoSwipe = require('photoswipe')
const PhotoSwipeUI_Default = require('photoswipe/dist/photoswipe-ui-default')
require('photoswipe/dist/photoswipe.css')
require('photoswipe/dist/default-skin/default-skin.css')

引入相关样式和api

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo https://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

引入相关dom节点,只需要再你需要的页面引入即可,不会影响页面的结构

在js操作中获取图片的dom节点,给图片添加点击事件。

var pswpElement = document.querySelectorAll('.pswp')[0]; //上面添加的dom节点

var items = [
    {
        src: 'https://placekitten.com/600/400',//获取图片的src
        w: 600, // 原始大小
        h: 400
    },
    {
        src: 'https://placekitten.com/1200/900',
        w: 1200, // 放大后的大小
        h: 900
    }
];

// define options (if needed)
var options = {
    // optionName: 'option value'
    // for example:
    index: 0 // start at first slide
};

// Initializes and opens PhotoSwipe
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

官网demo地址:https://photoswipe.com/documentation/getting-started.html

当我们获取到的是图片数组时,在vue中不能直接遍历dom,需要转换一下。

let img = pswpElement.getElementsByTagName('img')
let arr1 = [].slice.call(img) //转化获取的dom节点数组
arr1.forEach((item) => {
    item.addEventListener('click', e => {
    this.openPhotoSwipe(e) //调用上面的js函数 传入节点获取src
    })
})

多张图的时候使用方法

引入下面的函数,并传入dom节点

  mounted () {
    this.$nextTick(() => {
    //   document.getElementById('contentScroll').addEventListener('mousewheel', (event) => {
    //     event.stopPropagation()
    //   })
      this.initPhotoSwipeFromDOM('.my-gallery')
    })
  },
......
initPhotoSwipeFromDOM (gallerySelector) {
      // parse slide data (url, title, size ...) from DOM elements
    // (children of gallerySelector)
      var parseThumbnailElements = function (el) {
        var thumbElements = el.childNodes
        var numNodes = thumbElements.length
        var items = []
        var figureEl
        var linkEl
        var size
        var item

        for (var i = 0; i < numNodes; i++) {
          figureEl = thumbElements[i] // <figure> element

          // include only element nodes
          if (figureEl.nodeType !== 1) {
            continue
          }

          linkEl = figureEl.children[0] // <a> element

          size = linkEl.getAttribute('data-size').split('x')

          // create slide object
          item = {
            src: linkEl.getAttribute('href'),
            w: parseInt(size[0], 10),
            h: parseInt(size[1], 10)
          }

          if (figureEl.children.length > 1) {
            // <figcaption> content
            item.title = figureEl.children[1].innerHTML
          }

          if (linkEl.children.length > 0) {
            // <img> thumbnail element, retrieving thumbnail url
            item.msrc = linkEl.children[0].getAttribute('src')
          }

          item.el = figureEl // save link to element for getThumbBoundsFn
          items.push(item)
        }

        return items
      }

      // find nearest parent element
      var closest = function closest (el, fn) {
        return el && (fn(el) ? el : closest(el.parentNode, fn))
      }

      // triggers when user clicks on thumbnail
      var onThumbnailsClick = function (e) {
        e = e || window.event
        e.preventDefault ? e.preventDefault() : e.returnValue = false

        var eTarget = e.target || e.srcElement

        // find root element of slide
        var clickedListItem = closest(eTarget, function (el) {
          return (el.tagName && el.tagName.toUpperCase() === 'FIGURE')
        })

        if (!clickedListItem) {
          return
        }

        // find index of clicked item by looping through all child nodes
        // alternatively, you may define index via data- attribute
        var clickedGallery = clickedListItem.parentNode
        var childNodes = clickedListItem.parentNode.childNodes
        var numChildNodes = childNodes.length
        var nodeIndex = 0
        var index

        for (var i = 0; i < numChildNodes; i++) {
          if (childNodes[i].nodeType !== 1) {
            continue
          }

          if (childNodes[i] === clickedListItem) {
            index = nodeIndex
            break
          }
          nodeIndex++
        }

        if (index >= 0) {
          // open PhotoSwipe if valid index found
          openPhotoSwipe(index, clickedGallery)
        }
        return false
      }

      // parse picture index and gallery index from URL (#&pid=1&gid=2)
      var photoswipeParseHash = function () {
        var hash = window.location.hash.substring(1)
        var params = {}

        if (hash.length < 5) {
          return params
        }

        var vars = hash.split('&')
        for (var i = 0; i < vars.length; i++) {
          if (!vars[i]) {
            continue
          }
          var pair = vars[i].split('=')
          if (pair.length < 2) {
            continue
          }
          params[pair[0]] = pair[1]
        }

        if (params.gid) {
          params.gid = parseInt(params.gid, 10)
        }

        return params
      }

html j结构

<div class="more-img my-gallery" v-else>
      <figure itemprop="associatedMedia" itemscope v-for="(item,index) in imgListData" :key="index">
        <a :href="item.image_big" itemprop="contentUrl" data-size="100%x100%">
            <img :src="item.image_small" itemprop="thumbnail" alt="Image description" />
        </a>                             
      </figure>
     </div>

相关文章

  • photoswipe放大图片的使用

    简介 引入依赖包 使用 引入相关样式和api 引入相关dom节点,只需要再你需要的页面引入即可,不会影响页面的结构...

  • 图片点击放大photoswipe

    对比另外一个图片放大插件vue-photo-preview,photoswipe也是一款图片放大插件,使用之前需要...

  • vue-photo-preview

    基于photoswipe的vue图片预览插件 说明 简化了photoswipe的默认设置 取消了图片需设定尺寸的要...

  • 积累

    1,图片查看https://github.com/dimsemenov/PhotoSwipe2.window神器 ...

  • H5使用PhotoSwipe预览图片

    如果在h5上想要实现类似app上的点击预览图片并且可以放大缩小这种功能,确实比在app上实现起来更为麻烦,但是有了...

  • 解决:使用Photoswipe进行图片展示

    python 2.7Django 1.6.1photoswipe 前言 对于前端的照片存储,已经在前一篇博文中进行...

  • 杂货铺

    photoSwipe——对多图页面可实现点击放大,并且可以左右滑动轮播(参考淘宝商品详情页面)

  • vue点击图片放大预览图片支持旋转等

    vue图片点击放大预览v-viewer库使用 提到图片放大预览,可能好多人想到的是lightbox,在vue中使用...

  • Vue查看大图(vue-photo-preview)

    简介 基于photoswipe的vue图片预览插件 地址 http://npm.taobao.org/packag...

  • PhotoSwipe 官方API解读(一)

    一:getting started开始之前要知道:1、 PhotoSwipe需要提前预定义图片的大小(more:...

网友评论

      本文标题:photoswipe放大图片的使用

      本文链接:https://www.haomeiwen.com/subject/yfvrmctx.html