jquery 图片点击放大预览功能详解

 更新时间:2023年06月16日 09:13:13   作者:九亓  
这篇文章主要介绍了jquery 图片点击放大预览功能,结合实例形式详细分析了jquery 图片点击放大预览功能相关实现步骤、
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

一个简单相册预览功能,可缩放可拖动,只是觉得这个功能应该很常用,先记录下来方便下次
代码也都是我在项目中抠出来的,如果有什么问题欢迎留言讨论

在这里插入图片描述

html

图片列表的样式我就不写出来了  但是下面的class 和 布局结构 都很重要:

    <div>
        <div><img src="http://9i0i.com/pic.php?p=images/banner.jpg" ></div>
        <div><img src="http://9i0i.com/pic.php?p=images/banner.jpg" ></div>
        <div><img src="http://9i0i.com/pic.php?p=images/banner.jpg" ></div>
        <div><img src="http://9i0i.com/pic.php?p=images/banner.jpg" ></div>
    </div>

查看图片的弹窗代码(核心): 

<div></div>	
	<div>
		<div>×</div>
		<div>
			<ul >
			</ul>
		</div>
		<div>
			<div>?</div>
			<div>?</div>
		</div>
	</div>

弹窗的样式

.seeImg_mask{ width:100%; height: 100%; position: fixed; top: 0px; bottom: 0px; left: 0px; right: 0px; background: rgba(0,0,0,0.7); margin: auto; z-index: 999; display: none; }	
	.seeImg_popup{  width: 80%; height: 80%; background: #fff; position: fixed;  bottom: 200%; left: 10%; right: 0px; margin: auto; border: 10px rgba(0,0,0,0.2) solid; border-radius: 10px; z-index: 9999;  }  
	.seeImg_close{ width: 40px; height: 40px; line-height: 35px; text-align: center; font-size: 28px; color: #fff; position: absolute; right: 0px; top: 0px; background: rgba(226, 36, 36, 0.72); cursor: pointer;  }
	.seeImg_swrieb{ width: 90%; height: 100%; margin: 0px auto; position: relative; overflow: hidden;  }
	.seeImg_list{ height: 100%; width: auto; display: flex; flex-direction: row; transition: all 0.5s; }
	.seeImg_list li{ height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; position: relative; overflow: hidden;  }
	.seeImg_list li img{
		height: 90%;display: block;position: absolute; top: -160px;cursor: all-scroll;
		-webkit-touch-callout: none; /* iOS Safari */
		-webkit-user-select: none; /* Chrome/Safari/Opera */
		-khtml-user-select: none; /* Konqueror */
		-moz-user-select: none; /* Firefox */
		-ms-user-select: none; /* Internet Explorer/Edge */
		user-select: none; /* Non-prefixed version, currently not supported by any browser */
		padding: 200px 0px;
		z-index: 555;
	}
	.seeImg_fun{ width: 100%; height: 40px; line-height: 40px; display: flex; flex-direction: row; justify-content: space-between; box-sizing: border-box; padding: 0px 15px; position: absolute; top: 0px; bottom: 0px; left: 0px; right: 0px; margin: auto;   }
	.fun_label{ font-size: 50px; color: #777;  cursor: pointer;   -webkit-touch-callout: none;  -webkit-user-select: none; -khtml-user-select: none;  -moz-user-select: none; -ms-user-select: none; user-select: none; }

js部分

$(document).on("click",".seeimg",function(){
		$(".seeImg_list li").remove();
		var seeImglistLiLeng = $(this).parents(".imgnyr").children(".img-list").length;
		$(".seeImg_popup").attr("data",seeImglistLiLeng)
		var arrtimg = [];
		$(this).parents(".imgnyr").children(".img-list").each(function(i){
			var imgse = $(this).children("img").attr("src");
			arrtimg.push(imgse)
		});
		for( var i in arrtimg){
			var html ="";
				html +="<li><img src="http://9i0i.com/pic.php?p= +arrtimg[i]+ " class='enlarge' draggable='false' onmousewheel='return bbimg(this)'  /></li>";
			$(".seeImg_list").append(html)
		}
		var seeImgswriebWight = $(".seeImg_swrieb").width();
		var seeImglistWigth = seeImgswriebWight * seeImglistLiLeng;
		console.log(seeImglistWigth)
		$(".seeImg_mask").show();
		$(".seeImg_popup").css("bottom","10%");
		$(".seeImg_list").width(seeImglistWigth)
		$(".seeImg_list").css("margin-left","0px")
		$(".seeImg_list li").width(seeImgswriebWight)
		$(".seeImg_list").children("li").each(function(i){
			$(this).children("img").attr("data",i)
			$(this).children("img").addClass("maxdom"+i)
		})
	})
	$(document).on("click",".seeImg_close",function(){
		$(".seeImg_mask").hide();
		$(".seeImg_popup").css("bottom","200%");	
	})
	var UlMarRig = 0;
	$(document).on("click",".labelRight",function(){
		var roue = $(".seeImg_swrieb").width();
		var mes = $(".seeImg_popup").attr("data");
		var csdx = roue * mes;
		console.log(csdx)
		UlMarRig = UlMarRig - roue;
		if( UlMarRig <= -csdx){
		   UlMarRig = 0;
		}
		$(".seeImg_list").css("margin-left", UlMarRig)
	})
	$(document).on("click",".labelLeft",function(){
		var emgd = $(".seeImg_swrieb").width();
		var aas = $(".seeImg_popup").attr("data");
		var ddx = emgd * aas;		
		if( UlMarRig == 0){
		   UlMarRig = -ddx;
		}
		UlMarRig = UlMarRig + emgd;
		$(".seeImg_list").css("margin-left", UlMarRig)
	})	
	var ment = 90;
	function bbimg(o){
		var zoom=parseInt(o.style.zoom, 10)||100;
			zoom+=event.wheelDelta/12;
			console.log(zoom)
			if(zoom>=110){
				ment = ment +10;
				o.style.height = ment +'%';
			}
			if(zoom<110){
				ment = ment -10;
				o.style.height = ment +'%';
			}
			// console.log(ment)
	}
	$(document).on("mousedown",".enlarge",function(e){
		var u = $(this).attr("data");
	    // e.pageX
		var imtLeft = $(this).position().left;
		var imtTop = $(this).position().top;
	    var distenceX = e.pageX - imtLeft;     //记录鼠标点击的位置与div左上角水平方向的距离
	    var distenceY = e.pageY - imtTop;     //记录鼠标点击的位置与div左上角数值方向的距离
	    $(document).mousemove(function(e){
	      var x = e.pageX - distenceX;
	      var y = e.pageY - distenceY;
	      if(x<0){
	        x= x ;
	      }else if(x>$(document).width()-$('.maxdom'+u).outerWidth(true)){
	        x = $(document).width()-$('.maxdom'+u).outerWidth(true);
	      }
	      if(y<0){
	        y= y;
	      }else if(y>$(document).height()-$('.maxdom'+u).outerHeight(true)){
	        y = $(document).height()-$('.maxdom'+u).outerHeight(true);
	      }
	      $('.maxdom'+u).css({'left':x+'px','top':y+'px'});
	    });
	    $(document).mouseup(function(){
	      $(document).off('mousemove'); //移除鼠标移动事件
	    });		
	})

感兴趣的朋友可以使用如下在线运行工具测试上述代码运行效果:

http://tools.jb51.net/code/HtmlJsRun

http://tools.jb51.net/code/WebCodeRun 

相关文章

  • JQuery获得内容和属性方法解析

    JQuery获得内容和属性方法解析

    这篇文章主要介绍了JQuery获得内容和属性方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • jQuery发送Ajax请求的几种方法举例

    jQuery发送Ajax请求的几种方法举例

    Ajax一个向后端发送请求的方式,下面这篇文章主要给大家介绍了关于jQuery发送Ajax请求的几种方法,文中通过实例代码介绍的非常详细,对大家学习或者使用jQuery具有一定的参考学习价值,需要的朋友可以参考下
    2023-06-06
  • jQuery样式操作方法整理介绍

    jQuery样式操作方法整理介绍

    这篇文章主要介绍了jQuery样式操作方法,并通过实际案例更浅显的理解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • jQuery length 和 size()区别总结

    jQuery length 和 size()区别总结

    这篇文章主要介绍了jQuery length 和 size()区别总结,在这里length是属性,size()是方法,需要的朋友可以参考下
    2018-04-04
  • jQuery实现的分页功能示例

    jQuery实现的分页功能示例

    这篇文章主要介绍了jQuery实现的分页功能,结合实例形式较为详细的分析了jQuery实现分页功能的具体步骤及相关操作技巧,包括前台样式、布局及jQuery分页插件的调用方法,需要的朋友可以参考下
    2017-01-01
  • jQuery获取与设置iframe高度的方法

    jQuery获取与设置iframe高度的方法

    这篇文章主要介绍了jQuery获取与设置iframe高度的方法,涉及jQuery针对iframe元素的获取与设置相关操作技巧,非常简单实用,需要的朋友可以参考下
    2016-08-08
  • JQuery Ajax执行跨域请求数据的解决方案

    JQuery Ajax执行跨域请求数据的解决方案

    今天小编就为大家分享一篇关于JQuery Ajax执行跨域请求数据的解决方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • jquery 中ajax执行的优先级

    jquery 中ajax执行的优先级

    本文给大家讲述的是个人再做用户注册的时候遇到的ajax执行优先级的问题,以及在网友们的帮助下,解决此问题的全过程,这里记录下来,分享给大家。
    2015-06-06
  • jQuery对html元素的取值与赋值实例详解

    jQuery对html元素的取值与赋值实例详解

    这篇文章主要介绍了jQuery对html元素的取值与赋值,较为详细的分析了jQuery针对常见html元素的获取与赋值技巧,非常简单实用,需要的朋友可以参考下
    2015-12-12
  • 文本域光标操作的jQuery扩展分享

    文本域光标操作的jQuery扩展分享

    最近的项目中,好几次用到操作文本域的方法,比如光标位置、删除光标前字符等。每次都是查阅资料(这部分操作涉及到的js方法都比较生僻),费时费事。于是就封装了一个jQuery扩展
    2014-03-03

最新评论

?


http://www.vxiaotou.com