JQuery 放大镜

JQuery 放大镜


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>放大镜</title>
		<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
		<style type="text/css">
			*{
				margin: 0px;
				padding: 0px;
			}
			.box{
				width: 1200px;
				height: 500px;
				margin: 0 auto;
			}
			.left{
				width: 600px;
				height: 100%;
				float: left;
			}
			.left>div:nth-child(1){
				width: 400px;
				height: 400px;
				position: relative;
			}
			.left>div:nth-child(2){
				width: 400px;
				height: 100px; 
			}
			.left>div:nth-child(1)>div{
				width: 160px;
				height: 160px;
				background:rgba(255,250,232,0.4);
				position: absolute;
				display: none;
			}
			.left>div:nth-child(1)>div:hover{
				cursor: move;
			}
			.left>div:nth-child(1)>img{
				width: 400px;
				height: 400px;
			}
			.left>div:nth-child(2)>img{
				width: 100px;
				height: 100px;
				float: left;
				filter: grayscale(100%);
			}
			.left>div:nth-child(2)>.hea{
				filter: grayscale(0);
			}
			.right{
				width: 600px;
				height: 100%;
				float: left;
			}
			.right>div{
				width: 400px;
				height: 400px;
				overflow: hidden;
				position: relative;
				display: none;
			}
			.right>div>img{
				position: absolute;
				
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="left">
				<div>
					<img src="img/a.jpg"/>
					<div></div>
				</div>
				<div>
					<img class="hea" src="img/a.jpg"/>
					<img src="img/d.jpg"/>
					<img src="img/c.jpg"/>
					<img src="img/b.jpg"/>
				</div>
			</div>
			<div class="right">
				<div>
					<img src="img/a.jpg"/>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">

		function magnifying(left,right){
			this.left=document.getElementsByClassName(left);
			this.right=document.getElementsByClassName(right); 
			this.myclick();
			this.mymouseenter();
			this.mymouseleave();
			this.mymousemove();
		}
		magnifying.prototype.myclick=function(){
			var that = this;
			$(this.left).children().eq(1).children().click(function(){ 
				$(this).siblings().removeClass('hea');
				$(this).addClass('hea')
				$(this).parent().prev().empty();
				$(this).parent().prev().append($(this).clone(true));
				$(this).parent().prev().append($("<div></div>"));
				$(that.right).children().first().empty();
				$(that.right).children().first().append($(this).clone(true));
			}) 
		}
		magnifying.prototype.mymouseenter=function(){ 
			$(this.left).children().eq(0).on('mouseenter',function(){
				$(this).children().eq(1).css('display','block');
			})
		}
		magnifying.prototype.mymouseleave=function(){
			var that = this;
			$(this.left).children().eq(0).on('mouseleave',function(){
				$(this).children().eq(1).css('display','none');
				$(that.right).children().first().css('display','none');
			})
		}
		magnifying.prototype.mymousemove=function(){
			var that = this;
			$(this.left).children().eq(0).on('mousemove',function(e){
				$(that.right).children().first().css('display','block'); 
				var x = parseInt(e.pageX);
				var y = parseInt(e.pageY);
				var fx = parseInt($(that.left).offset().left);
				var fy = parseInt($(that.left).offset().top);
				var cx = x-fx-parseInt($(this).children().eq(1).width())/2;
				var cy = y-fy-parseInt($(this).children().eq(1).height())/2;
				if(cx<=0){
					cx=0;
				}
				if(cx>=$(that.left).children().first().width()-$(this).children().eq(1).width()){
					cx=$(that.left).children().first().width()-$(this).children().eq(1).width();
				}
				if(cy<=0){
					cy=0;
				}
				if(cy>=$(that.left).children().first().height()-$(this).children().eq(1).height()){
					cy=$(that.left).children().first().height()-$(this).children().eq(1).height();
				}
				piyix = cx / parseInt($(that.left).children().first().width());
				piyiy = cy / parseInt($(that.left).children().first().height());
				bigx = parseInt($(that.right).children().children().first().width()) * piyix;
				bigy = parseInt($(that.right).children().children().first().height()) * piyiy; 
				$(that.right).children().children().first().css('left',-bigx+'px');
				$(that.right).children().children().first().css('top',-bigy+'px');
				$(this).children().eq(1).css('left',cx+'px');
				$(this).children().eq(1).css('top',cy+'px');
			})
			
				
		}

		var mymagnifying=new magnifying('left','right' );
		
		
	</script>
</html>

效果图

透明块为鼠标移入效果

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值