js响应式图片幻灯片支持触屏滑动手机幻灯片代码



121 482 161



特效描述:js响应式图片 幻灯片 支持触屏滑动 手机幻灯片代码,html5响应式手机图片幻灯片代码

代码结构

1. 引入CSS

<link rel="stylesheet" href="dist/photoswipe.css">
<link rel="stylesheet" href="dist/default-skin/default-skin.css">
<link rel="stylesheet" href="css/site.css">

2. 引入JS

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

3. HTML代码

<div class="row">
	<div id="demo-test-gallery" class="demo-gallery">
		<a href="img/15008518202_c265dfa55f_h.jpg" data-size="1600x1600" data-med="img/15008465772_d50c8f0531_h.jpg" data-med-size="1024x1024" data-author="Folkert Gorter" class="demo-gallery__img--main">
			<img src="img/15008518202_b016d7d289_m.jpg" alt="" />
			<figure>This is dummy caption.</figure>
		</a>
		<a href="img/15008465772_d50c8f0531_h.jpg" data-size="1600x1067" data-med="img/15008465772_d50c8f0531_h.jpg" data-med-size="1024x683" data-author="Thomas Lefebvre">
		  <img src="img/15008465772_383e697089_m.jpg" alt="" />
		  <figure>It's a dummy caption. He who searches for meaning here will be sorely disappointed.</figure>
		</a>
		<a href="img/14985868676_b51baa4071_h.jpg" data-size="1600x1067" data-med="img/14985868676_b51baa4071_h.jpg" data-med-size="1024x683" data-author="Michael Hull">
		  <img src="img/14985868676_4b802b932a_m.jpg" alt="" />
		  <figure>Dummy caption. It's Greek to you. Unless, of course, you're Greek, in which case, it really makes no sense.</figure>
		</a>
		<a href="img/14985871946_24f47d4b53_h.jpg" data-size="1600x1067" data-med="img/14985871946_24f47d4b53_h.jpg" data-med-size="1024x683" data-author="Ales Krivec">
		  <img src="img/14985871946_86abb8c56f_m.jpg" alt="" />
		  <figure>This is dummy caption. It is not meant to be read.</figure>
		</a>
		<a href="img/15008867125_b61960af01_h.jpg" data-size="1600x1068" data-med="img/15008867125_b61960af01_h.jpg" data-med-size="1024x683" data-author="Samuel Rohl">
		  <img src="img/15008867125_68a8ed88cc_m.jpg" alt="" />
		  <figure>This is dummy caption. It has been placed here solely to demonstrate the look and feel of finished, typeset text.</figure>
		</a>
	</div>
</div>
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="pswp__bg"></div>
	<div class="pswp__scroll-wrap">
		<div class="pswp__container">
			<div class="pswp__item"></div>
			<div class="pswp__item"></div>
			<div class="pswp__item"></div>
		</div>
		<div class="pswp__ui pswp__ui--hidden">
			<div class="pswp__top-bar">
				<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>
				<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>
