How to change a div class that is fixed when scrolling to 570px. What will be javascript code. I found an article on the Internet, but they change the diva’s style, here’s the code:

if ((typeof window.pageYOffset != 'undefined' ? window.pageYOffset: document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop? document.body.scrollTop:0) >= 570) { $("div #true").css({ 'background-color':'#54a58b' }); }; 

How to change the code so that it changes the class diva

  • jQuery using? It will be easier to work with scrolling events. I can sketch a codepen example. - Invision
  • let's. if you can of course - Daniyar
  • why in css # nav-fixed.active? - Daniyar
  • how to make it simple .active was - Daniyar
  • >> How to change a div class that is fixed, when scrolling to 570px << in the example and changed to .active based on your question. In css I put styles for # nav-fixed.active, in what sense why? Well, just write .active - Invision

2 answers 2

An example of implementing your task on jQuery:

Demo: http://codepen.io/anon/pen/xZEPPz

 $(window).scroll(function(e) { var height = $(this).scrollTop(); $('#nav-fixed')[height >= 570 ? 'addClass' : 'removeClass']('active').text(height+'px'); }); 

    Replace $("div #true").css({ 'background-color':'#54a58b' }); on $("div #true").addClass('имя_класса');

    • Thanks you!! !!! - Daniyar
    • I wrote it this way, but when I scroll through 570 px, the div starts to drive, changes only the color of the text, and starts blinking .. how to decide ?? - Daniyar
    • addClass() use addClass() or toggleClass() ? Need first (addClass ()) - alenkins
    • If only the text color changes, then most likely an error in css (other properties were not specified for the class) - alenkins
    • I tried with Addclass (), but it only changes the text in the diva. and the background-color div itself does not change - Daniyar