您好,登錄后才能下訂單哦!
小編給大家分享一下JS如何實(shí)現(xiàn)商品櫥窗特效,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體內(nèi)容如下
知識(shí)點(diǎn)
換算公式
① 滾動(dòng)條長(zhǎng)度 = ( 盒子的寬度 / 內(nèi)容的寬度) * 盒子的寬度
滾動(dòng)條長(zhǎng)度/盒子的長(zhǎng)度 = 盒子的長(zhǎng)度/內(nèi)容的長(zhǎng)度
② 內(nèi)容走的距離 = (內(nèi)容的長(zhǎng)度 - 盒子的長(zhǎng)度)/ (盒子的長(zhǎng)度 - 滾動(dòng)條的長(zhǎng)度)* 滾動(dòng)條走的距離
運(yùn)行效果
代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; list-style: none; border:none; } #box{ width: 800px; height: 200px; border: 1px solid #ddd; position: relative; margin: 100px auto; overflow: hidden; } #box ul{ width: 5200px; position: absolute; left: 0; top: 20px; } #box ul li{ float: left; } #box_bottom{ position: absolute; left: 0; bottom: 0; background-color: #e8e8e8; width: 100%; height: 25px; } .mask{ position: absolute; left: 0; top:0; height: 25px; background-color: orangered; border-radius: 12px; cursor: pointer; } </style> </head> <body> <div id="box"> <ul id="box_top"> <li><img src="images/img1.jpg" alt=""></li> <li><img src="images/img2.jpg" alt=""></li> <li><img src="images/img3.jpg" alt=""></li> <li><img src="images/img4.jpg" alt=""></li> <li><img src="images/img5.jpg" alt=""></li> <li><img src="images/img6.jpg" alt=""></li> <li><img src="images/img7.jpg" alt=""></li> <li><img src="images/img8.jpg" alt=""></li> <li><img src="images/img1.jpg" alt=""></li> <li><img src="images/img2.jpg" alt=""></li> <li><img src="images/img1.jpg" alt=""></li> <li><img src="images/img2.jpg" alt=""></li> <li><img src="images/img3.jpg" alt=""></li> <li><img src="images/img4.jpg" alt=""></li> <li><img src="images/img5.jpg" alt=""></li> <li><img src="images/img6.jpg" alt=""></li> <li><img src="images/img7.jpg" alt=""></li> <li><img src="images/img8.jpg" alt=""></li> <li><img src="images/img1.jpg" alt=""></li> <li><img src="images/img2.jpg" alt=""></li> <li><img src="images/img1.jpg" alt=""></li> <li><img src="images/img2.jpg" alt=""></li> <li><img src="images/img3.jpg" alt=""></li> <li><img src="images/img4.jpg" alt=""></li> <li><img src="images/img5.jpg" alt=""></li> <li><img src="images/img6.jpg" alt=""></li> <li><img src="images/img7.jpg" alt=""></li> <li><img src="images/img8.jpg" alt=""></li> <li><img src="images/img1.jpg" alt=""></li> <li><img src="images/img2.jpg" alt=""></li> <li><img src="images/img1.jpg" alt=""></li> <li><img src="images/img2.jpg" alt=""></li> <li><img src="images/img3.jpg" alt=""></li> <li><img src="images/img4.jpg" alt=""></li> <li><img src="images/img5.jpg" alt=""></li> <li><img src="images/img6.jpg" alt=""></li> <li><img src="images/img7.jpg" alt=""></li> <li><img src="images/img8.jpg" alt=""></li> <li><img src="images/img1.jpg" alt=""></li> <li><img src="images/img2.jpg" alt=""></li> </ul> <div id="box_bottom"> <span class="mask"></span> </div> </div> <script> window.onload = function () { // 1. 獲取需要的標(biāo)簽 var box = document.getElementById("box"); var box_top = document.getElementById("box_top"); var box_bottom = document.getElementById("box_bottom"); var mask = box_bottom.children[0]; // 2. 設(shè)置滾動(dòng)條的長(zhǎng)度 // 滾動(dòng)條長(zhǎng)度 = ( 盒子的寬度 / 內(nèi)容的寬度) * 盒子的寬度 var mask_len = (box.offsetWidth / box_top.offsetWidth) * box.offsetWidth; mask.style.width = mask_len + 'px'; // 3. 鼠標(biāo)操作 mask.onmousedown = function (event) { var e = event || window.event; // 3.1 求出初始值 var beginX = e.clientX - mask.offsetLeft; // 3.2 移動(dòng) document.onmousemove = function (event) { var e = event || window.event; // 3.3 求出移動(dòng)的距離 var endX = event.clientX - beginX; // 邊界值 if(endX < 0){ endX = 0; }else if(endX >= box.offsetWidth - mask.offsetWidth){ endX = box.offsetWidth - mask.offsetWidth; } // 3.4 動(dòng)起來(lái) mask.style.left = endX + 'px'; // 內(nèi)容走的距離 = (內(nèi)容的長(zhǎng)度 - 盒子的長(zhǎng)度)/ (盒子的長(zhǎng)度 - 滾動(dòng)條的長(zhǎng)度)* 滾動(dòng)條走的距離 var content_len = (box_top.offsetWidth - box.offsetWidth) / (box.offsetWidth - mask.offsetWidth) * endX; box_top.style.left = -content_len + 'px'; return false; }; document.onmouseup = function () { document.onmousemove = null; } } } </script> </body> </html>
以上是“JS如何實(shí)現(xiàn)商品櫥窗特效”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。