javascript - How to change color when user scrolls -


i have piece of jquery code changes elements of couple of colors when users scrolls down. when user scrolls page, want switch original color. doesn't work way expect...

original code works

jquery(window).scroll(function()  {     var scrolltop = jquery(this).scrolltop(),     offset = jquery('#masthead').height() - 55;     if ( scrolltop < offset )          jquery(".float-manu").css({ backgroundcolor: "rgba(0,0,0,0)" });     else          jquery(".float-manu").css({ backgroundcolor: "rgba(0,0,0,0.7)" }); }) 

modified code works

})(window.jquery); jquery(window).scroll(function()  {     var scrolltop = jquery(this).scrolltop(),     offset = jquery('#masthead').height() - 55;     if ( scrolltop < offset )          jquery(".float-manu").css({ backgroundcolor: "rgba(0,0,0,0)" });     else          jquery(".float-manu").css({ backgroundcolor: "rgba(0,0,0,0.7)" });         jquery(".primary-menu a").css({ color: "white" }); }) 

adding in additional css modifier first if statement kills script.

})(window.jquery);  jquery(window).scroll(function()  {     var scrolltop = jquery(this).scrolltop(),     offset = jquery('#masthead').height() - 55;     if ( scrolltop < offset )          jquery(".float-manu").css({ backgroundcolor: "rgba(0,0,0,0)" });         jquery(".primary-menu a").css({ color: "black" });     else          jquery(".float-manu").css({ backgroundcolor: "rgba(0,0,0,0.7)" });         jquery(".primary-menu a").css({ color: "white" }); }) 

i see missing brace in if , else. hence first line following if , else gets executed. instead add brace so:

 .....  if ( scrolltop < offset ) {       jquery(".float-manu").css({ backgroundcolor: "rgba(0,0,0,0)" });       jquery(".primary-menu a").css({ color: "black" });  } else {      jquery(".float-manu").css({ backgroundcolor: "rgba(0,0,0,0.7)" });      jquery(".primary-menu a").css({ color: "white" });  }  .... 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -