Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about this, see <a href="http://jsfiddle.net/MW8N3/" rel="nofollow"><strong>JSFiddle</strong></a> (explained below):</p> <pre><code>var height_colors = [ [0, "#FFF"], [50, "#FFFF00"], [100, "#FF0000"], [150, "#FF00FF"], [200, "#00FF00"], [250, "#0000FF"], [300, "#00FFFF"], [350, "#123456"], [400, "#654321"], [450, "ABCDEF"], [500, "FEDCBA"] ]; var box = $("#lipsum"); box.on("scroll", function () { var scrollPos = $(this).scrollTop(); $.each(height_colors, function(key, value) { if(scrollPos &lt; value[0] || key == height_colors.length - 1) { box.css("background", value[1]); return false; } }); }); </code></pre> <p>Given a scrollable div <code>#lipsum</code>, and an Array of <code>[&lt;height&gt;, &lt;color&gt;]</code> values, when we scroll our element, we check it's <code>scrollTop()</code> value, and iterate over our array until we find a <code>height</code> value which is lower than the current <code>scrollTop()</code>. </p> <p>Now, this could be transformed in a number of ways. For example, rather than calculate the position of a scrollable div, you could calculate the scroll position relative to the <code>document</code>, and then replace your <code>height_colors</code> array, to be an array of elements, and colors to iterate over. You'd do very much the same thing, but calculate the <code>offset</code> of the iterated elements until you've got an element with an <code>offset</code> greater than the current <code>scrollTop</code>.</p> <p>Your new Array could look like:</p> <pre><code>var height_colors = [ ["#elem-a", "#FFF"], ["#elem-b", "#555"], ... ] </code></pre> <p>It might be better in this case to keep a track of the last checked element so that you use the "one before last" once you realise you've gone too far. (not checked this code)</p> <pre><code> // Assign a default color for scroll positions before your first element var last_color = "#FFF"; $.each(height_colors, function(key, value) { var current = $(value[0]); if(scrollPos &lt; current.offset().top) { $("body").css("background", last_color); } last_color = value[1]; }); </code></pre> <p>The link you provided seems to do something similar, and as someone pointed out, you could use the jQuery UI variant of <code>.animate()</code> to fade between colours (though in my example with only 50 pixel stops, a 200ms animation was far too long).</p>
    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.
    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