Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@andy, here is Tom Th's idea worked up; ie a js constructor function, from which you can instantiate multiple instances :</p> <pre><code>function bgScroller(options) { var settings = { containerID: '', //id of the scroller's containing element scrollSpeed: 50, //Speed in milliseconds step: 1, //How many pixels to move per step imageHeight: 0, //Background image height headerHeight: 0, //How tall the header is. autoStart: true }; if(options) { jQuery.extend(settings, options); } var current = 0, // The current pixel row restartPosition = -(settings.imageHeight - settings.headerHeight), //The pixel row where to start a new loop interval = null, $container = jQuery('#' + settings.containerID), that = {}; if(!$container.length || !settings.imageHeight || !settings.headerHeight) { return false; //nothing will work without these settings so let's not even try } function setBg() { $container.css("background-position", "0 " + current + "px"); } function scrollBg(){ current -= settings.step;//Go to next pixel row. //If at the end of the image, then go to the top. if (current &lt;= restartPosition){ current = 0; } setBg(); } that.reset = function() { that.stop(); current = 0; setBg(); } that.start = function() { interval = setInterval(scrollBg, settings.scrollSpeed); }; that.stop = function(){ clearInterval(interval); }; that.reset(); if(settings.autoStart) { that.start(); } return that; } </code></pre> <p>Parameters are passed as properties of an object literal "map", overriding the hard-coded defaults in the constructor. For any parameters not included, the default values will be used. Here's a couple of examples :</p> <pre><code>var headerScroller = new bgScroller({ containerID: "header", scrollSpeed: 70, //Speed in milliseconds imageHeight: 4300, //Background image height headerHeight: 300, //How tall the header is. }); var otherScroller = new bgScroller({ containerID: "myOtherDiv", scrollSpeed: 30, //Speed in milliseconds imageHeight: 2800, //Background image height headerHeight: 200, //How tall the header is. }); </code></pre> <p>I have included three public methods; <code>.reset()</code>, <code>.start()</code> and <code>.stop()</code>, which provide limited control over scrollers after instantiation. Use as follows:</p> <ul> <li><code>headerScroller.stop();</code></li> <li><code>headerScroller.reset();</code></li> <li><code>headerScroller.start();</code></li> </ul> <p>Notes:</p> <ul> <li>Working demo <a href="http://jsfiddle.net/VfMkt/3/" rel="nofollow">here</a>.</li> <li>Dependencies: jQuery 1.0 or later</li> <li><code>.reset()</code> calls <code>.stop()</code> automatically so there's no need to call <code>.stop()</code> beforehand. </li> <li>No provision is made for changing settings after instantiation but this would be possible with a little more thought.</li> <li>jQuery plugin would be similar but would take a little more time to develop (with little advantage).</li> </ul>
    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.
 

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