Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ive made the script but also included some changes. In your code, you would append multiple classes 'tiny' since the event gets trigged by every scrollmovement. Ive just added a class and I first check if this class has been set. If it has not been set, then I will change the color on the scroll. If you remove the 'check', the color would change every scrollmovement, which can be very cool if you aint epileptic :)</p> <pre><code> // array with the colors, you can add the colors here. var colors = ["red", "blue", "yellow", "black", "green"]; $(window).scroll(function () { if ($(document).scrollTop() == 0) { //if scrollbar is at the top (doing nothing atm) } else if($( "#header_nav" ).hasClass( "colorSet" )) { //if the colorSet class has been apended (remove this, if you want some fun :)) } else { //first, add the class, so we know we do not have to walk tru this anymore. $('#header_nav').addClass('colorSet'); // Apend a CSS background-color based on a rand function on our color array $('#header_nav').css( "background-color", colors[Math.floor(Math.random() * colors.length)] ); } }); </code></pre> <p>Now if you want, you can alter the first if statement (scrollTop() ). If you remove the colorSet class here, you will get a new random color if someone scrolls down -> top -> down again.</p> <p>For multiple classes with random colors;</p> <pre><code> // array with the colors, you can add the colors here. var colors = ["red", "blue", "yellow", "black", "green"]; $(window).scroll(function () { if ($(document).scrollTop() == 0) { //if scrollbar is at the top (doing nothing atm) } else if($( "#header_nav" ).hasClass( "colorSet" )) { //if the colorSet class has been apended (remove this, if you want some fun :)) } else { //first, add the class, so we know we do not have to walk tru this anymore. $('#header_nav').addClass('colorSet'); // Apend a CSS background-color based on a rand function on our color array $('#header_nav').css( "background-color", colors[Math.floor(Math.random() * colors.length)] ); $('.header').css( "background-color", colors[Math.floor(Math.random() * colors.length)] ); $('.header.tiny').css( "background-color", colors[Math.floor(Math.random() * colors.length)] ); } }); </code></pre> <p>And for multiple classes who share the same color;</p> <pre><code>// array with the colors, you can add the colors here. var colors = ["red", "blue", "yellow", "black", "green"]; $(window).scroll(function () { if ($(document).scrollTop() == 0) { //if scrollbar is at the top (doing nothing atm) } else if($( "#header_nav" ).hasClass( "colorSet" )) { //if the colorSet class has been apended (remove this, if you want some fun :)) } else { //first, add the class, so we know we do not have to walk tru this anymore. $('#header_nav').addClass('colorSet'); var color = colors[Math.floor(Math.random() * colors.length)]; // Apend a CSS background-color based on a rand function on our color array $('#header_nav').css( "background-color", color ); $('.header').css( "background-color", color ); $('.header.tiny').css( "background-color", color ); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload