JavaScript函數(shù)封裝的示例詳解
更新時間:2022年03月09日 10:42:34 作者:一夕ξ
這篇文章主要通過動畫的示例來為大家詳細介紹一下JavaScript的函數(shù)封裝,文中的示例代碼講解詳細,感興趣的小伙伴可以學(xué)習一下

<!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>
//鼠標經(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'
}
}
//鼠標離開的時候,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>
//鼠標經(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單獨寫在一個獨立的文件中。
之后直接使用函數(shù)。但在此之前要先引入JS文件

<script>
//鼠標經(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單獨文件:
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%'
}覺得在圖標不是很多的時候用iconfont要方便很多。單數(shù)如果圖標很多,就用lcoMoon來導(dǎo)入圖標?;蛘呤褂镁`圖等來設(shè)置
以上就是JavaScript函數(shù)封裝的示例詳解的詳細內(nèi)容,更多關(guān)于JavaScript函數(shù)封裝的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript錯誤處理之分析 Uncaught(in promise) error的
在開發(fā)過程中,JavaScript的錯誤處理是一個老生常談的話題,當應(yīng)用程序發(fā)生未捕獲的異常時,Uncaught(in promise) error是其中最常見的錯誤類型,這篇文章將從多個方面詳細闡述這種錯誤類型的原因與解決方案,感興趣的朋友一起看看吧2023-12-12
JavaScript獲得當前網(wǎng)頁來源頁面(即上一頁)的方法
這篇文章主要介紹了JavaScript獲得當前網(wǎng)頁來源頁面(即上一頁)的方法,涉及javascript中document.referrer方法的使用技巧,需要的朋友可以參考下2015-04-04

