RinoDrummer icon

ScrollUp/Down

RinoDrummer | PRO | 05/26/15 09:12:03 AM UTC | 0 ⭐ | 492 👁️ | Never ⏰ | []
JavaScript |

1.21 KB

|

None

|

0 👍

/

0 👎

$(document).ready(function () {
    var lastScrollPosition = 0;
    var currStep = 0;
    var totalSteps = 0;
    
    jQuery.fn.extend({
        scrollTo: function (elem, speed) {
            if (!console.assert(($(this).children(elem).length > 0), "No children found for " + this)) {
                $(this).animate({
                    scrollTop: $(elem).offset().top;
                }, speed, "linear", function () {
                    setTimeout($(this).scrollTo(elem, speed), 400);
                });
            }
        }
    });
    
    $('body').scroll(function () {
        if ($(this).scrollTop() < lastScrollPosition) {
            $(this).trigger('scrollUp');
        }
        else if ($(this).scrollTop() > lastScrollPosition) {
            $(this).trigger('scrollDown');
        }
    });
    
    $('body').on('scrollUp', function () {
        if ((currStep > 0) || (currStep <= totalSteps)) {
            ++currStep;
            
            $(this).scrollTo('section[data-step="' + currStep + '"]', 800);
        }
    });
    
    $('body').on('scrollDown', function () {
        if ((currStep >= 0) || (currStep < totalSteps)) {
            --currStep;
        }
    });
});

Comments