JavaScript實(shí)現(xiàn)長(zhǎng)圖滾動(dòng)效果
本文實(shí)例為大家分享了JavaScript之長(zhǎng)圖滾動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
長(zhǎng)圖的滾動(dòng)會(huì)涉及定時(shí)器:
我們先來(lái)回顧下定時(shí)器:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>定時(shí)器回顧</title></head><body> <button id='start'>開(kāi)啟</button> <button id='stop'>關(guān)閉</button> <script type='text/javascript'>var start = document.getElementById('start');var stop = document.getElementById('stop');var num = 0,timer = null;start.onclick = function (){ // 使用定時(shí)器的時(shí)候 先清除原有定時(shí)器 再開(kāi)啟定時(shí)器 可以試著將下邊的clearInterval(timer);注釋掉然后多次點(diǎn)擊開(kāi)啟按鈕對(duì)比效果 clearInterval(timer); timer = setInterval(function (){num++;console.log(num); },1000)}stop.onclick = function (){ clearInterval(timer);} </script></body></html>
溫習(xí)完定時(shí)器內(nèi)容后,來(lái)看長(zhǎng)圖滾動(dòng)的代碼:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>長(zhǎng)圖滾動(dòng)效果</title> <style>*{ padding: 0; margin: 0;}body{ background-color: #000; }#box{ width: 658px; height: 400px; border: 1px solid #ff6700; margin: 100px auto; overflow: hidden; position: relative; }#box img{ position: absolute; top: 0; left: 0; }#box span{ position: absolute; width: 100%; height: 50%; left: 0; cursor: pointer;}#box #top{ top: 0; } #box #bottom{ bottom: 0; } </style></head><body> <div id='box'><img src='http://www.leifengta.com.cn/bcjs/img/timer.jpeg' alt=''><span id='top'></span><span id='bottom'></span> </div> <script type='text/javascript'>// 1.獲取事件源var box = document.getElementById(’box’); var pic = document.getElementsByTagName(’img’)[0]; var divTop = document.getElementById(’top’); var divBottom = document.getElementById(’bottom’);// 2.添加事件var num = 0,timer = null;divBottom.onmouseover = function () { // 清除之前的加速效果 clearInterval(timer); // 讓圖片向下滾動(dòng) timer = setInterval(function () { num -= 10; // 這個(gè)-3666是根據(jù)圖片自己調(diào)控的 if(num >= -3666){ pic.style.top = num + ’px’; }else{ clearInterval(timer); } },50); } divTop.onmouseover = function () { clearInterval(timer); // 讓圖片向上滾動(dòng) timer = setInterval(function () { num += 10; if(num <= 0){ pic.style.top = num + ’px’; }else{ clearInterval(timer); } },100); } // 鼠標(biāo)移開(kāi)則停止?jié)L動(dòng) box.onmouseout = function () { clearInterval(timer); } </script></body></html>
這里不放效果圖了,需要可以自己試試(記得找長(zhǎng)圖)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 刪除docker里建立容器的操作方法2. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法3. Intellij IDEA 關(guān)閉和開(kāi)啟自動(dòng)更新的提示?4. 詳解PHP laravel中的加密與解密函數(shù)5. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法6. IntelliJ IDEA配置Tomcat服務(wù)器的方法7. docker /var/lib/docker/aufs/mnt 目錄清理方法8. 如何用JS實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)監(jiān)聽(tīng)9. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法10. IntelliJ IDEA刪除類(lèi)的方法步驟

網(wǎng)公網(wǎng)安備