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

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

 更新時間:2022年03月09日 10:42:34   作者:一夕ξ  
這篇文章主要通過動畫的示例來為大家詳細介紹一下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>
        //鼠標經(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來導入圖標?;蛘呤褂镁`圖等來設(shè)置

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

相關(guān)文章

最新評論

商丘市| 秀山| 文登市| 类乌齐县| 勐海县| 长武县| 梁山县| 海城市| 称多县| 平原县| 东乡族自治县| 湘潭县| 青铜峡市| 内乡县| 白玉县| 平利县| 大丰市| 盐边县| 育儿| 淮滨县| 长宁县| 石泉县| 和政县| 苏尼特左旗| 新民市| 荣昌县| 安新县| 阜宁县| 乌恰县| 维西| 霍山县| 天峨县| 彩票| 宣汉县| 满洲里市| 湾仔区| 鄯善县| 息烽县| 宾川县| 喀什市| 共和县|