且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据div更改滚动时的字体颜色

更新时间:2022-04-28 02:56:14

您可以使用jQuery来获取滚动条根据深色背景元素的位置来定位和切换类。这是一个例子

You can use jQuery to get the scroll position and toggle the classes based on where the dark background element is. Here is an example

$(document).ready(function(){
    $(window).scroll(function(){
    var light_pos = $('#white_div').offset().top;
    var light_height = $('#white_div').height();
    var menu_pos = $('.NavigationButton').offset().top;
    var scroll = $(window).scrollTop();

    if(menu_pos > light_pos && menu_pos < (light_pos + light_height)) {
        $('.NavigationButton').addClass('menu_black');
      $('.NavigationButton').removeClass('menu_white');
    }
    else {
        $('.NavigationButton').removeClass('menu_black');
      $('.NavigationButton').addClass('menu_white');
    }

  })
})

这里是一个工作小提琴 https://jsfiddle.net/atqkuwhs/

and here is a working fiddle https://jsfiddle.net/atqkuwhs/