使用電腦逛淘寶,京東等商城時,將滑鼠移入圖片中,圖片會放大,之前一直在想這種是怎麼實現的,前兩天剛寫出來,純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> <</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>
佈局就搞定了,比較簡單的哈。下麵就是一個思路吧
- 選擇元素
- 滑鼠進入
- 顯示span和大圖
- 計算span的寬高
- 滑鼠移出
- 隱藏span和大圖
- 滑鼠移動
- span跟隨滑鼠移動
- span的邊界限定
- 計算比例
- 大圖跟隨小圖移動
大體思路就是這樣的,個人建議是根據思路直接自己寫,實在不會可以參照我的代碼哈!因為自己寫出來的和看了別人代碼再寫出來的感覺不太一樣的。好啦,下麵就是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>
僅供參考,如有疑問,歡迎評論哈!