<script type="text/javascript">
	(function() {
	var initPhotoSwipeFromDOM = function(gallerySelector) {
		var parseThumbnailElements = function(el) {
			var thumbElements = el.childNodes,
				numNodes = thumbElements.length,
				items = [],
				el,
				childElements,
				thumbnailEl,
				size,
				item;
			for(var i = 0; i < numNodes; i++) {
				el = thumbElements[i];
				// include only element nodes 
				if(el.nodeType !== 1) {
				  continue;
				}
				childElements = el.children;
				size = el.getAttribute('data-size').split('x');
				// create slide object
				item = {
					src: el.getAttribute('href'),
					w: parseInt(size[0], 10),
					h: parseInt(size[1], 10),
					author: el.getAttribute('data-author')
				};
				item.el = el; // save link to element for getThumbBoundsFn
				if(childElements.length > 0) {
				  item.msrc = childElements[0].getAttribute('src'); // thumbnail url
				  if(childElements.length > 1) {
					  item.title = childElements[1].innerHTML; // caption (contents of figure)
				  }
				}
				var mediumSrc = el.getAttribute('data-med');
				if(mediumSrc) {
					size = el.getAttribute('data-med-size').split('x');
					// "medium-sized" image
					item.m = {
						src: mediumSrc,
						w: parseInt(size[0], 10),
						h: parseInt(size[1], 10)
					};
				}
				// original image
				item.o = {
					src: item.src,
					w: item.w,
					h: item.h
				};
				items.push(item);
			}
			return items;
		};
		// find nearest parent element
		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 === 'A';
			});
			if(!clickedListItem) {
				return;
			}
			var clickedGallery = clickedListItem.parentNode;
			var childNodes = clickedListItem.parentNode.childNodes,
				numChildNodes = childNodes.length,
				nodeIndex = 0,
				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) {
				openPhotoSwipe( index, clickedGallery );
			}
			return false;
		};
		var photoswipeParseHash = function() {
			var hash = window.location.hash.substring(1),
			params = {};
			if(hash.length < 5) { // pid=1
				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;
		};
		var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
			var pswpElement = document.querySelectorAll('.pswp')[0],
				gallery,
				options,
				items;
			items = parseThumbnailElements(galleryElement);
			// define options (if needed)
			options = {
				galleryUID: galleryElement.getAttribute('data-pswp-uid'),
				getThumbBoundsFn: function(index) {
					// See Options->getThumbBoundsFn section of docs for more info
					var thumbnail = items[index].el.children[0],
						pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
						rect = thumbnail.getBoundingClientRect(); 
					return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
				},
				addCaptionHTMLFn: function(item, captionEl, isFake) {
					if(!item.title) {
						captionEl.children[0].innerText = '';
						return false;
					}
					captionEl.children[0].innerHTML = item.title +  '<br/><small>Photo: ' + item.author + '</small>';
					return true;
				}
			};
			if(fromURL) {
				if(options.galleryPIDs) {
					// parse real index when custom PIDs are used 
					// http://photoswipe.com/documentation/faq.html#custom-pid-in-url
					for(var j = 0; j < items.length; j++) {
						if(items[j].pid == index) {
							options.index = j;
							break;
						}
					}
				} else {
					options.index = parseInt(index, 10) - 1;
				}
			} else {
				options.index = parseInt(index, 10);
			}
			// exit if index not found
			if( isNaN(options.index) ) {
				return;
			}
			var radios = document.getElementsByName('gallery-style');
			for (var i = 0, length = radios.length; i < length; i++) {
				if (radios[i].checked) {
					if(radios[i].id == 'radio-all-controls') {
					} else if(radios[i].id == 'radio-minimal-black') {
						options.mainClass = 'pswp--minimal--dark';
						options.barsSize = {top:0,bottom:0};
						options.captionEl = false;
						options.fullscreenEl = false;
						options.shareEl = false;
						options.bgOpacity = 0.85;
						options.tapToClose = true;
						options.tapToToggleControls = false;
					}
					break;
				}
			}
			if(disableAnimation) {
				options.showAnimationDuration = 0;
			}
			// Pass data to PhotoSwipe and initialize it
			gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
			// see: http://photoswipe.com/documentation/responsive-images.html
			var realViewportWidth,
				useLargeImages = false,
				firstResize = true,
				imageSrcWillChange;
			gallery.listen('beforeResize', function() {
				var dpiRatio = window.devicePixelRatio ? window.devicePixelRatio : 1;
				dpiRatio = Math.min(dpiRatio, 2.5);
				realViewportWidth = gallery.viewportSize.x * dpiRatio;
				if(realViewportWidth >= 1200 || (!gallery.likelyTouchDevice && realViewportWidth > 800) || screen.width > 1200 ) {
					if(!useLargeImages) {
						useLargeImages = true;
						imageSrcWillChange = true;
					}
				} else {
					if(useLargeImages) {
						useLargeImages = false;
						imageSrcWillChange = true;
					}
				}
				if(imageSrcWillChange && !firstResize) {
					gallery.invalidateCurrItems();
				}
				if(firstResize) {
					firstResize = false;
				}
				imageSrcWillChange = false;
			});
			gallery.listen('gettingData', function(index, item) {
				if( useLargeImages ) {
					item.src = item.o.src;
					item.w = item.o.w;
					item.h = item.o.h;
				} else {
					item.src = item.m.src;
					item.w = item.m.w;
					item.h = item.m.h;
				}
			});
			gallery.init();
		};
		// select all gallery elements
		var galleryElements = document.querySelectorAll( gallerySelector );
		for(var i = 0, l = galleryElements.length; i < l; i++) {
			galleryElements[i].setAttribute('data-pswp-uid', i+1);
			galleryElements[i].onclick = onThumbnailsClick;
		}
		// Parse URL and open gallery if it contains #&pid=3&gid=1
		var hashData = photoswipeParseHash();
		if(hashData.pid && hashData.gid) {
			openPhotoSwipe( hashData.pid,  galleryElements[ hashData.gid - 1 ], true, true );
		}
	};
	initPhotoSwipeFromDOM('.demo-gallery');
})();
</script>



用户评论
大牛,别默默的看了,快登录帮我点评一下吧!:)      登录 | 注册


热门标签: 顶部悬浮 底部悬浮 浮动导航菜单 导航切换 右键菜单 图片放大镜 图片头像上传 缩略图 图片广告 文字提示框 文字放大缩小 图片放大缩小 文件上传 图片相册 数字放大缩小 对联广告 图片切换 提示框/弹出层 文本框 图片墙 文字切换 layer 下拉框 百叶窗 叠加浮动层 背景切换 lightbox 复选 图片网格 手风琴 单选 图片九宫格 Tooltip工具提示框 选项卡自动切换 登录框 浮动提示框 视频切换 注册框 图片高亮 html5弹窗动画 列表切换 搜索框 图片对比 验证码 图片文字 渐隐切换 二级联动 单排图片 全屏切换 三级联动 响应式图片 切换按钮 城市选择 图片列表 缩放切换 value赋值 筛选 按钮控制 购物车 颜色选择 表单提交 全选/反选 添加删除 添加标签 密码强度验证 表单验证 html5按钮动画 按钮样式 密码修改设置 图片拖动 图片放大镜 图片叠加 图片翻转旋转 图片延迟加载 图片淡出淡进 图片全屏 图片头像上传 二维码 滚动切换 滑动选项卡切换 图片广告 图片放大缩小 html5图片动画 带缩略图的幻灯片 地图 图片滚动 图片滑动 图片切换 图片收缩展开 旋转木马 图片轮播 悬浮浮动漂浮 自动滚动图片轮播 图标导航菜单 弹出层拖动 选项卡切换 滑动手风琴 背景切换 窗口提示框 手风琴 浮动提示框 缩放放大缩小 切换按钮 缩放切换 form表单 图表 图片 头像截图

×
×

注册

官方QQ群

扫描上面二维码加微信群

官方QQ群

jQuery/js讨论群
群号:642649996
Css3+Html5讨论群
群号:322131262

加群请备注:从官网了解到