其他分享
首页 > 其他分享> > 放大镜效果

放大镜效果

作者:互联网

使用电脑逛淘宝,京东等商城时,将鼠标移入图片中,图片会放大,之前一直在想这种是怎么实现的,前两天刚写出来,纯js实现的,无任何工具库。下面先来看下思路吧!

刚学js的时候可能对于布局不是很重要,但学到面向对象编程后,布局就变得很重要了,有时候布局会影响到整体效果;先来看下布局吧!

  1     <div class="photo">
  2         <div class="sbox">
  3             <img src="http://img0.imgtn.bdimg.com/it/u=4175582523,2707192513&fm=15&gp=0.jpg">
  4             <span></span>
  5         </div>
  6         <div class="bbox">
  7             <img src="http://img0.imgtn.bdimg.com/it/u=4175582523,2707192513&fm=15&gp=0.jpg">
  8         </div>
  9         <div class="nav">
 10             <span> &lt;</span>
 11             <img src="http://img0.imgtn.bdimg.com/it/u=4175582523,2707192513&fm=15&gp=0.jpg" class="selected">
 12             <img src="http://img4.imgtn.bdimg.com/it/u=181188734,374783636&fm=15&gp=0.jpg">
 13             <img src="http://img2.imgtn.bdimg.com/it/u=2136674516,3472494802&fm=15&gp=0.jpg">
 14             <img src="http://img0.imgtn.bdimg.com/it/u=3344949169,188332301&fm=15&gp=0.jpg">
 15             <span>></span>
 16         </div>
 17     </div>
 18 
 19 
 20     <style>
 21         .photo {
 22             width: 98%;
 23             height: 500px;
 24             margin: 20px auto;
 25             border: 1px solid black;
 26             position: relative;
 27         }
 28 
 29         .sbox {
 30             width: 600px;
 31             height: 300px;
 32             position: absolute;
 33             top: 20px;
 34             left: 20px;
 35         }
 36 
 37         .sbox img {
 38             width: 600px;
 39             height: 300px;
 40         }
 41 
 42         .sbox span {
 43             position: absolute;
 44             background-color: rgba(199, 199, 199, .5);
 45             top: 0;
 46             display: none;
 47             left: 0;
 48         }
 49 
 50         .bbox {
 51             width: 600px;
 52             height: 300px;
 53             overflow: hidden;
 54             position: absolute;
 55             top: 20px;
 56             left: 630px;
 57             display: none;
 58         }
 59 
 60         .bbox img {
 61             width: 1200px;
 62             height: 600px;
 63             position: absolute;
 64             top: 0;
 65             left: 0;
 66         }
 67 
 68         .nav {
 69             width: 300px;
 70             height: 32px;
 71             position: absolute;
 72             overflow: hidden;
 73             bottom: 20px;
 74             left: 20px;
 75 
 76         }
 77 
 78         .nav img {
 79             width: 60px;
 80             height: 30px;
 81             border: 1px solid #dddddd;
 82         }
 83 
 84         .nav img:first-of-type {
 85             margin-left: 20px;
 86         }
 87 
 88         .nav span {
 89             position: absolute;
 90             width: 16px;
 91             height: 32px;
 92             text-align: center;
 93             line-height: 30px;
 94             background-color: #cccccc;
 95             top: 0;
 96             cursor: pointer;
 97         }
 98 
 99         .nav span:last-child {
100             right: 0;
101         }
102 
103         .nav .selected {
104             border: 1px solid #e48c63;
105         }
106     </style>

布局就搞定了,比较简单的哈。下面就是一个思路吧

  1. 选择元素
  2. 鼠标进入
    1. 显示span和大图
    2. 计算span的宽高
  3. 鼠标移出
    1. 隐藏span和大图
  4. 鼠标移动
    1. span跟随鼠标移动
    2. span的边界限定
    3. 计算比例
    4. 大图跟随小图移动

大体思路就是这样的,个人建议是根据思路直接自己写,实在不会可以参照我的代码哈!因为自己写出来的和看了别人代码再写出来的感觉不太一样的。好啦,下面就是js部分的代码!

 1 <script>
 2     // 创建函数
 3     function Big() {
 4         // 获取元素
 5         this.span = document.querySelector(".sbox span");
 6         this.sbox = document.querySelector(".sbox");
 7         this.bbox = document.querySelector(".bbox");
 8         this.simg = document.querySelector(".sbox img");
 9         this.bimg = document.querySelector(".bbox img");
10         this.img = document.querySelectorAll(".nav img");
11         this.init();
12     }
13     Big.prototype.init = function () {
14         // this的指向问题
15         var that = this;
16         for (var i = 0; i < this.img.length; i++) {
17             this.img[i].index = i;
18         // 点击导航图
19             this.img[i].onclick = function () {
20                 for (var j = 0; j < that.img.length; j++) {
21                     // 清除默认样式
22                     that.img[j].className = "";
23                 }
24                 // 给当前的图片加样式
25                 that.img[this.index].className = "selected";
26                 that.index = this.index;
27                 // 将当前图放入主体框中
28                 that.changeImg();
29             }
30         }
31         // 鼠标移入事件
32         this.sbox.onmouseover = function () {
33             // 显示
34             that.span.style.display = "block";
35             that.bbox.style.display = "block";
36             // 计算span宽高
37             that.span.style.width = that.bbox.offsetWidth / that.bimg.offsetWidth * that.sbox.offsetWidth + "px";
38             that.span.style.height = that.bbox.offsetHeight / that.bimg.offsetHeight * that.sbox.offsetHeight + "px";
39         }
40         // 鼠标移动
41         this.sbox.onmousemove = function () {
42             that.move();
43         }
44         // 鼠标移出
45         this.sbox.onmouseout = function () {
46             // 隐藏
47             that.span.style.display = "none";
48             that.bbox.style.display = "none";
49         }
50     }
51     // 改变图片
52     Big.prototype.changeImg = function () {
53         this.simg.src = this.img[this.index].src;
54         this.bimg.src = this.img[this.index].src;
55     }
56     // 移动
57     Big.prototype.move = function () {
58         this.span.style.left = event.clientX - this.sbox.offsetLeft - this.span.offsetWidth / 2 + "px";
59         this.span.style.top = event.clientY - this.sbox.offsetTop - this.span.offsetHeight / 2 + "px";
60         // 边界限定
61         if (this.span.offsetLeft < 0) {
62             this.span.style.left = 0;
63         }
64         if (this.span.offsetTop < 0) {
65             this.span.style.top = 0;
66         }
67         if (this.span.offsetLeft > this.sbox.offsetWidth - this.span.offsetWidth) {
68             this.span.style.left = this.sbox.offsetWidth - this.span.offsetWidth + "px";
69         }
70         if (this.span.offsetTop > this.sbox.offsetHeight - this.span.offsetHeight) {
71             this.span.style.top = this.sbox.offsetHeight - this.span.offsetHeight + "px";
72         }
73         // 计算比例
74         var num=this.bbox.offsetWidth/this.bimg.offsetWidth;
75         console.log(num);
76         // 大图移动
77         this.bimg.style.left=-(this.span.offsetLeft/num)+"px";
78         this.bimg.style.top=-(this.span.offsetTop/num)+"px";
79     }
80     new Big();
81 </script>

仅供参考,如有疑问,欢迎评论哈!

标签:style,span,sbox,img,放大镜,效果,height,bbox
来源: https://www.cnblogs.com/ruo-shui-yi-fang/p/11478050.html