点击小图查看大图,双指缩放,点击放大,流畅度爆爽
发布在APICloud移动应用开发平台2015年5月26日view:6667
在文章任何区域双击击即可给文章添加【评注】!浮到评注点上可以查看详情。

今天分享的是一个图片列表页点击小图查看大图的功能的插件,原生JS,流畅度100%,纯前段 双指缩放,双击放大,拖动切换,上拉或下拉退出,有进度条,分页。可以完美替代相册模块。 需要用到的插件photoswipe。先来张效果图片 enter image description here enter image description here 图片列表容器代码:

<div class="my-simple-gallery">
<figure itemprop="associatedMedia" itemscope=""><a href="原图地址" data-size="图片尺寸(格式300X300)"><img src="缩略图"></a></figure>
<figure itemprop="associatedMedia" itemscope=""><a href="原图地址" data-size="图片尺寸(格式300X300)"><img src="缩略图"></a></figure>
</div>

给列表使用函数

initPhotoSwipeFromDOM('.my-simple-gallery');

大图展示的容器,看不明白可以直接拿去使用的

<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true" style="">
        <div class="pswp__bg"></div>
        <div class="pswp__scroll-wrap">
                <div class="pswp__container" style="transform: translate3d(0px, 0px, 0px);">
                        <div class="pswp__item" style="display: block; transform: translate3d(-1792px, 0px, 0px);">
                                <div class="pswp__zoom-wrap" style="transform: translate3d(288px, 56px, 0px) scale(1);"></div>
                        </div>
                        <div class="pswp__item" style="transform: translate3d(0px, 0px, 0px);">
                                <div class="pswp__zoom-wrap" style="transform: translate3d(473px, 66.8125px, 0px) scale(0.146484375);"></div>
                        </div>
                        <div class="pswp__item" style="display: block; transform: translate3d(1792px, 0px, 0px);">
                                <div class="pswp__zoom-wrap" style="transform: translate3d(447px, 44px, 0px) scale(0.6904296875);"></div>
                        </div>
                </div>
                <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
                <div class="pswp__ui pswp__ui--fit pswp__ui--hidden">
                        <div class="pswp__top-bar">
                                <div class="pswp__counter">
                                        4 / 4
                                </div>
                                <button class="pswp__button pswp__button--close" title="Close (Esc)"></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__caption">
                                <div class="pswp__caption__center"></div>
                        </div>
                </div>
        </div>
</div>

加载所需JS文件,别看JS文件有点多,但是处理起来效果还是非常棒的,后面会有文件打包下载

<script src="../script/touchPhotos/stopExecutionOnTimeout.js"></script>
<script src="../script/touchPhotos/photoswipe.min.js"></script>
<script src="../script/touchPhotos/photoswipe-ui-default.min.js"></script>

