第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

position/getboundingclientrect 中的部分

position/getboundingclientrect 中的部分

吃雞游戲 2023-05-25 15:35:05
我正在嘗試獲取部分在視口中的位置并向它們添加活動類,但代碼不起作用。任何人都可以幫忙嗎?// Get the position of the sections in viewportfunction isInViewport(element) {    const rect = element.getBoundingClientRect();    return (      rect.top >= 0 &&      rect.left >= 0 &&      rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&      rect.right <= (window.innerWidth || document.documentElement.clientWidth)    );  }//add active class to sections in viewport function setActiveClass(){    for (let i=0; i <= sectionsArr.length; i++){        if (isInViewport(sections[i])){            sections[i].classList.add("your-active-class");        }else{            sections[i].classList.remove("your-active-class");        }    }}
查看完整描述

1 回答

?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個贊

let sections = document.querySelectorAll("section");

let sectionsArr = Array.from(sections);



function isInViewport(element) {

    const rect = element.getBoundingClientRect();

    return (

      rect.top >= 0 &&

      rect.left >= 0 &&

      rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&

      rect.right <= (window.innerWidth || document.documentElement.clientWidth)

    );

  }




 function setActiveClass(){

    for (let i=0; i < sectionsArr.length; i++){

        if (isInViewport(sectionsArr[i])){

            sectionsArr[i].classList.add("your-active-class");

        }else{

            sectionsArr[i].classList.remove("your-active-class");

        }

    }}

body{

    margin:0;

}


.filler{

    width:100vw;

    height:100vh;

    background:#db9292;

    display:flex;

    align-items:center;

    justify-content:center;

}




.your-active-class{

  background:red;

}

<!DOCTYPE html>

<html>

<head>

   <meta charset="UTF-8">

   <meta name="viewport" content="width=device-width, initial-scale=1.0">

   <title>Document</title>

   <link rel="stylesheet" href="style.css">

</head>

<body>

   <div class="filler">

    <p class="button" onclick="setActiveClass()" >Click me!</div>

   </div>

    <section class="secs">Section1</section>

    <section class="secs">Section2</section>

    <section class="secs">Section3</section>

    <section class="secs">Section4</section>

   <script src="index.js"></script>

</body>

</html>


我猜你應(yīng)該在 for 循環(huán)中將你的“sections[i]”更改為“sectionsArr[i]”并將“i <= sectionsArr.length”更改為僅“i <sectionsArr.length”


function setActiveClass(){

    for (let i=0; i < sectionsArr.length; i++){

        if (isInViewport(sectionsArr[i])){

            sectionsArr[i].classList.add("your-active-class");

        }else{

            sectionsArr[i].classList.remove("your-active-class");

        }

    }

}

查看完整回答
反對 回復(fù) 2023-05-25
  • 1 回答
  • 0 關(guān)注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號