放大镜实例

33 篇文章 1 订阅
23 篇文章 1 订阅
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>放大镜案例</title>
		<style type="text/css">
			*{   padding: 0;
			margin: 0;
				}
			#box{
				width: 430px;
				height: 430px;
				border: 1px  solid  #DDDDDD;
				position: relative;
				 margin: 50px;
			}
			#small_box{
				width: 430px;
				height: 430px;
				position: relative;
			}
			#small_box  #mask{
				position: absolute;
				width: 210px;
				height: 210px;
				background: url(images/zzc.png)  repeat;
				top: 0;
				left: 0;
				display: none;
				 
			}
			#big_box{
				position: absolute;
				left: 440px;
				top: 0;
				width: 430px;
				height: 430px;
		        border: 1px solid  #ddd;
				overflow: hidden;
				 display: none;
			}
			#big_box  img{
				position: absolute;
				z-index: 5;
			}
		</style>
	</head>
	<body>
		   <div id="box">
			    <div id="small_box" >
			    	   <img src="images/shuijiao.jpg" >
					    <span id="mask">
					    	
					    </span>
			    </div>
				<div id="big_box">
					<img src="images/shuijiao.jpg" >
				</div>
		   </div>
		   <script type="text/javascript">
			   
			   window.onload=function(){
				   //1.获取需要的标签
				   var  box=document.getElementById("box");
				   var  small_box=box.children[0];
				   var  big_box=box.children[1];
				   var  small_img=small_box.children[0];
				   var  mask=small_box.children[1];
				   var  big_img=big_box.children[0];
				   
				   //2. 监听鼠标移入
				   
				   small_box.onmouseover=function(){
					   //2.1让遮罩层和大盒子显示出来
					   
					   mask.style.display="block";
					   big_box.style.display="block";
					   
					   small_box.onmousemove=function(e){
						   e=e||window.event;
						   
						   //2.3 求出小盒子移动的水平和垂直的距离
						   var    moveX=e.clientX-small_box.offsetLeft-box.offsetLeft-mask.offsetWidth*0.5;
						   var    moveY=e.clientY-small_box.offsetTop-box.offsetTop-mask.offsetHeight*0.5; 
					       // 2.4边界处理
						   if(moveX<0){
							   moveX=0;
							   
						   }else  if(moveX>=small_box.offsetWidth-mask.offsetWidth){
							   moveX=small_box.offsetWidth-mask.offsetWidth;
						   }
						   
						   if(moveY<0){
						   							   moveY=0;
						   							   
						   }else  if(moveY>=small_box.offsetHeight-mask.offsetHeight){
						   							   moveY=small_box.offsetHeight-mask.offsetHeight;
						   }
						   //让小盒子移动起来
						   //公式:  moveX/大图移动的距离???=(small_box宽度-mask宽度)/(big_img 宽度-big_small宽度)
						   mask.style.left=moveX+'px';
						   mask.style.top=moveY+'px';
						   
						   var   x=moveX/(small_box.offsetWidth-mask.offsetWidth);
						   var   y=moveY/(small_box.offsetHeight-mask.offsetHeight);
						   
						   big_img.style.left=-x*(big_img.offsetWidth-big_box.offsetWidth)+"px";
						   big_img.style.top=-y*(big_img.offsetHeight-big_box.offsetHeight)+"px";
					   }
				       small_box.onmouseout=function(){
						   mask.style.display='none';
						   big_box.style.display='none';
					   }
				   }
			   }
			   
		   </script>
	</body>
</html>

效果:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue2实现放大镜效果通常会结合HTML5的canvas元素以及CSS样式来完成。以下是一个简单的步骤: 1. **创建元素**: - 在HTML中添加一个div作为放大镜容器,包含一个小的图片(目标图像)和一个大一些的canvas元素(用于绘制放大的图像)。 ```html <div class="zoom-container"> <img :src="targetImage" alt="Target Image"> <canvas ref="canvas"></canvas> </div> ``` 2. **JavaScript部分**: - 使用Vue实例的数据属性存储目标图像,并监听鼠标移动事件。 - 当鼠标靠近图片时,计算放大后的坐标并更新canvas的内容。 ```javascript export default { data() { return { targetImage: 'path/to/your/image.jpg', canvasWidth: 0, canvasHeight: 0, isZooming: false, zoomLevel: 1, // 初始放大级别 }; }, mounted() { this.canvas = this.$refs.canvas; this.canvasWidth = this.canvas.width; this.canvasHeight = this.canvas.height; }, methods: { onMouseMove(event) { if (this.isZooming) { const mousePos = { x: event.clientX, y: event.clientY }; const imgPos = this.getImgPosFromMouse(mousePos); const scaledMousePos = this.mapToCanvas(imgPos); // 更新canvas内容 this.drawImagescaled(scaledMousePos); } }, ... // 其他方法如 drawImagescaled, getImgPosFromMouse, mapToCanvas 等 }, }; ``` 3. **辅助函数**: - `getImgPosFromMouse` 和 `mapToCanvas` 分别用于计算鼠标位置相对于图像的位置以及将该位置映射到canvas上。 4. **CSS样式**: - 你可以设置放大镜容器和canvas的初始样式,以及当鼠标悬浮或按住时的视觉变化。 ```css .zoom-container { position: relative; } .zoom-container img { position: absolute; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值