jQuery学习:点击元素实现放大镜效果

 

 

 重点在于设置

如何更新left  top值 根据鼠标的移动

			function bigImg() {
				var $mask = $('#mask'); //遮挡需要加显示
				var $mediuImg = $('#mediuImg'); //鼠标移入图片需要控制效黄块的显示
				var $loading = $('#loading');
				var $largeImg = $('#largeImg');
				var $largeImgContainer = $('#largeImgContainer');
				var $mediuImgContainer = $('#mediuImgContainer');
				var $mediuImgContainerHeight = $mediuImgContainer.height();
				var $mediuImgContainerWidth = $mediuImgContainer.width();
				$mediuImgContainer.hover(function() {
						//显示 并得出left top值
						$mask.show();
						//动态加载大图 加载对应大图的src
						var src = $mediuImg.attr('src');
						$largeImgContainer.show();
						//图片的加载完成需要针对图片是否加载成功
						//on机制--加载单一图片
						$largeImg.attr('src', src);
						//只有在图片加载完成后才有意义
						$largeImg.on('load', function() {

							//监听鼠标移动事件
							$mediuImgContainer.mousemove(function(event) {
								//在移动过程中不断调用
								var left = 0;
								var top = 0;
								//给小方块定位
								//鼠标的位置
								left = event.clientX - $mediuImgContainer.offset().left - $mask.width() / 2;
								top = event.clientY - $mediuImgContainer.offset().top - $mask.height() / 2;
								//区间需要有限制 0-$mediuImgContainerWidth-$mask.width()
								//0-0-$mediuImgContainerHeight-$mask.height()
								if (left < 0) {
									left = 0
								} else if (left > $mediuImgContainerWidth - $mask.width()) {
									left = $mediuImgContainerWidth - $mask.width();
								}
								if (top < 0) {
									top = 0
								} else if (top > $mediuImgContainerHeight - $mask.height()) {
									top = $mediuImgContainerHeight - $mask.height();
								}

								$mask.css({
									left: left,
									top: top
								});

								//移动大图片
								//区间需要有限制 0-$mediuImgContainerWidth-$mask.width()   2倍关系
								//0-0-$mediuImgContainerHeight-$mask.height()
								left = left * 2;
								top = top * 2;
								//设置大图的坐标
								if (left < 0) {
									left = 0
								} else if (left > ($mediuImgContainerWidth - $mask.width()) * 2) {
									left = (left > ($mediuImgContainerWidth - $mask.width()) * 2) * 2
								}

								if (top < 0) {
									top = 0
								} else if (top > ($mediuImgContainerHeight - $mask.height()) * 2) {
									top = (top > $mediuImgContainerHeight - $mask.height()) * 2
								}
								
								$largeImg.css({
									left: -left,
									top: -top
								});
								//显示大图
								$largeImg.show();
								//隐藏加载记录条
								$loading.hide();
							});
						});


					},
					function() {
						$mask.hide();
							$largeImgContainer.hide();
						
						

					});


			}
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../js/jquery-3.6.1.js"></script>
		<style>
			* {
				margin: 0;
				padding: 0;
			}

			li {
				list-style-type: none;
			}

			#product_intro {
				position: relative;

				height: 600px;
				width: 520px;
				left: 100px;
				top: 100px;
			}

			#mediuImg {
				width: 520px;
				height: 450px;
			}

			.backward,
			.forword {
				display: inline-block;
				font-size: 60px;
				width: 60px;
				height: 150px;
				line-height: 150px;
				background: yellow;
				float: left;
				text-decoration: none;
				overflow: hidden;


			}

			.forword {
				position: absolute;
				left: 0px;
				top: 450px;
			}


			.backward {
				position: absolute;
				left: 460px;
				top: 450px;

			}

			.color {
				width: 60px;
				height: 150px;
				background-color: black;

			}

			#tmp {
				position: absolute;
				float: left;
				width: 450px;
				height: 150px;
				top: 450px;
				overflow: hidden;
				left: 60px;
				background: #000;

			}

			#icon_list {
				position: absolute;
				height: 150px;
				width: 800px;
				float: left;
				font-size: 0px;
			}

			#icon_list li {
				width: 100px;
				height: 150px;
				font-size: 30px;
				font-weight: bold;
				text-align: center;
				line-height: 150px;
				display: inline-block;


			}

			#mediuImgContainer {
				position: absolute;

			}

			#mask {
				width: 200px;
				height: 200px;
				background-color: coral;
				position: absolute;
				left: 0px;
				opacity: 0.5;
				display: none;
			}

			#largeImgContainer {
				position: absolute;
				width: 400px;
				height: 400px;
				left: 530px;
				top: 200px;
				overflow: hidden;
				display: none;

			}

			#largeImgContainer img {
				/* 两倍关系 */
				width: 1040px;
				/* 520 */
				height: 900px;
				/* 450 */
				display: none;
			}

			#largeImg {
				position: absolute;
				top: 800px;

			}
		</style>
	</head>
	<body>
		<div id="product_intro">
			<span id="mediuImgContainer">
				<img id="mediuImg" src="../../img/a0.png" alt="图片">
				<span id="mask"></span><!-- 遮挡 -->
			</span>
			<span id="largeImgContainer">
				<img id="loading" src="../../img/load.png" alt="未加载成功时候的显示"> <!-- 显示一个预加载的状态 网不好的状态显示 -->
				<img id="largeImg" alt="显示其中的1/4">
			</span>


			<div id="tmp">
				<ul id="icon_list">
					<li style="background: #2ab;">1</li>
					<li style="background: #bfa;">2</li>
					<li style="background: #acb;">3</li>
					<li style="background: #897;">4</li>
					<li style="background: #345;">5</li>
					<li style="background: #a56;">6</li>
					<li style="background: #189;">7</li>
					<li style="background: #c78;">8</li>
				</ul>
			</div>
			<!-- 点击监听 -->
			<a class="backward" href="#">&#x2192;</a>
			<a class="forword" href="#">&#x2190;</a>

		</div>
		<script>
			moveMiniImg();
			hoverMiniImg();
			bigImg()

			function bigImg() {
				var $mask = $('#mask'); //遮挡需要加显示
				var $mediuImg = $('#mediuImg'); //鼠标移入图片需要控制效黄块的显示
				var $loading = $('#loading');
				var $largeImg = $('#largeImg');
				var $largeImgContainer = $('#largeImgContainer');
				var $mediuImgContainer = $('#mediuImgContainer');
				var $mediuImgContainerHeight = $mediuImgContainer.height();
				var $mediuImgContainerWidth = $mediuImgContainer.width();
				$mediuImgContainer.hover(function() {
						//显示 并得出left top值
						$mask.show();
						//动态加载大图 加载对应大图的src
						var src = $mediuImg.attr('src');
						$largeImgContainer.show();
						//图片的加载完成需要针对图片是否加载成功
						//on机制--加载单一图片
						$largeImg.attr('src', src);
						//只有在图片加载完成后才有意义
						$largeImg.on('load', function() {

							//监听鼠标移动事件
							$mediuImgContainer.mousemove(function(event) {
								//在移动过程中不断调用
								var left = 0;
								var top = 0;
								//给小方块定位
								//鼠标的位置
								left = event.clientX - $mediuImgContainer.offset().left - $mask.width() / 2;
								top = event.clientY - $mediuImgContainer.offset().top - $mask.height() / 2;
								//区间需要有限制 0-$mediuImgContainerWidth-$mask.width()
								//0-0-$mediuImgContainerHeight-$mask.height()
								if (left < 0) {
									left = 0
								} else if (left > $mediuImgContainerWidth - $mask.width()) {
									left = $mediuImgContainerWidth - $mask.width();
								}
								if (top < 0) {
									top = 0
								} else if (top > $mediuImgContainerHeight - $mask.height()) {
									top = $mediuImgContainerHeight - $mask.height();
								}

								$mask.css({
									left: left,
									top: top
								});

								//移动大图片
								//区间需要有限制 0-$mediuImgContainerWidth-$mask.width()   2倍关系
								//0-0-$mediuImgContainerHeight-$mask.height()
								left = left * 2;
								top = top * 2;
								//设置大图的坐标
								if (left < 0) {
									left = 0
								} else if (left > ($mediuImgContainerWidth - $mask.width()) * 2) {
									left = (left > ($mediuImgContainerWidth - $mask.width()) * 2) * 2
								}

								if (top < 0) {
									top = 0
								} else if (top > ($mediuImgContainerHeight - $mask.height()) * 2) {
									top = (top > $mediuImgContainerHeight - $mask.height()) * 2
								}
								
								$largeImg.css({
									left: -left,
									top: -top
								});
								//显示大图
								$largeImg.show();
								//隐藏加载记录条
								$loading.hide();
							});
						});


					},
					function() {
						$mask.hide();
							$largeImgContainer.hide();
						
						

					});


			}

			function hoverMiniImg() {
				$('#icon_list>li').hover(function() {
					//鼠标移入

					$(this).css('color', 'red'); //添加样式
					//显示对应的中图
					var src = "../../img/a" + $(this).index() + ".png";
					$('#mediuImg').attr('src', src)

				}, function() {
					//鼠标移出
					$(this).css('color', 'black'); //添加样式

				});
			}

			function moveMiniImg() {
				var $aa = $('#product_intro>a');
				var $backward = $('.backward');
				var $forword = $('.forword');
				var $ul = $('#icon_list');
				var showPic = 4; //一次性显示4个
				var imgCount = $ul.children().length; //所有图片数量
				var distance = 100;

				//初始化更新
				if (imgCount > showPic) {
					$forword.addClass('color')
				}
				//向右的绑定监听
				var moveCount = 0; //点击向右+1 点击向左 -1 


				$backward.click(function() {
					//判断是否需要移动 不需要直接不行
					if (moveCount === imgCount - showPic) { //4个
						$backward.addClass('color');
						return
					}

					moveCount++;
					//更新右边移动的按钮
					$forword.removeClass('color');
					//更新右边的按钮
					if (moveCount === imgCount - showPic) {
						$backward.addClass('color');
					}
					//移动图片的距离

					$ul.css('left', -distance * moveCount);
				})

				//更新左边移动的按钮
				$forword.click(function() {

					//判断是否需要移动 不需要直接不行
					if (moveCount === 0) { //一上来不能移动 是0
						$forword.addClass('color');
						return
					}

					moveCount--;

					//更新右边移动的按钮
					$backward.removeClass('color');
					//更新左边的按钮
					if (moveCount === 0) {
						$forword.addClass('color');
					}
					//移动图片的距离

					$ul.css('left', -(moveCount) * distance);
				})

			}
		</script>
	</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值