最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript函數(shù)封裝的示例詳解

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

<!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">會(huì)員內(nèi)容</div>
    </div>
 
    <script>
        //鼠標(biāo)經(jīng)過box1的時(shí)候,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) //先把原先地定時(shí)器清除之后,再開啟另外一個(gè)新地定時(shí)器
            obj.timer = setInterval(fn, [15])
 
            function fn() {
                var a = obj.offsetLeft //不能換成div.style.left 不然會(huì)只移動(dòng)一次。注意讀取位置永offset,修改永style
                var step = (target - a) / 10
                step = step > 0 ? Math.ceil(step) : Math.floor(step) //將結(jié)果賦值回去
                    //把步長值改為整數(shù),不要出現(xiàn)小數(shù)的情況
                if (a == target) {
 
                    //取消定時(shí)器
                    clearInterval(obj.timer)
                        //執(zhí)行回調(diào)函數(shù) 函數(shù)名+()回調(diào)函數(shù)寫到定時(shí)器結(jié)束里面
                        //首先判斷沒有有這個(gè)回調(diào)函數(shù)
                    if (callback) {
                        callback()
                    }
 
                }
 
                obj.style.left = a + step + 'px'
 
            }
        }
        //鼠標(biāo)離開的時(shí)候,box2就往右邊移140px
    </script>
</body>
 
</html>

這個(gè)下面看著就頭暈

<!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">會(huì)員內(nèi)容</div>
    </div>
 
    <script>
        //鼠標(biāo)經(jīng)過box1的時(shí)候,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ú)寫在一個(gè)獨(dú)立的文件中。

之后直接使用函數(shù)。但在此之前要先引入JS文件

    <script>
        //鼠標(biāo)經(jīng)過box1的時(shí)候,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) //先把原先地定時(shí)器清除之后,再開啟另外一個(gè)新地定時(shí)器
    obj.timer = setInterval(fn, [15])
 
    function fn() {
        var a = obj.offsetLeft //不能換成div.style.left 不然會(huì)只移動(dòng)一次。注意讀取位置永offset,修改永style
        var step = (target - a) / 10
        step = step > 0 ? Math.ceil(step) : Math.floor(step) //將結(jié)果賦值回去
            //把步長值改為整數(shù),不要出現(xiàn)小數(shù)的情況
        if (a == target) {
 
            //取消定時(shí)器
            clearInterval(obj.timer)
                //執(zhí)行回調(diào)函數(shù) 函數(shù)名+()回調(diào)函數(shù)寫到定時(shí)器結(jié)束里面
                //首先判斷沒有有這個(gè)回調(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)不是很多的時(shí)候用iconfont要方便很多。單數(shù)如果圖標(biāo)很多,就用lcoMoon來導(dǎo)入圖標(biāo)?;蛘呤褂镁`圖等來設(shè)置

以上就是JavaScript函數(shù)封裝的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于JavaScript函數(shù)封裝的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

桐乡市| 嘉定区| 湄潭县| 虎林市| 民乐县| 金华市| 义马市| 新晃| 许昌市| 托克逊县| 铁岭市| 郓城县| 璧山县| 彝良县| 香港 | 三门峡市| 格尔木市| 阿城市| 长宁区| 兰州市| 五家渠市| 瓮安县| 辽宁省| 新竹县| 石景山区| 淳化县| 夹江县| 嘉定区| 城口县| 蚌埠市| 乌什县| 洮南市| 原阳县| 刚察县| 西城区| 长汀县| 富民县| 蓝田县| 鸡东县| 商都县| 长岛县|