亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2022年03月09日 10:42:34   作者:一夕ξ  
這篇文章主要通過動畫的示例來為大家詳細(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">會員內(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)文章

  • js正則表達(dá)exec與match的區(qū)別說明

    js正則表達(dá)exec與match的區(qū)別說明

    本篇文章主要是對js正則表達(dá)exec與match的區(qū)別進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • JavaScript錯誤處理之分析 Uncaught(in promise) error的原因及解決方案

    JavaScript錯誤處理之分析 Uncaught(in promise) error的

    在開發(fā)過程中,JavaScript的錯誤處理是一個老生常談的話題,當(dāng)應(yīng)用程序發(fā)生未捕獲的異常時,Uncaught(in promise) error是其中最常見的錯誤類型,這篇文章將從多個方面詳細(xì)闡述這種錯誤類型的原因與解決方案,感興趣的朋友一起看看吧
    2023-12-12
  • JavaScript編程中容易出BUG的幾點(diǎn)小知識

    JavaScript編程中容易出BUG的幾點(diǎn)小知識

    這篇文章主要介紹了JavaScript編程中容易出BUG的幾點(diǎn)小知識,本文總結(jié)了8條小知識,這些小知識如果弄不明白,會在編程中給你惹麻煩出BUG,需要的朋友可以參考下
    2015-01-01
  • javascript定義函數(shù)的方法

    javascript定義函數(shù)的方法

    javscript中定義和聲明函數(shù)有三種方式:正常方法 構(gòu)造函數(shù) 函數(shù)直接量。
    2010-12-12
  • 小程序?qū)崿F(xiàn)日歷打卡功能

    小程序?qū)崿F(xiàn)日歷打卡功能

    這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)日歷打卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • js獲取微信版本號的方法

    js獲取微信版本號的方法

    本文給大家分享的是使用javascript實(shí)現(xiàn)判斷微信版本號以及判斷是否在內(nèi)置的微信瀏覽器中打開的代碼,十分的簡單實(shí)用,有需要的小伙伴可以參考下。
    2015-05-05
  • 微信小程序定位當(dāng)前城市的方法

    微信小程序定位當(dāng)前城市的方法

    這篇文章主要為大家詳細(xì)介紹了微信小程序定位當(dāng)前城市的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • js實(shí)現(xiàn)淘寶首頁的banner欄效果

    js實(shí)現(xiàn)淘寶首頁的banner欄效果

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)淘寶首頁的banner欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Bootstrap菜單按鈕及導(dǎo)航實(shí)例解析

    Bootstrap菜單按鈕及導(dǎo)航實(shí)例解析

    這篇文章主要介紹了Bootstrap菜單按鈕及導(dǎo)航的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧
    2016-09-09
  • JavaScript獲得當(dāng)前網(wǎng)頁來源頁面(即上一頁)的方法

    JavaScript獲得當(dāng)前網(wǎng)頁來源頁面(即上一頁)的方法

    這篇文章主要介紹了JavaScript獲得當(dāng)前網(wǎng)頁來源頁面(即上一頁)的方法,涉及javascript中document.referrer方法的使用技巧,需要的朋友可以參考下
    2015-04-04

最新評論