jQuery inview not detecting current item

329 Views Asked by At

I'm trying to recreate a behaviour similar to Foundation's magellan navigation (or this http://www.outyear.co.uk/smint/) where I use inView (https://github.com/protonet/jquery.inview) to detect the current section. I can't seem to detect the current item and grab its class name to be used as a selector for the navigation. The plugin already exist on the project, so I'm just taking advantage of it's presence.

So far here's what I have: https://jsfiddle.net/xrobhw2b/1/

    $('section').bind('inview', function(e, isInView, visiblePartX, visiblePartY) {
        var elem = $(this);
        var curItemId = elem.attr('id');
        var mainNav = $("#main-navigation ul");

        if (elem.data('inviewtimer')) {
          clearTimeout(elem.data('inviewtimer'));
          elem.removeData('inviewtimer');
        }

        if (isInView) {
          elem.data('inviewtimer', setTimeout(function() {
;
            if (visiblePartY == 'top') {
              elem.data('seenTop', true);
            } else if (visiblePartY == 'bottom') {
              elem.data('seenBottom', true);
            } else {
              elem.data('seenTop', true);
              elem.data('seenBottom', true);
            }

            if (elem.data('seenTop') && elem.data('seenBottom')) {

        console.log( $(elem).attr('id') );

              mainNav.find('li').removeClass('active');
              mainNav.find('.' + id ).addClass('active');

            }
          }, 0));
        }
    });

enter image description here

0

There are 0 best solutions below