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

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

如何在滾動時到達(dá)不同的 div 元素后更改導(dǎo)航圖像

如何在滾動時到達(dá)不同的 div 元素后更改導(dǎo)航圖像

富國滬深 2023-02-17 15:52:07
我在導(dǎo)航中有一張圖片(我們稱之為 pic1),一旦我到達(dá)頁面內(nèi)容 div 的頂部就需要更改為 pic2,但是當(dāng)我到達(dá)頁腳時它需要改回 pic 1 并重復(fù),所以如果我向上滾動到頁面內(nèi)容,它將再次轉(zhuǎn)到圖片 2,等等。我嘗試做類似下面的事情,但無法讓它工作,我怎樣才能讓它工作?var scrollContent = $("#content").offset().top;var scrollHero = $("#hero").offset().top;var scrollPos = $(document).scrollTop();if (scrollPos > scrollContent) {    $(".image-test").css({        "background-image": "url('')"    });}  else if(scrollPos < scrollContent) {    $(".image-test").css({        "background-image": "url('')"    });
查看完整描述

1 回答

?
慕姐4208626

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

您嘗試使用的 jQyery 代碼存在一些問題:

1.你只是在頁面加載時檢查滾動位置 - 你需要不斷檢查滾動事件:

$(window).on('scroll', function( /* handler function */));

2.您試圖通過 CSS 更改圖像,但圖像未使用 CSS 顯示。相反,您可以像這樣更改元素src的:img

$(".image-test img").attr("src", imgUrl);

3.您沒有檢查頁面內(nèi)容元素的底部(替換圖像將被換回的位置)。你可以這樣得到它:

var contentTop = $(".page-content").offset().top;
var contentBottom = contentTop + $(".page-content").outerHeight(true);

4.您需要檢查滾動是否在這些位置之間:

if ( ($(this).scrollTop() > contentTop) && ($(this).scrollTop() < contentBottom))

為了使其具有響應(yīng)性(例如,如果在頁面加載后通過調(diào)整窗口大小更改屏幕大小,它將起作用),您還需要將其包含在滾動事件處理程序中。

功能的完整代碼


// get the URL of the image so we can use it to swap back

defaultImgUrl =  $(".image-test img").attr("src");


// check the scroll position on the scroll event

$(window).on('scroll', function() {


  // get the top and bottom positions pf the page content

  var contentTop = $(".page-content").offset().top;

  var contentBottom = contentTop + $(".page-content").outerHeight(true);


  // check if the scroll position is within the page content 

  if (($(this).scrollTop() > contentTop) && ($(this).scrollTop() < contentBottom)) {

    // change the image url

    $(".image-test img").attr("src", "https://lorempixel.com/output/nature-q-c-100-50-2.jpg");

  } else {

    $(".image-test img").attr("src", defaultImgUrl);

  }

  

});

工作示例:


// get the URL of the image so we can use it to swap back

defaultImgUrl = $(".image-test img").attr("src");


// check the scroll position on the scroll event

$(window).on('scroll', function() {


  // get the top and bottom positions pf the page content

  var contentTop = $(".page-content").offset().top;

  var contentBottom = contentTop + $(".page-content").outerHeight(true);


  // check if the scroll position is within the page content 

  if (($(this).scrollTop() > contentTop) && ($(this).scrollTop() < contentBottom)) {

    // change the image url

    $(".image-test img").attr("src", "https://lorempixel.com/output/nature-q-c-100-50-2.jpg");

  } else {

    $(".image-test img").attr("src", defaultImgUrl);

  }


});

.section1,

.section2,

.page-content {

  height: 100vh;

}


.section1 {

  background-color: green;

  padding-top: 50px;

}


.section2 {

  background-color: red;

}


nav {

  height: 50px;

  background-color: grey;

  position: fixed;

  width: 100%;

  display: flex;

}


.image-test img {

  width: 100px;

  height: 50px;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<nav>

  <div class="image-test">

    <img src="https://lorempixel.com/output/nature-q-c-100-50-5.jpg" alt="">

  </div>

  <div>

    <p>Change me to a different picture once I reach the top of page content. Then change me back to the same picture as the one I had in the hero once I reach the footer.</p>

  </div>

</nav>

<div class="section1" id="hero">

  <h1>Hero</h1>

</div>

<div class="page-content">

  <h1>Page Content</h1>

</div>

<div class="section2">

  <h1>Footer</h1>

</div>


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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