2011-09-21 43 views
0

我想什麼元素是可見的知道它有可能與jQuery做到以下幾點:的jQuery +滾動+檢查

http://jsfiddle.net/AzFJR/2/

-check其中「主要」目前可見並動態地將「.active」添加到「nav」中的相應鏈接?

問候!

編輯:

我一直在使用的工作了這Viewport Plugin和下面的代碼:

//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');  
    } 
}); 

感謝every1的幫助!

+0

可能重複(http://stackoverflow.com/questions/487073/check-if-element-is - 滾動後可見) –

回答

2

使用jQuery ELEMENT ‘INVIEW’ EVENT plugin

$('div').bind('inview', function(event, isInView, visiblePartX, visiblePartY) { 
    if (isInView) { 
    // find link and addClass '.active' 
    } else { 
    // remove the class 
    } 
}); 
+0

謝謝!會看看! – Andrej

+0

再次嗨!我試着用插件,但它不知道怎麼工作。在http://jsfiddle.net/87uU5/2/上查看我的示例。你可能會看到我做錯了什麼? – Andrej