jQuery放大镜 实现图片的放大

@TOC使用jQuery实现简单的图片放大操作

一张图片的放大

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>放大镜</title>
</head>
	<style>
		*{margin:0px;padding:0;}
		.small{
			position: relative;
			height: 400px;
			margin-left: 5px;
			
		}
		.small img{
			height: 400px;
		}
		.box{
			position: absolute;
			background: rgba(254, 238, 167, .4);
			left:0;
			top: 0;
			display: none;
			cursor: crosshair;
		}
		.big{
			position:absolute;
			top: 0px;
			left:410px;
			width: 400px;
			height: 400px;
			overflow: hidden;
			display: none;
		}
		.big img{
			position: relative;
		}
	</style>
	<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<body>
	<div class="wrap">
		<!-- 定义一个的盒子放缩略图 -->
		<div class="small">
			<!-- 盒子中的图片 -->
			<img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561221109738&di=1c26477168b0ccfcce36da9cbd5c6829&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F78c92e0e0ad029e5dc3e4f027beacc383eb31e902de17-e0ZWwL_fw658" alt="">
			<!-- 图片中的放那个放大镜 -->
			<div class="box"></div>
		</div>
		<!-- 放大后图片的盒子 -->
		<div class="big">
			<img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561221109738&di=1c26477168b0ccfcce36da9cbd5c6829&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F78c92e0e0ad029e5dc3e4f027beacc383eb31e902de17-e0ZWwL_fw658" alt="">
		</div>
	</div>
	<script>
		
		// 当鼠标移入到小图的盒子时,放大镜盒子和大图盒子显示出来
		$('.small').mouseover(function(){
			//放大镜盒子和大图盒子显示
			$('.box').show();
			$('.big').show();
			//获取小图的图片的宽度,高度为400px
			var width = $('.small img').width();
			//获取大图的大小
			var Bwidth = $('.big img').width();
			var Bheight = $('.big img').height();
			//设置放置小图盒子的宽度
			$('.small').css('width',width+'px');
			//设置放大镜滑块的大小
			var Hwidth = ($('.big').width())/Bwidth*($('.small').width());
			var Hheight = ($('.big').height())/Bheight*($('.small').height());
			//设置放大镜盒子的大小
			$('.box').css({'width':Hwidth + 'px','height':Hheight + 'px'});
			
			//
			//设置放大镜盒子移动事件
			$('.small').mousemove(function(e){
				//获取鼠标的位置
				var x = e.clientX;
				var y = e.clientY;
				// console.log(x,y)
				//设置鼠标在放大镜盒子的中心上
				var xx = x - $('.box').width()/2;
				var yy = y -$('.box').height()/2;
				//设置使放大镜盒子不可以出边框
				//最大边界
				var maxX = $('.small').width()-$('.box').width();
				var maxY = $('.small').height()-$('.box').height();
				// console.log(maxX)
				if (xx<0) {
					xx = 0;
				}
				if (xx>maxX) {
					xx = maxX;
				}
				if (yy<0){
					yy = 0;
				}
				if (yy>maxY){
					yy = maxY;
				}
				$('.box').css({'left':xx + 'px','top':yy + 'px'});
				//
				//设置大图跟着放大镜盒子移动
				//查看放大的倍数,及大图的移动距离
				//放大的图片的移动距离为放大镜盒子移动的距离*放大的倍数
				var img_x = xx * ($('.big img').width()/$('.small img').width());
				var img_y = yy * ($('.big img').height()/$('.small img').height());
				//放大的图片的移动
				$('.big img').css({'left':-img_x + 'px','top':-img_y + 'px'});
			})

		})
		
		//鼠标离开放大镜盒子于大图盒子进行隐藏
		$('.small').mouseout(function(){
			$('.box').hide();
			$('.big').hide();
		})
	</script>
</body>
</html>

