2011-09-21 3 views
0

가 나는 경우의 수를 알고 싶습니다 어떤 요소를 볼 수 확인 : 순간에 볼 수 있습니다 ".main"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 -visible-after-scroll) –

답변

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

관련 문제