使用PhotoSwipe做一个兼容手机和pc端的图片滑动切换展示库

首先要引入photoSwiped 的js 的包,其中那个demo.css是自己写的,可以按需求修改

下面这段代码一定要加,至于为什么,好像是说是photoSwipe作者故意的,而且最好是加在body里面

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. PhotoSwipe keeps only 3 slides in DOM to save memory. -->
        <div class="pswp__container">
            <!-- don't modify these 3 pswp__item elements, data is added later on -->
            <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>

                <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>

你也可以根据需求稍微改动一下,就是把一些按钮删掉啊,或增加。

图片的话,你可以自己设置,我这里就先用我已经做好的做一下展示

搞了一个点击事件

js的话就去获取这些值,由于我的主要功能是要通过图片和类型查出相同类型的图片,所以我是这样写的

<script>
    function openphoto(st, file, obj) {
        var img = new Image();
        img.src = obj.src;
        var height = img.height;
        var width =img.width;

        var myphoto = document.getElementById('type_' + st);
        myphoto.onclick = function () {
            filepaths(st,file,height,width);
        }
    }
    function filepaths(type,file,height,width) {
        $.ajax({
            url: '${ctx}/out/filepath?type=' + type+'&code=${passInfo.VERIFY_CODE}',
            success: function (data) {
                var filepath = new Array();
                var L_P = new Array();
                for (var a in data) {
                    filepath.push(data[a].FILEPATH);
                    L_P.push(data[a].LPTYPEINFORMATION);
                }
                openPhotoSwipe(filepath,L_P,file,height,width);
            }
        });
    }
    var openPhotoSwipe = function (filepath,L_P,file,height,width) {
        var pswpElement = document.querySelectorAll('.pswp')[0];
        var items = [];
        var s=0;
        for (var i in filepath) {
            if(filepath[i]==file){
                s=parseInt(i);
            };
            items.push({
                src: '${ctx}/out/showImg?imgFile=' + filepath[i], w: width,
                h: height, title: L_P[i]
            });
        }
        // build items array

        // define options (if needed)
        var options = {
            index: s,
            loop: false,
            closeOnScroll: false,
            // history & focus options are disabled on CodePen
            history: true,
            focus: true,
            showAnimationDuration: 0,
            hideAnimationDuration: 0
        };

        var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
    };

</script>

其实主要的轮播展示是这段代码,前面的都是我自己根据需要去做处理的,下面这段是最原始的例子,可以根据自己需求改动

<script>
var openPhotoSwipe = function() {
    var pswpElement = document.querySelectorAll('.pswp')[0];
    // build items array
    var items = [
        {
            src: 'images/s1.jpg',
            w: 950,
            h: 534,
            title: '特斯拉Semi卡车亮相:2019年生产'
        },
        {
            src: 'images/s2.jpg',
            w: 800,
            h: 500,
            title: '12月3日,第四届世界互联网大会在浙江乌镇开幕。本届大会主题为“发展数字经济 促进开放共享——携手共建网络空间命运共同体”。'
        },
        {
            src: 'images/s3.jpg',
            w: 516,
            h: 712,
            title: '阿里巴巴菜鸟网络的无人机群组,满载湄洲岛居民下单的六箱货品,仅耗时9分钟就成功飞越海峡,为农村淘宝提供物流服务。'
        },
        {
            src: 'images/s4.jpg',
            w: 857,
            h: 1200
        }
    ];
    
    // define options (if needed)
    var options = {
        loop: false,
        closeOnScroll: false, 
             // history & focus options are disabled on CodePen        
        history: true,
        focus: true,


        showAnimationDuration: 0,
        hideAnimationDuration: 0
        
    };
    
    var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
    gallery.init();
};

var myphoto = document.getElementById('myphoto');
myphoto.onclick = function() {
    var p_id = myphoto.getAttribute('rel');
    openPhotoSwipe(p_id);
}

效果就是这样,手动滑动图片,也可以点击放大。

我已经将最基础的演示文件和代码放到蓝奏云了,下面是链接:

https://wwa.lanzoui.com/b00ddephi
密码:dmk4

自行下载和体验吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值