实现多张图片的放大镜效果:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>放大镜</title>
</head>
	<style>
		*{margin:0px;padding:0;}
		.small{
			position: relative;
			height: 400px;
			margin-left: 5px;
			
		}
		.small img{
			height: 400px;
		}
		.box{
			position: absolute;
			background: rgba(254, 238, 167, .4);
			left:0;
			top: 0;
			display: none;
			cursor: crosshair;
		}
		.big{
			position:absolute;
			top: 0px;
			left:410px;
			width: 400px;
			height: 400px;
			overflow: hidden;
			display: none;
		}
		.big img{
			position: relative;
		}
		ul{
			width: 570px;
			border:1px dashed #ffdf;
		}
		ul li{
			display: inline-block;
		}
		ul li img{
			height: 156px;
		}
	</style>
	<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<body>
	<div class="wrap">
		<!-- 定义一个的盒子放缩略图 -->
		<div class="small">
			<!-- 盒子中的图片 -->
			<img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561221109738&di=1c26477168b0ccfcce36da9cbd5c6829&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F78c92e0e0ad029e5dc3e4f027beacc383eb31e902de17-e0ZWwL_fw658" alt="">
			<!-- 图片中的放那个放大镜 -->
			<div class="box"></div>
		</div>
		<!-- 放大后图片的盒子 -->
		<div class="big">
			<img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561221109738&di=1c26477168b0ccfcce36da9cbd5c6829&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F78c92e0e0ad029e5dc3e4f027beacc383eb31e902de17-e0ZWwL_fw658" alt="">
		</div>
	</div>
	<ul class="nav">
		<li><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561221109738&di=1c26477168b0ccfcce36da9cbd5c6829&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F78c92e0e0ad029e5dc3e4f027beacc383eb31e902de17-e0ZWwL_fw658" alt=""></li>
		<li><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561230022906&di=8808155e0d8e5d5fe083596b4cce9700&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fbed197486e6f662dbd5bf6c927611c460ad7a2674a5f0-KcQhBq_fw658" alt=""></li>
		<li><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561230022906&di=c4412bc2c6c74b4fb8caeb0891a7367f&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F34b5d2ff24b87494470669b0a1b7f541d9e785a428cc9-6UnUhC_fw658" alt=""></li>
		<li><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561230022907&di=291e490c3643c4264eeb3c415eb0ebe2&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Ffc7b7a6caa33bc11eff990b8157dcd88eea8b0c124745-bLXnyq_fw658" alt=""></li>
	</ul>
	<script>
		
		// 当鼠标移入到小图的盒子时,放大镜盒子和大图盒子显示出来
		$('.small').mouseover(function(){
			//放大镜盒子和大图盒子显示
			$('.box').show();
			$('.big').show();
			//获取小图的图片的宽度,高度为400px
			var width = $('.small img').width();
			//获取大图的大小
			var Bwidth = $('.big img').width();
			var Bheight = $('.big img').height();
			//设置放置小图盒子的宽度
			$('.small').css('width',width+'px');
			//设置放大镜滑块的大小
			var Hwidth = ($('.big').width())/Bwidth*($('.small').width());
			var Hheight = ($('.big').height())/Bheight*($('.small').height());
			//设置放大镜盒子的大小
			$('.box').css({'width':Hwidth + 'px','height':Hheight + 'px'});
			
			//
			//设置放大镜盒子移动事件
			$('.small').mousemove(function(e){
				//获取鼠标的位置
				var x = e.clientX;
				var y = e.clientY;
				// console.log(x,y)
				//设置鼠标在放大镜盒子的中心上
				var xx = x - $('.box').width()/2;
				var yy = y -$('.box').height()/2;
				//设置使放大镜盒子不可以出边框
				//最大边界
				var maxX = $('.small').width()-$('.box').width();
				var maxY = $('.small').height()-$('.box').height();
				// console.log(maxX)
				if (xx<0) {
					xx = 0;
				}
				if (xx>maxX) {
					xx = maxX;
				}
				if (yy<0){
					yy = 0;
				}
				if (yy>maxY){
					yy = maxY;
				}
				$('.box').css({'left':xx + 'px','top':yy + 'px'});
				//
				//设置大图跟着放大镜盒子移动
				//查看放大的倍数,及大图的移动距离
				//放大的图片的移动距离为放大镜盒子移动的距离*放大的倍数
				var img_x = xx * ($('.big img').width()/$('.small img').width());
				var img_y = yy * ($('.big img').height()/$('.small img').height());
				//放大的图片的移动
				$('.big img').css({'left':-img_x + 'px','top':-img_y + 'px'});
			})

		})
		//
		//实现图片的切换
		$('.nav li').click(function(){
			var Src = $(this).find('img').attr('src');
			//设置小图和大图框子中的图片
			$('.small img').attr('src',Src);
			$('.big img').attr('src',Src);
		})
		//鼠标离开放大镜盒子于大图盒子进行隐藏
		$('.small').mouseout(function(){
			$('.box').hide();
			$('.big').hide();
		})
	</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值