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

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

如何根據(jù) div offsetHeight 更改標(biāo)題的背景顏色?

如何根據(jù) div offsetHeight 更改標(biāo)題的背景顏色?

喵喵時光機(jī) 2021-08-20 18:26:55
當(dāng)我通過原始 div 以外的 div 時,我試圖更改標(biāo)題的顏色,但我沒有得到它。如何向 window.addEventListener 添加另一個函數(shù)?是這樣嗎?爪哇腳本var height = document.getElementById('section-2').offsetHeight;function logoChanger() {  if(this.scrollY > height) {    document.getElementById('header').classList.add('white');  } else {    document.getElementById('header').classList.remove('white');  }}window.addEventListener('scroll', logoChanger);
查看完整描述

1 回答

?
30秒到達(dá)戰(zhàn)場

TA貢獻(xiàn)1828條經(jīng)驗 獲得超6個贊

你不應(yīng)該使用offsetHeight,offsetTop而是使用。在您的示例中Section-1并且Section-2具有相似的高度,因此您不會面對任何東西..如果您的Section-2高度大于/小于Section-1它,它將無法按預(yù)期工作......

在下面的兩個示例中,我將#Section-2元素的高度減少到500px而不是1000px.

看到這個或看到下面的片段:

var height = document.getElementById('section-2').offsetHeight;


function logoChanger() {

  if(this.scrollY > height) {

    document.getElementById('header').classList.add('white');

  } else {

    document.getElementById('header').classList.remove('white');

  }

}


window.addEventListener('scroll', logoChanger);

* {

  box-sizing: border-box;

  margin: 0;

  padding: 0;

}


#header {

  background: #ccc;

  padding: 24px;

  position: fixed;

  width: 100%;

  

}


  #header.white {

    background: tomato;

  }

#section-1 {

  background: #fff;

  height: 1000px;

}


#section-2 {

  background: #000;

  height: 500px;

}


#section-3 {

  background: yellow;

  height: 1000px;

}

<header id="header">

  <h1>Logo</h1>

  <p>(Section-2 height=500px and checking with <strong>offsetHeight</strong>)</p>

</header>


<div id="section-1">

  Section 1

</div>


<div id="section-2">

  Section 2

</div>


<div id="section-3">

  Section 3

</div>

你應(yīng)該使用offsetTop來使條件..

看到這個或看到下面的片段:

var height = document.getElementById('section-2').offsetTop;


function logoChanger() {

  if(this.scrollY > height) {

    document.getElementById('header').classList.add('white');

  } else {

    document.getElementById('header').classList.remove('white');

  }

}


window.addEventListener('scroll', logoChanger);

* {

  box-sizing: border-box;

  margin: 0;

  padding: 0;

}


#header {

  background: #ccc;

  padding: 24px;

  position: fixed;

  width: 100%;

  

}


#header.white {

    background: tomato;

  }

#section-1 {

  background: #fff;

  height: 1000px;

}


#section-2 {

  background: #000;

  height: 500px;

}


#section-3 {

  background: yellow;

  height: 1000px;

}

<header id="header">

  <h1>Logo</h1>

  <p>(Section-2 height=500px and checking with <strong>offsetTop</strong>)</p>

</header>


<div id="section-1">

  Section 1

</div>


<div id="section-2">

  Section 2

</div>


<div id="section-3">

  Section 3

</div>


查看完整回答
反對 回復(fù) 2021-08-20
  • 1 回答
  • 0 關(guān)注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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