jQuery实现朋友圈查看图片


Posted in jQuery onSeptember 11, 2020

jQuery实现朋友圈查看图片效果,供大家参考,具体内容如下

效果: 图片点击显示大图,多张图可以滑动,左右按钮点击可切换查看图片 (左右点击切换效果不需要删除样式即可)

jQuery实现朋友圈查看图片

index.html 文件

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui">
 <title>jQuery模仿朋友圈查看图片效果</title>
 <link rel="stylesheet" type="text/css" href="css/css.css" >
 <link rel="stylesheet" type="text/css" href="touchTouch/touchTouch.css" >
 <script src="js/jquery-3.4.1.js"></script>
 <script src="js/touchTouch.jquery.js"></script>
 <script>
 $(function() {
 //图片事件 img-gather处为图片效果展示
 $('#thumbs a').touchTouch();
 });
 </script>
 </head>
 
 <body>
 <!--content-->
 <div class="content">
 <!--img-gather-->
 <div class="clearfix img-gather" id="thumbs">
 <a href="images/img1-large.png" style="background-image:url(images/img1-large.png)" title="图片一"></a>
 <a href="images/img2-large.jpg" style="background-image:url(images/img2-large.jpg)" title="图片二"></a>
 <a href="images/img1-large.png" style="background-image:url(images/img1-large.png)" title="图片一"></a>
 <a href="images/img2-large.jpg" style="background-image:url(images/img2-large.jpg)" title="图片二"></a>
 <a href="images/img1-large.png" style="background-image:url(images/img1-large.png)" title="图片一"></a>
 <a href="images/img2-large.jpg" style="background-image:url(images/img2-large.jpg)" title="图片二"></a>
 </div>
 <!--img-gather end-->
 </div>
 </body>
</html>

touchTouch.css 文件

预加载loading动图 preloader.gif

jQuery实现朋友圈查看图片

左右切换按钮图片 arrows.png(不需要可删除)

jQuery实现朋友圈查看图片

#galleryOverlay{
 width:100%;
 height:100%;
 position:fixed;
 top:0;
 left:0;
 opacity:0;
 z-index:100000;
 background-color:#222;
 background-color:rgba(0,0,0,1);
 overflow:hidden;
 display:none;
 -moz-transition:opacity 1s ease;
 -webkit-transition:opacity 1s ease;
 transition:opacity 1s ease;
}
#galleryOverlay.visible{
 opacity:1;
}
#gallerySlider{
 height:100%;
 left:0;
 top:0;
 width:100%;
 white-space: nowrap;
 position:absolute;
 -moz-transition:left 0.4s ease;
 -webkit-transition:left 0.4s ease;
 transition:left 0.4s ease;
}
#gallerySlider .placeholder{
 /* preloader.gif 预加载loading动图 */
 background: url("preloader.gif") no-repeat center center;
 height: 100%;
 line-height: 1px;
 text-align: center;
 width:100%;
 display:inline-block;
}
#gallerySlider .placeholder:before{
 content: "";
 display: inline-block;
 height: 50%;
 width: 1px;
 margin-right:-1px;
}
#gallerySlider .placeholder img{
 display: inline-block;
 max-height: 100%;
 max-width: 100%;
 vertical-align: middle;
}
#gallerySlider.rightSpring{
 -moz-animation: rightSpring 0.3s;
 -webkit-animation: rightSpring 0.3s;
}
#gallerySlider.leftSpring{
 -moz-animation: leftSpring 0.3s;
 -webkit-animation: leftSpring 0.3s;
}
/* Firefox Keyframe Animations */
@-moz-keyframes rightSpring{
 0%{
 margin-left:0px;
 }
 50%{
 margin-left:-30px;
 }
 100%{
 margin-left:0px;
 }
}
@-moz-keyframes leftSpring{
 0%{
 margin-left:0px;
 }
 50%{
 margin-left:30px;
 }
 100%{
 margin-left:0px;
 }
}
/* Safari and Chrome Keyframe Animations */
@-webkit-keyframes rightSpring{
 0%{
 margin-left:0px;
 }
 50%{
 margin-left:-30px;
 }
 100%{
 margin-left:0px;
 }
}
@-webkit-keyframes leftSpring{
 0%{
 margin-left:0px;
 }
 50%{
 margin-left:30px;
 }
 100%{
 margin-left:0px;
 }
}
/* 左右切换按钮 */
/* arrows.png 左右切换按钮图片 不需要可删除 */
#prevArrow,#nextArrow{
 border:none;
 text-decoration:none;
 background:url('arrows.png') no-repeat;
 opacity:1;
 cursor:pointer;
 position:absolute;
 width:43px;
 height:58px;
 top:50%;
 margin-top:-29px;
 -moz-transition:opacity 0.2s ease;
 -webkit-transition:opacity 0.2s ease;
 transition:opacity 0.2s ease;
}
#prevArrow:hover, #nextArrow:hover{
 opacity:1;
}
#prevArrow{
 background-position:left top;
 left:40px;
}
#nextArrow{
 background-position:right top;
 right:40px;
}
/* 页码 */
#pagelimit{
 position:absolute;
 bottom:20px;
 left:50%;
 margin-left:-18px;
 color:#fff;
 font-size:1.4rem;
}

