JS實(shí)現(xiàn)頁面滾動(dòng)

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .mi{
            width: 512px;
            height: 400px;
            margin:100px auto;
            overflow: hidden;
            position: relative;
        }
        .mi span{
            position: absolute;
            left:0;
            width:100%;
            height: 50%;
            cursor: pointer;
        }
        #picUp{
            top:0;
        }
       #picDown{
            bottom:0
        }
        #pic{
            position: absolute;
            top:0;
            left:0;
        }
    </style>
</head>
<body>
<div class="mi">
   <img src = "images/mi.png">
    <span id="picUp"></span>
    <span id="picDown"></span>
</div>
<script>
        function $(id) {
            return document.getElementById(id);
        }
        var timer = null; // 定義定時(shí)器
        var count = 0;  // 定義top值
        
        //  鼠標(biāo)懸浮上半部分時(shí),圖片向上滾動(dòng)
        $("picUp").onmouseover = function () {
            clearInterval(timer);  // 清除定時(shí)器
            timer = setInterval(function () {  // 開啟定時(shí)器
                count -= 3;
                if(count >= -1070){ 
                    $("pic").style.top = count + "px";
                }else{
                        clearInterval(timer);
                }
            },30)
        }

        //  鼠標(biāo)懸浮下半部分時(shí),圖片向下滾動(dòng)
        $("picDown").onmouseover = function () {
            clearInterval(timer);
            timer = setInterval(function () {
                count ++;
                if(count <= 0){
                    $("pic").style.top = count + "px";
                }else{
                    clearInterval(timer);
                }
            },20)
        }

        // 當(dāng)鼠標(biāo)移出時(shí),停止定時(shí)器

        $("picUp").parentNode.onmouseout = function () {
            clearInterval(timer);
        }

</script>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容