<script>
var initPhotoSwipeFromDOM = function(gallerySelector) {
var parseThumbnailElements = function(el) {
        var thumbElements = el.childNodes, numNodes = thumbElements.length, items = [], figureEl, childElements, linkEl, size, item;
        for (var i = 0; i < numNodes; i++) {
                if (window.CP.shouldStopExecution(1)) {
                        break;
                }
                figureEl = thumbElements;
                if (figureEl.nodeType !== 1) {
                        continue;
                }
                linkEl = figureEl.children[0];
                size = linkEl.getAttribute('data-size').split('x');
                item = {
                        src : linkEl.getAttribute('href'),
                        w : parseInt(size[0], 10),
                        h : parseInt(size[1], 10)
                };
                if (figureEl.children.length > 1) {
                        item.title = figureEl.children[1].innerHTML;
                }
                if (linkEl.children.length > 0) {
                        item.msrc = linkEl.children[0].getAttribute('src');
                }
                item.el = figureEl;
                items.push(item);
        }
        window.CP.exitedLoop(1);
        return items;
};
var closest = function closest(el, fn) {
        return el && (fn(el) ? el : closest(el.parentNode, fn));
};
var onThumbnailsClick = function(e) {
        e = e || window.event;
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
        var eTarget = e.target || e.srcElement;
        var clickedListItem = closest(eTarget, function(el) {
                return el.tagName && el.tagName.toUpperCase() === 'FIGURE';
        });
        if (!clickedListItem) {
                return;
        }
        var clickedGallery = clickedListItem.parentNode, childNodes = clickedListItem.parentNode.childNodes, numChildNodes = childNodes.length, nodeIndex = 0, index;
        for (var i = 0; i < numChildNodes; i++) {
                if (window.CP.shouldStopExecution(2)) {
                        break;
                }
                if (childNodes.nodeType !== 1) {
                        continue;
                }
                if (childNodes === clickedListItem) {
                        index = nodeIndex;
                        break;
                }
                nodeIndex++;
        }
        window.CP.exitedLoop(2);
        if (index >= 0) {
                openPhotoSwipe(index, clickedGallery);
        }
        return false;
};
var photoswipeParseHash = function() {
        var hash = window.location.hash.substring(1), params = {};
        if (hash.length < 5) {
                return params;
        }
        var vars = hash.split('&');
        for (var i = 0; i < vars.length; i++) {
                if (window.CP.shouldStopExecution(3)) {
                        break;
                }
                if (!vars) {
                        continue;
                }
                var pair = vars.split('=');
                if (pair.length < 2) {
                        continue;
                }
                params[pair[0]] = pair[1];
        }
        window.CP.exitedLoop(3);
        if (params.gid) {
                params.gid = parseInt(params.gid, 10);
        }
        if (!params.hasOwnProperty('pid')) {
                return params;
        }
        params.pid = parseInt(params.pid, 10);
        return params;
};
var openPhotoSwipe = function(index, galleryElement, disableAnimation) {
        var pswpElement = document.querySelectorAll('.pswp')[0], gallery, options, items;
        items = parseThumbnailElements(galleryElement);
        options = {
                index : index,
                galleryUID : galleryElement.getAttribute('data-pswp-uid'),
                getThumbBoundsFn : function(index) {
                        var thumbnail = items[index].el.getElementsByTagName('img')[0], pageYScroll = window.pageYOffset || document.documentElement.scrollTop, rect = thumbnail.getBoundingClientRect();
                        return {
                                x : rect.left,
                                y : rect.top + pageYScroll,
                                w : rect.width
                        };
                },
                history : false,
                focus : false
        };
        if (disableAnimation) {
                options.showAnimationDuration = 0;
        }
        gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
};
var galleryElements = document.querySelectorAll(gallerySelector);
for (var i = 0, l = galleryElements.length; i < l; i++) {
        if (window.CP.shouldStopExecution(4)) {
                break;
        }
        galleryElements.setAttribute('data-pswp-uid', i + 1);
        galleryElements.onclick = onThumbnailsClick;
}
window.CP.exitedLoop(4);
var hashData = photoswipeParseHash();
if (hashData.pid > 0 && hashData.gid > 0) {
        openPhotoSwipe(hashData.pid - 1, galleryElements[hashData.gid - 1], true);
}
};

</script>

原文地址:http://community.apicloud.com/bbs/forum.php?mod=viewthread&tid=5239&extra=page%3D3%26filter%3Dtypeid%26typeid%3D15

评论
发表评论
暂无评论
WRITTEN BY
PUBLISHED IN
APICloud移动应用开发平台
  • 7天开发APP,重新定义移动应用开发。
  • 可以帮助开发者快速实现移动应用的开发、测试、发布、管理和运营的全生命周期管理。
  • 云API:减少70%服务器端编码工作量,同时提供推送、云修复、大数据分析等服务。
  • 端API:“积木拼装”式的开发跨平台APP,为APP开发者提供大量优质的原生模块,涵盖UI、系统、交互、影音、支付等类型的原生模块。
  • 模块store:“模块Store”通过提供“1+1”的服务(一站式+一键集成),让APP开发以及第三方服务SDK的集成变得非常简单,可以像逛超市一样在APICloud“模块Store”挑选自己中意的第三方服务,只需在线一键绑定,就可以实现不同服务的快速集成。
  • 官方网站:www.apicloud.com

友情链接 大搜车前端团队博客
我的收藏