JavaScript函數(shù)封裝的示例詳解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box1 { width: 30px; height: 30px; background-color: pink; position: absolute; top: 100px; right: 0px; z-index: 1; } .box2 { width: 140px; height: 30px; background-color: purple; position: absolute; top: 100px; right: -140px; } .box { width: 400px; height: 1000px; border: 1px solid grey; position: relative; overflow: hidden; } </style> </head> <body> <div class="box"> <div class="box1">^</div> <div class="box2">會員內(nèi)容</div> </div> <script> //鼠標(biāo)經(jīng)過box1的時候,box2就往左邊移140px; var box1 = document.querySelector('.box1') var box2 = document.querySelector('.box2') var a = box2.offsetLeft box1.addEventListener('mouseover', function() { animate(box2, a - 140) }) box1.addEventListener('mouseout', function() { animate(box2, a + 140) }) function animate(obj, target, callback) { clearInterval(obj.timer) //先把原先地定時器清除之后,再開啟另外一個新地定時器 obj.timer = setInterval(fn, [15]) function fn() { var a = obj.offsetLeft //不能換成div.style.left 不然會只移動一次。注意讀取位置永offset,修改永style var step = (target - a) / 10 step = step > 0 ? Math.ceil(step) : Math.floor(step) //將結(jié)果賦值回去 //把步長值改為整數(shù),不要出現(xiàn)小數(shù)的情況 if (a == target) { //取消定時器 clearInterval(obj.timer) //執(zhí)行回調(diào)函數(shù) 函數(shù)名+()回調(diào)函數(shù)寫到定時器結(jié)束里面 //首先判斷沒有有這個回調(diào)函數(shù) if (callback) { callback() } } obj.style.left = a + step + 'px' } } //鼠標(biāo)離開的時候,box2就往右邊移140px </script> </body> </html>
這個下面看著就頭暈
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box1 { width: 30px; height: 30px; background-color: pink; position: absolute; top: 100px; right: 0px; z-index: 1; } .box2 { width: 140px; height: 30px; background-color: purple; position: absolute; top: 100px; right: -140px; } .box { width: 400px; height: 1000px; border: 1px solid grey; position: relative; overflow: hidden; } </style> <script src="animater.js"></script> </head> <body> <div class="box"> <div class="box1">^</div> <div class="box2">會員內(nèi)容</div> </div> <script> //鼠標(biāo)經(jīng)過box1的時候,box2就往左邊移140px; var box1 = document.querySelector('.box1') var box2 = document.querySelector('.box2') var a = box2.offsetLeft box1.addEventListener('mouseover', function() { animate(box2, a - 110) }) box1.addEventListener('mouseout', function() { animate(box2, a + 110) }) </script> </body> </html>
先將js單獨(dú)寫在一個獨(dú)立的文件中。
之后直接使用函數(shù)。但在此之前要先引入JS文件
<script> //鼠標(biāo)經(jīng)過box1的時候,box2就往左邊移140px; var box1 = document.querySelector('.box1') var box2 = document.querySelector('.box2') var img = document.querySelector('img') var a = box2.offsetLeft box1.addEventListener('mouseover', function() { animate(box2, a - 110, callback) }) box1.addEventListener('mouseout', function() { animate(box2, a + 110, callback1) }) </script>
JS單獨(dú)文件:
function animate(obj, target, callback) { clearInterval(obj.timer) //先把原先地定時器清除之后,再開啟另外一個新地定時器 obj.timer = setInterval(fn, [15]) function fn() { var a = obj.offsetLeft //不能換成div.style.left 不然會只移動一次。注意讀取位置永offset,修改永style var step = (target - a) / 10 step = step > 0 ? Math.ceil(step) : Math.floor(step) //將結(jié)果賦值回去 //把步長值改為整數(shù),不要出現(xiàn)小數(shù)的情況 if (a == target) { //取消定時器 clearInterval(obj.timer) //執(zhí)行回調(diào)函數(shù) 函數(shù)名+()回調(diào)函數(shù)寫到定時器結(jié)束里面 //首先判斷沒有有這個回調(diào)函數(shù) if (callback) { callback() } } obj.style.left = a + step + 'px' } } function callback() { img.src = '10-右.png' img.style.width = '50%' } function callback1() { img.src = '9-左.png' img.style.width = '50%' }
覺得在圖標(biāo)不是很多的時候用iconfont要方便很多。單數(shù)如果圖標(biāo)很多,就用lcoMoon來導(dǎo)入圖標(biāo)。或者使用精靈圖等來設(shè)置
以上就是JavaScript函數(shù)封裝的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于JavaScript函數(shù)封裝的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript錯誤處理之分析 Uncaught(in promise) error的
在開發(fā)過程中,JavaScript的錯誤處理是一個老生常談的話題,當(dāng)應(yīng)用程序發(fā)生未捕獲的異常時,Uncaught(in promise) error是其中最常見的錯誤類型,這篇文章將從多個方面詳細(xì)闡述這種錯誤類型的原因與解決方案,感興趣的朋友一起看看吧2023-12-12JavaScript編程中容易出BUG的幾點(diǎn)小知識
這篇文章主要介紹了JavaScript編程中容易出BUG的幾點(diǎn)小知識,本文總結(jié)了8條小知識,這些小知識如果弄不明白,會在編程中給你惹麻煩出BUG,需要的朋友可以參考下2015-01-01Bootstrap菜單按鈕及導(dǎo)航實(shí)例解析
這篇文章主要介紹了Bootstrap菜單按鈕及導(dǎo)航的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-09-09JavaScript獲得當(dāng)前網(wǎng)頁來源頁面(即上一頁)的方法
這篇文章主要介紹了JavaScript獲得當(dāng)前網(wǎng)頁來源頁面(即上一頁)的方法,涉及javascript中document.referrer方法的使用技巧,需要的朋友可以參考下2015-04-04