The revolutionary slider only plays when the slider is visible in the browser window

I have two sliders on the page. The first one is always displayed on page load (at the top), the second at the bottom of the page.

I need to get the second slider to start playing only when it is visible (in the rollout). I got a response from the developers :

(function() {
    var win, slider, sliderHeight, sliderPaused, winHeight;

    // Change the "revapi1" part here to whatever "revapi" name that your slider uses
    slider = revapi1.on('revolution.slide.onloaded', function() {

        win = jQuery(window).on('scroll', checkScroll).on('resize', sizer);
        sizer();

    });

    function sizer() {
        sliderHeight = slider.height();
        winHeight = win.height();
        checkScroll();
    }

    function checkScroll() {
        var scrTop = win.scrollTop(),
        offset = slider.offset().top;

        if(offset <= scrTop + winHeight && offset + sliderHeight >= scrTop) {

            if(sliderPaused) slider.revresume();
            sliderPaused = false;
        } else {
            if(!sliderPaused) slider.revpause();
            sliderPaused = true;
        }
    }
})();

      

I tried using this code (I changed revapi1

to my slider id already) but it doesn't work.

Does anyone have a working solution or can they help me fix the code I have?

+3


source to share


1 answer


You need to check if the slider is in the viewport.



$(window).scroll(function() {
  var windowPosition = $(window).scrollTop();
  var slidePosition = $("#your-slide").offset().top;

  if (slidePosition < windowPosition) {
    // add code here to play slider
  }
}

      

+1


source







All Articles