jquery+;滚动+;检查什么元素是可见的
我想知道jQuery是否可以执行以下操作: -检查此时哪个“.main”可见,并将“.active”动态添加到“nav”中的相应链接中 问候 编辑: 我已使用以下代码计算出:jquery+;滚动+;检查什么元素是可见的,jquery,scroll,Jquery,Scroll,我想知道jQuery是否可以执行以下操作: -检查此时哪个“.main”可见,并将“.active”动态添加到“nav”中的相应链接中 问候 编辑: 我已使用以下代码计算出: //find what element is in view var inview = '#' + $('.sectionSelector:in-viewport:first').attr('id'), //find the corresponding link $link = $('.mainNav li a').f
//find what element is in view
var inview = '#' + $('.sectionSelector:in-viewport:first').attr('id'),
//find the corresponding link
$link = $('.mainNav li a').filter('[hash=' + inview + ']');
//check i its already active or not and if not
if ($link.length && !$link.is('.active')) {
//remove all previous active links and make the current one active
$('.mainNav li a').removeClass('active');
$link.addClass('active');
}
//Start same proccess on every scroll event again
$(window).scroll(function () {
var inview = '#' + $('.sectionSelector:in-viewport:first').attr('id'),
$link = $('.mainNav li a').filter('[hash=' + inview + ']');
if ($link.length && !$link.is('.active')) {
$('.mainNav li a').removeClass('active');
$link.addClass('active');
}
});
谢谢大家的帮助 使用
scrollTo
插件应该可以再次玩这个把戏了!我用插件试过了,但不知怎么的它不起作用。请参阅我的示例。你可能知道我做错了什么?可能是
$('div').bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
if (isInView) {
// find link and addClass '.active'
} else {
// remove the class
}
});