jquery +滚动+检查哪些元素可见
我想知道 jQuery 是否可以执行以下操作:
-检查当前哪个“.main”可见,并动态地将“.active”添加到“nav”中的相应链接?
问候!
编辑:
我已经使用这个 Viewport 插件 和以下代码解决了这个问题:
//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');
}
});
感谢大家的帮助!
I would like to know if its possible with jQuery to do the following:
-check which ".main" is visible at the moment and dynamically add ".active" to the corresponding link in the "nav" ?
Regards!
Edit:
I have worked it out using this Viewport Plugin and following code:
//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');
}
});
Thanks every1 for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 jQuery ELEMENT 'INVIEW' EVENT 插件。
Use the jQuery ELEMENT ‘INVIEW’ EVENT plugin.
scrollTo
插件应该可以解决问题 http://flesler.blogspot .com/2007/10/jqueryscrollto.htmlThe
scrollTo
plugin should do the trick http://flesler.blogspot.com/2007/10/jqueryscrollto.html