touchTouch.jquery.js 文件

(function(){
 /* Private variables */
 var overlay = $('<div id="galleryOverlay">'),
 slider = $('<div id="gallerySlider">'),
 prevArrow = $('<a id="prevArrow"></a>'),
 nextArrow = $('<a id="nextArrow"></a>'),
 pageSpan = $('<span id="pagelimit"></span'),
 overlayVisible = false;
 
 /* Creating the plugin */
 $.fn.touchTouch = function(){
 var placeholders = $([]),
 pl1=[],
 index = 0,
 items = this;
 
 // Appending the markup to the page
 overlay.hide().appendTo('body');
 slider.appendTo(overlay);
 pageSpan.appendTo(overlay);
 
 // Creating a placeholder for each image
 items.each(function(){
 placeholders = placeholders.add($('<div class="placeholder">'));
 });
 
 // Hide the gallery if the background is touched / clicked
 slider.append(placeholders).on('click',function(e){
 hideOverlay(); 
 });
 
 // Listen for touch events on the body and check if they
 // originated in #gallerySlider img - the images in the slider.
 $('body').on('touchstart', '#gallerySlider img', function(e){
 var touch = e.originalEvent,
 startX = touch.changedTouches[0].pageX;
 slider.on('touchmove',function(e){
 e.preventDefault();
 touch = e.originalEvent.touches[0] ||e.originalEvent.changedTouches[0];
 if(touch.pageX - startX > 10){
 slider.off('touchmove');
 showPrevious();
 }
 else if (touch.pageX - startX < -10){
 slider.off('touchmove');
 showNext();
 }
 
 });

 // Return false to prevent image 
 // highlighting on Android
 return false;
 }).on('touchend',function(){
 slider.off('touchmove');
 });
 
 // Listening for clicks on the thumbnails
 
 //评论事件
 
 items.on('click', function(e){
 e.preventDefault();
 // Find the position of this image
 // in the collection
 
 index = items.index(this);
 showOverlay(index);
 showImage(index);
 
 calcPages(items,index);
 // Preload the next image
 preload(index+1);
 
 // Preload the previous
 preload(index-1);
 $(document).data("overlayVisible",true);
 e.cancelBubble = true; //取消冒泡事件
 //e.stopPropagation(); 
 
 });
 
 
 
 function calcPages(items,index){
 pageSpan.text((index+1)+"/"+items.length);
 }
 // If the browser does not have support 
 // for touch, display the arrows
 if ( !("ontouchstart" in window) ){
 overlay.append(prevArrow).append(nextArrow);
 
 prevArrow.click(function(e){
 e.preventDefault();
 showPrevious();
 });
 
 nextArrow.click(function(e){
 e.preventDefault();
 showNext();
 });
 }
 
 // Listen for arrow keys
 $(window).bind('keydown', function(e){
 if (e.keyCode == 37){
 showPrevious();
 }
 else if (e.keyCode==39){
 showNext();
 }
 });
 
 
 /* Private functions */
 
 
 function showOverlay(index){
 // If the overlay is already shown, exit
 if (overlayVisible){
 return false;
 }
 
 // Show the overlay
 overlay.show();
 
 setTimeout(function(){
 // Trigger the opacity CSS transition
 overlay.addClass('visible');
 }, 100);
 
 // Move the slider to the correct image
 offsetSlider(index);
 
 // Raise the visible flag
 overlayVisible = true;
 }
 
 function hideOverlay(){
 // If the overlay is not shown, exit
 
 if(!overlayVisible){
 return false;
 }
 
 // Hide the overlay
 overlay.hide().removeClass('visible');
 overlayVisible = false;
 $(document).data("overlayVisible",overlayVisible);
 }
 
 function offsetSlider(index){
 // This will trigger a smooth css transition
 slider.css('left',(-index*100)+'%');
 }
 
 // Preload an image by its index in the items array
 function preload(index){
 setTimeout(function(){
 showImage(index);
 }, 1000);
 }
 
 // Show image in the slider
 function showImage(index){
 
 // If the index is outside the bonds of the array
 if(index < 0 || index >= items.length){
 return false;
 }
 
 // Call the load function with the href attribute of the item
 loadImage(items.eq(index).attr('href'), function(){
 placeholders.eq(index).html(this);
 });
 }
 
 // Load the image and execute a callback function.
 // Returns a jQuery object
 
 function loadImage(src, callback){
 var img = $('<img>').on('load', function(){
 callback.call(img);
 });
 img.attr('src',src);
 }
 
 function showNext(){
 
 // If this is not the last image
 if(index+1 < items.length){
 index++;
 offsetSlider(index);
 preload(index+1);
 calcPages(items,index);
 }
 else{
 // Trigger the spring animation
 
 slider.addClass('rightSpring');
 setTimeout(function(){
 slider.removeClass('rightSpring');
 },500);
 }
 }
 
 function showPrevious(){
 
 // If this is not the first image
 if(index>0){
 index--;
 offsetSlider(index);
 preload(index-1);
 calcPages(items,index);
 }
 else{
 // Trigger the spring animation
 slider.addClass('leftSpring');
 setTimeout(function(){
 slider.removeClass('leftSpring');
 },500);
 }
 }
 };
})(jQuery);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jQuery操作css样式
May 15 jQuery
jQuery Validate表单验证插件实现代码
Jun 08 jQuery
jQuery序列化后的表单值转换成Json
Jun 16 jQuery
jquery动态赋值id与动态取id方法示例
Aug 21 jQuery
使用jQuery实现页面定时弹出广告效果
Aug 24 jQuery
基于jquery.page.js实现分页效果
Jan 01 jQuery
jQuery滚动条美化插件nicescroll简单用法示例
Apr 18 jQuery
jQuery中元素选择器(element)简单用法示例
May 14 jQuery
jQuery UI实现动画效果代码分享
Aug 19 jQuery
使用jquery的cookie实现登录页记住用户名和密码的方法
Mar 13 jQuery
jquery操作select常见方法大全【7种情况】
May 28 jQuery
jQuery实现查看图片功能
Dec 01 jQuery
jQuery实现日历效果
Sep 11 #jQuery
jquery实现简单每周轮换的日历
Sep 10 #jQuery
如何使用jQuery操作Cookies方法解析
Sep 08 #jQuery
jQuery实现简单三级联动效果
Sep 05 #jQuery
如何基于jQuery实现五角星评分
Sep 02 #jQuery
jQuery中getJSON跨域原理的深入讲解
Sep 02 #jQuery
Jquery $.map使用方法实例详解
Sep 01 #jQuery
You might like
WordPress开发中用于标题显示的相关函数使用解析
2016/01/07 PHP
TP3.2.3框架文件上传操作实例详解
2020/01/23 PHP
jQuery简单实现隐藏以及显示特效
2015/02/26 Javascript
11种ASP连接数据库的方法
2015/09/18 Javascript
jQuery的Each比JS原生for循环性能慢很多的原因
2016/07/05 Javascript
Vuejs第八篇之Vuejs组件的定义实例解析
2016/09/05 Javascript
微信小程序 progress组件详解及实例代码
2016/10/25 Javascript
JS 对java返回的json格式的数据处理方法
2016/12/05 Javascript
基于vue.js实现侧边菜单栏
2017/03/20 Javascript
angular或者js怎么确定选中ul中的哪几个li
2017/08/16 Javascript
jQuery模拟爆炸倒计时功能实例代码
2017/08/21 jQuery
cocos creator Touch事件应用(触控选择多个子节点的实例)
2017/09/10 Javascript
template.js前端模板引擎使用详解
2017/10/10 Javascript
js使用文档就绪函数动态改变页面内容示例【innerHTML、innerText】
2019/11/07 Javascript
微信公众号H5之微信分享常见错误和问题(小结)
2019/11/14 Javascript
[07:59]2014DOTA2叨叨刀塔 林熊猫称被邀请赛现场盛况震撼
2014/07/21 DOTA
Python减少循环层次和缩进的技巧分析
2016/03/15 Python
Python 装饰器深入理解
2017/03/16 Python
Python实现监控键盘鼠标操作示例【基于pyHook与pythoncom模块】
2018/09/04 Python
Python scrapy增量爬取实例及实现过程解析
2019/12/24 Python
Python中实现一行拆多行和多行并一行的示例代码
2020/09/06 Python
python绘制汉诺塔
2021/03/01 Python
CSS3 @media的基本用法总结
2019/09/10 HTML / CSS
使用数据结构给女朋友写个Html5走迷宫游戏
2019/11/26 HTML / CSS
美国价格实惠的在线眼镜网站:Zeelool
2020/12/25 全球购物
党支部书记岗位责任制
2014/02/11 职场文书
和解协议书
2014/04/16 职场文书
《得道多助,失道寡助》教学反思
2014/04/19 职场文书
理发店策划方案
2014/06/05 职场文书
交通志愿者活动总结
2014/06/27 职场文书
关于感恩的演讲稿200字
2014/08/26 职场文书
2014年应急管理工作总结
2014/11/26 职场文书
2015年教务处干事工作总结
2015/07/22 职场文书
宝宝满月祝酒词
2015/08/10 职场文书
四年级语文教学反思
2016/03/03 职场文书
go select编译期的优化处理逻辑使用场景分析
2021/06/28 Golang