Home » jQuery » jQuery – Auto Scrolling the Slider

jQuery – Auto Scrolling the Slider

B03B0037 Question from Yuhsin:

I would like to make the jquery steps slider to do auto-increment when the window loads. Is there an easy way to trigger the slide to move by itself ?

I’m assuming this question is looking for the content to auto scroll as well.  So let’s pull up the code from last week and take a look at this issue.

There is a timer plugin available for jQuery that will do some of the work for you, but all we really need for this functionality is:

  • A function to do the auto incrementing
  • A call to the javascript setTimeout() function.

So first, the auto incrementing function.

function scrollWindow() {

    var slideValue;
    slideValue = $("#slider").slider("value");
    if(slideValue > -100)
    {
        $("#slider").slider("value", slideValue - 1);
        setTimeout(scrollWindow, 1000);
    }
}

A couple things to note about this function:

  • We are retrieving the current position of the slider using the .slider() method of the slider widget.
  • We are assuming that the range of our slider is between 0 and -100.
  • We call setTimeout at the end to call this function again in a second.
  • We only call setTimeout if we have not reached the end of our scrolling.

The only thing left is that we need to kick this off when the page loads.  To do this we add a call to setTimeout in our jQuery ready method:

$(function() {
    $("#slider").slider(
    { change: handleChange,
        slide: handleSlide,
        min: -100,
        max: 0
    });
    setTimeout(scrollWindow, 1000);
});

The actual scrolling of the window happens “automatically” with the code we wrote last week.

 

Other post in jQuery

Related Post

  • jQuery – Using Slider as a ScrollbarjQuery – Using Slider as a Scrollbar Last week I introduced the jQuery UI element known as a slider and indicated that this control could easily be used as a custom scrollbar.Today I'm going to show you exactly how […]
  • jQuery, JSON, and ASP.NETjQuery, JSON, and ASP.NET A few months ago, I demonstrated how to use ASP.NET's JSON capabilities.  Lately, I've been demonstrating how to use jQuery.  Both use a considerable amount of JavaScript so if there is […]
  • jQuery – Retrieving HTML FragmentsjQuery – Retrieving HTML Fragments A couple of weeks ago I mentioned that I had built a tooltip using jQuery.  We focused mostly on the positioning of the tooltip at the time because, historically, that's where most of the […]
  • jQuery, bgiframe and IE6 z-order hacksjQuery, bgiframe and IE6 z-order hacks Well, it probably looks like an odd title.  But let's face it: we still have to support IE6 for some of our clients no matter how much we don't like it or it shouldn't be.  And one of the […]
  • jQuery SplitterjQuery Splitter Hey!  This is pretty cool. I was just mentioning at the last jQuery presentation I did that there were some controls that were definitely missing from the jQuery UI suite of […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.