I need to detect scroll direction on mouseenter
on certain div so I try to use code below
$.fn.scroll = function () {
var $this = $(this);
var $list = $(this).find('ul');
var $lis = $list.find('li');
var direction;
$this.addClass('scroll');
$list.addClass('slides-list');
$lis.addClass('slide');
$lis.filter(':first').addClass('current');
$lis.filter(':not(:first)').addClass('past');
$this.on('mouseenter', function(){
var lastScrollTop = $this.offset().top;
$(window).scroll(function () {
if ($this.hasClass('animated')) {
return;
}
var scrollTop = $(window).scrollTop();
if (scrollTop > lastScrollTop) {
direction = 'down';
} else {
direction = 'up';
}
lastScrollTop = scrollTop;
});
});
}
$(document).ready(function () {
$('#scroll').scroll();
});
but in line
var lastScrollTop = $this.offset().top;
I get an error
Uncaught TypeError: Cannot read property 'top' of undefined
.
I guess I have some problem with scopes, but I don't know how to fix it?