Search code examples
javascriptjqueryjquery-scrollify

Get last viewed index with Scrollify


I'm using scrollify to swap out fixed position components, and adding/removing classes to animate the transition. I have everything working as I'd like as you progress forward through the scroll, but when you scroll up to see a previous section, its not removing the previous class, and the previous section now animates in behind it.

I don't think the prev method will be of use, because it doesn't call the previous section your were at, only the previous section assuming you are always moving forward.

Here is my code below, you can move forward just fine, but trying to go backwards presents a problem as the new current section will load behind the previous one, and the previous one will still be visible.

jsfilddle here

var wrapper = $('.wrapper');
var currentPosition = 0;

$(wrapper).each(function(index) {
    if (currentPosition != index) {
        $(this).css('opacity', 0);
    } else if (currentPosition == index) {
        $(this).css('opacity', 1);
    }
});


$(function() {
    $.scrollify({
        section: ".wrapper",
        scrollSpeed: 700,
        setHeights: false,
        after: function(index, sections) {
            var prevWrapper = $.scrollify.current().prev();
            var currentWrapper = $.scrollify.current();
            var nextWrapper = $.scrollify.current().next();

            $(prevWrapper).removeClass('wrapper-enter').addClass('wrapper-leave');
            $(currentWrapper).removeClass('wrapper-leave').addClass('wrapper-enter');
        },
    });
});

Solution

  • The issue is depending on scrolling up or down your "next" or "prev" may not actually be what you think so you are hiding classes incorrectly.

    What you could do as scrollify doesn't have an option to detect scroll direction is to create a super quick variable that will detect if you are scrolling up or down. Then simply update your classes then in the after function.

    $.scrollify({
            section: ".wrapper",
            scrollSpeed: 700,
        setHeights: false,
            after: function(index, sections) {
        var prevWrapper = $.scrollify.current().prev();
                var currentWrapper = $.scrollify.current();
                var nextWrapper = $.scrollify.current().next();
                let elem = null;
    
          // Add wrapper-enter to current element. 
          $(currentWrapper).removeClass('wrapper-leave').addClass('wrapper-enter');
                if(lastIndex < index) {
              // Scolled down if lastIndex < index
              elem = prevWrapper;
            } else {
              // Scrolled up if last index > index
              elem = nextWrapper;
          }
          $(elem).removeClass('wrapper-enter').addClass('wrapper-leave'); 
          lastIndex = index;
            },
    

    In the above I simply created a variable to track the last index.

    Here is a working fiddle: https://jsfiddle.net/k1e6x79f/