jquery放大镜

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>放大镜</title>
<style type="text/css">
.bigbox{
	width: 710px;
	margin: 0 auto;
}
.box1{
	width: 300px;
	height: 300px;
	border: 1px solid #ccc;
	position: relative;
	float: left;
}
.box1 img{
	width: 300px;
}
.box2{
	width: 400px;
	height: 400px;
	border: 1px solid #ccc;
	position: relative;
	float: left;
	overflow: hidden;
	display: none;
}
.box2 img{
	width: 600px;
	position: absolute;
}
.small{
	width: 180px;
	height: 180px;
	background: rgba(0,0,0,0.3);
	position: absolute;
	top: 0;
	left: 0;
	display: none
}
</style>
</head>
<body>
	<div class="bigbox">
		<div class="box1">
			<img src="images/11.jpg" alt="">
			<div class="small"></div>
		</div>
		<div class="box2">
			<img src="images/11.jpg" alt="" class="imgs">
		</div>
	</div>
</body>
<script type="text/javascript" src="3.3.1.js"></script>
<script type="text/javascript">
$(function(){
	//获取元素
	var box1 = $(".box1")
	var small = $(".small")
	var box2 = $(".box2")
	var imgs = $(".imgs")
	// 创建鼠标移入移出事件
	box1.hover(function(){
		//移入显示隐藏的盒子
		small.css("display","block")
		box2.css("display","block")
	},function(){
		//移出继续隐藏
		small.css("display","none")
		box2.css("display","none")
	})
	//创建鼠标移动事件
	box1.mousemove(function(e){
		var e = e || event;
		//获取小盒子可移动的距离
		var x = e.clientX - box1.offset().left - small[0].offsetWidth/2;
		var y = e.clientY - box1.offset().top - small[0].offsetHeight/2;
		// 判断小盒子到大盒子左边距为0时,让他保持为0
		if (x<0) {
			x=0;
		}
		// 判断小盒子到大盒子上边距为0时,让他保持为0
		if (y<0) {
			y=0;
		}
		// 判断小盒子到大盒子右边距为最大可移动距离时,让他保持为最大可移动距离
		if (x>box1[0].offsetWidth-small[0].offsetWidth) {
			x=box1[0].offsetWidth-small[0].offsetWidth
		}
		// 判断小盒子到大盒子下边距为最大可移动距离时,让他保持为最大可移动距离
		if (y>box1[0].offsetHeight-small[0].offsetHeight) {
			y=box1[0].offsetHeight-small[0].offsetHeight
		}
		//小盒子的位置
		small.css({"left":x+"px","top":y+"px"})
		//隐藏的大盒子的显示位置
		imgs.css({"left":-x*2+"px","top":-y*2+"px"})
	})
})
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值