欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

放大镜效果

程序员文章站 2023-11-08 11:49:34
使用电脑逛淘宝,京东等商城时,将鼠标移入图片中,图片会放大,之前一直在想这种是怎么实现的,前两天刚写出来,纯js实现的,无任何工具库。下面先来看下思路吧! 刚学js的时候可能对于布局不是很重要,但学到面向对象编程后,布局就变得很重要了,有时候布局会影响到整体效果;先来看下布局吧! 布局就搞定了,比较 ......

使用电脑逛淘宝,京东等商城时,将鼠标移入图片中,图片会放大,之前一直在想这种是怎么实现的,前两天刚写出来,纯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>

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