最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change font color on scroll based on a div - Stack Overflow

programmeradmin1浏览0评论

I am currently coding a simple MENU button that is fixed in the top right of the screen.

With the text it is normally Black, but I want to be able to change the text to White when it is within a certain Div on a page so it is still visible on the dark background images.

I had set up two .CSS classes and tried to get them to switch on scroll but I cannot figure it out.

Anyone know how I can achieve this result?

HTML

  <div class="NavigationButton menu_white">
    MENU
  </div>

CSS

.NavigationButton {
 position: fixed;
 top: 5%;
 right: 5%;
 z-index: 99999;
 font-family: neuzeit-grotesk, sans-serif;
 font-weight: 700;
 color: inherit;
}

.menu_white {
 color: #fff;
}

.menu_black {
 color: #000;
}

(Not My Site) Example site:

Just without the snap scroll

Thanks

I am currently coding a simple MENU button that is fixed in the top right of the screen.

With the text it is normally Black, but I want to be able to change the text to White when it is within a certain Div on a page so it is still visible on the dark background images.

I had set up two .CSS classes and tried to get them to switch on scroll but I cannot figure it out.

Anyone know how I can achieve this result?

HTML

  <div class="NavigationButton menu_white">
    MENU
  </div>

CSS

.NavigationButton {
 position: fixed;
 top: 5%;
 right: 5%;
 z-index: 99999;
 font-family: neuzeit-grotesk, sans-serif;
 font-weight: 700;
 color: inherit;
}

.menu_white {
 color: #fff;
}

.menu_black {
 color: #000;
}

(Not My Site) Example site: http://flavinsky./home/amaio

Just without the snap scroll

Thanks

Share Improve this question edited Jul 20, 2017 at 16:34 MAGiK ARtS asked Jul 20, 2017 at 16:22 MAGiK ARtSMAGiK ARtS 351 gold badge2 silver badges8 bronze badges 3
  • 1 Show us your code. – Etherealm Commented Jul 20, 2017 at 16:24
  • Looks like its working? – lky Commented Jul 20, 2017 at 16:25
  • That's not my site – MAGiK ARtS Commented Jul 20, 2017 at 16:31
Add a ment  | 

2 Answers 2

Reset to default 5

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');
    }

  })
})

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

A possible solution is to get the offset of the div and the menu from the top of the page and apply your wanted changes once they intersect.

发布评论

评论列表(0)

  1. 暂无评论