HTML放大镜 (商品放大镜)

HTML 放大镜(商品放大镜)

HTML部分

<table border="1" align="center" width="490px">
					<tr height="500px">
						<td colspan="4" width="200px" height="200px">
							<img id="tu" src="图片地址" width="100%" onmousemove="fangdajing(event)"
								onmouseout="xiaoshi()">
							<canvas id="mycanvas" width="" height=""></canvas>
						</td>
					</tr>
					<tr hright="125px">
						<td width="50px" height="50px"> <img src="图片地址" alt="" width="100%"height="100%" onmouseenter="xianshi(this)"></td>
						<td width="50px" height="50px"><img src="图片地址" alt="" width="100%"height="100%" onmouseenter="xianshi(this)"></td>
						<td width="50px" height="50px"> <img src="图片地址" alt="" width="100%"height="100%" onmouseenter="xianshi(this)"></td>
						<td width="50px" height="50px"><img src="图片地址" alt="" width="100%"height="100%" onmouseenter="xianshi(this)"></td>
					</tr>
				</table>
				```
				CSS部分
				canvas {
				width: 100px;
				height: 100px;
				border: 2px solid red;
				position: absolute;
				border-radius: 50%;
			}

			table {
				position: absolute;
			}
			
JS部分
<script type="text/javascript">
		//放大镜
		var canvas = document.getElementById("mycanvas");
		var context = canvas.getContext("2d");
		canvas.style.display = "none";

		function xiaoshi() {
			canvas.style.display = "none";
		}

		function fangdajing(e) {
			canvas.style.display = "block";
			canvas.style.left = e.layerX + 10 + "px";
			canvas.style.top = e.layerY + 10 + "px";
			var img = new Image;
			img.src = document.getElementById("tu").src;
			var image = document.getElementById("tu");
			var sw = img.width / image.width;
			var sh = img.height / image.height
			context.clearRect(0, 0, canvas.width, canvas.height);
			context.drawImage(img, e.layerX * sw, e.layerY * sh, 50 * sw, 50 * sh, 0, 0, canvas.width, canvas.height);


		}
		</script>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的JavaScript商品放大镜的实现方法: 1. HTML结构 ```html <div class="product-image"> <img src="product-image.jpg" alt="Product Image"> <div class="zoom-container"></div> </div> ``` 2. CSS样式 ```css .product-image { position: relative; width: 400px; height: 400px; } .product-image img { width: 100%; height: 100%; object-fit: cover; } .zoom-container { position: absolute; top: 0; right: -400px; width: 400px; height: 400px; border: 1px solid #ccc; overflow: hidden; display: none; } .zoom-container img { position: absolute; top: 0; left: 0; width: 800px; height: 800px; } ``` 3. JavaScript代码 ```javascript const productImage = document.querySelector('.product-image'); const zoomContainer = document.querySelector('.zoom-container'); const zoomImage = document.createElement('img'); zoomImage.src = 'product-image.jpg'; zoomContainer.appendChild(zoomImage); productImage.addEventListener('mousemove', function(e) { const x = e.pageX - this.offsetLeft; const y = e.pageY - this.offsetTop; const zoomX = x / this.offsetWidth * zoomImage.offsetWidth - zoomContainer.offsetWidth / 2; const zoomY = y / this.offsetHeight * zoomImage.offsetHeight - zoomContainer.offsetHeight / 2; zoomImage.style.transform = `translate(${-zoomX}px, ${-zoomY}px)`; zoomContainer.style.display = 'block'; }); productImage.addEventListener('mouseleave', function() { zoomContainer.style.display = 'none'; }); ``` 以上代码实现了一个简单的商品放大镜效果,当鼠标在商品图片上移动时,会在旁边显示一个放大的镜头,镜头中显示的是鼠标所在位置的放大图像。当鼠标离开商品图片时,放大镜头会消失。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值