亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

向下滾動網站后顯示 div

向下滾動網站后顯示 div

白衣染霜花 2023-06-29 21:02:10
我想在向下滾動頁面時設置元素的動畫。我只想使用 JavaScript 來實現這一點。我希望它像這樣工作:沒有動畫,但是當您向下滾動到第二個容器時,JS 設置動畫并顯示容器內的內容。問題是,當我加載頁面時,沒有動畫(這很好)。當我向下滾動時,仍然沒有動畫(這很糟糕!),但是當我在網站底部使用 F5 刷新頁面時,動畫會顯示,但仍然沒有顯示帶有藍色背景的元素。這是我這部分的完整代碼:   <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>    * {        margin: 0;        padding: 0;    }    .main {        display: block;        margin: auto;        height: 1000px;        width: 100%;        background-color: grey;    }    .main-inner1 {        display: block;        margin: auto;        height: 600px;        width: 100%;        background-color: orange;    }    .main-inner2 {        display: block;        margin: auto;        height: 600px;        width: 100%;        background-color: green;    }    .main-inner2-container {        display: block;        position: relative;        height: 50px;        width: 200px;        overflow: hidden;        margin: auto;    }    .main-inner2-content1 {        display: block;        position: absolute;        top: 100%;        margin: auto;        height: 50px;        width: 200px;        background-color: blue;    }    @keyframes FadeIn {        { from: top: 100%; }        { to: top: 0; }    }   </style><script>        document.addEventListener("DOMContentLoaded", function(){        var y = window.scrollY;        var x = document.querySelector(".main-inner2-container").getBoundingClientRect().top;        if (y >= x) {            document.querySelector(".main-inner2-content1").style.animation = "FadeIn 1s linear 0s 1 forwards";        }     });</script><body><div class="main">    <div class="main-inner1"></div>    <div class="main-inner2">        <div class="main-inner2-container">            <div class="main-inner2-content1">                        </div>        </div>    </div></div>我正在學習 JS,所以不使用任何庫對我來說很重要:P
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

我稍微修改了你的代碼。您正在偵聽 DOMContentLoaded 事件,該事件僅觸發一次(在 DOM 完全加載之后),而不是窗口滾動事件。


window.onscroll = function() {

        var y = window.scrollY;


        var x = document.querySelector(".main-inner2-container").getBoundingClientRect().top;

        

        if (y >= x) {

            document.querySelector(".main-inner2-content1").style.animation = "FadeIn 1s linear 0s 1 forwards";

        } 

    }

* {

        margin: 0;

        padding: 0;

    }


    .main {

        display: block;

        margin: auto;

        height: 1000px;

        width: 100%;

        background-color: grey;

    }


    .main-inner1 {

        display: block;

        margin: auto;

        height: 600px;

        width: 100%;

        background-color: orange;

    }


    .main-inner2 {

        display: block;

        margin: auto;

        height: 600px;

        width: 100%;

        background-color: green;

    }


    .main-inner2-container {

        display: block;

        position: relative;

        height: 50px;

        width: 200px;

        overflow: hidden;

        margin: auto;

    }


    .main-inner2-content1 {

        display: block;

        position: absolute;

        top: 100%;

        margin: auto;

        height: 50px;

        width: 200px;

        background-color: blue;

    }


    @keyframes FadeIn {

        from { top: 100%; } 

        to {top: 0; }

    }

<div class="main">

    <div class="main-inner1"></div>

    <div class="main-inner2">

        <div class="main-inner2-container">

            <div class="main-inner2-content1">

            

            </div>

        </div>

    </div>

</div>


此外,您定義關鍵幀的語法不正確。這是


@keyframes FadeIn {

    from { top: 100%; } 

    to {top: 0; }

}

并不是


@keyframes FadeIn {

    { from: top: 100%; }

    { to: top: 0; }

}


查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號