2012-01-30 1 views
0

나는 항상 이중 작업을 많이 겪게됩니다. 내 말은, 나는 다른 div들에게 똑같은 것을 씁니다.jQuery 여러 div에 대해 복수 스크립트 자동화 jQuery에서

나는 다음과 같은 코드가 있습니다 :

var about = $('#about').offset().top - parseFloat($('#about').css('margin-top').replace(/auto/, 0)); 
var education = $('#education').offset().top - parseFloat($('#education').css('margin-top').replace(/auto/, 0)); 
var work = $('#work').offset().top - parseFloat($('#work').css('margin-top').replace(/auto/, 0)); 

$(window).scroll(function (event) { 
    // what the y position of the scroll is 
    var y = $(this).scrollTop(); 

    // whether that's below the form 
    if (y >= about) { 
     // if so, ad the fixed class 
     $('#nav li').removeClass('current'); 
     $('.ab_about').addClass('current'); 
    } else { 
     // otherwise remove it 
     $('.ab_about').removeClass('current'); 
    } 

    // whether that's below the form 
    if (y >= education) { 
     // if so, ad the fixed class 
     $('#nav li').removeClass('current'); 
     $('.ab_education').addClass('current'); 
    } else { 
     // otherwise remove it 
     $('.ab_education').removeClass('current'); 
    } 

    // whether that's below the form 
    if (y >= work) { 
     // if so, ad the fixed class 
     $('#nav li').removeClass('current'); 
     $('.ab_work').addClass('current'); 
    } else { 
     // otherwise remove it 
     $('.ab_work').removeClass('current'); 
    } 

}); 
당신은 내가 그 항목 (20)이있는 경우, 그것은 거 매우 긴 스크립트 수 있다고 말할 수

: P 에 어떤 생각 일을 자동화하고 더 작게 만드는 방법. 나는 각각의 방법으로 무언가를 시도했지만 그것은 나를위한 막 다른 길이었다.

u는 단순한 플러그인을 작성하고 같은 그 div에 해당 플러그인을 할당 할 수 있습니다

답변

3

타이 :

$.fn.myplugin = function(options){ 
    var $this = $(this); 
    var $navli = $(options.navli); 
    var $abClass = $(options.abClass); 
    var offset = $this.offset().top - parseFloat($this.css('margin-top').replace(/auto/, 0)); 
    $(window).scroll(function (event) { 
     // what the y position of the scroll is 
     var y = $(this).scrollTop(); 

     // whether that's below the form 
     if (y >= offset) { 
      // if so, ad the fixed class 
      $navli.removeClass('current'); 
      $abClass.addClass('current'); 
     } else { 
      // otherwise remove it 
      $abClass.removeClass('current'); 
     } 
    } 
} 

$(document).ready(function(){ 
    $('#about').myplugin({'navli': '#nav li', 'abClass': '.ab_about'}); 
}); 
+0

매력처럼 작동합니다. 감사합니다! :) –

+0

걱정할 필요가 없습니다. – sally

0

나는 당신이 뭘하려는 건지 아무 생각이 없지만, 그것의 모습에서 JS를 사용해서는 안됩니다.

아무튼, 그것을 단순화하는 가장 좋은 방법은 jQuery 플러그인을 만드는 것입니다. 다음과 같은 것 :

$.fn.fixStuff = function() { 
    return this.each(function() { 
     var $this = $(this); 
     return $this.offset().top - parseFloat($this.css('margin-top').replace(/auto/, 0)); 
    }); 
}; 

$('#about, #edutcation, #work').fixStuff(); 
+0

내가 사용해야 할 것은 무엇입니까? –

+0

내가 말했듯이. 나는 당신이 무엇을하려고하는지 전혀 모르지만, JS는 다소 해킹 된 것처럼 보입니다. 귀하의 상황에서 가능한지 모르겠지만 JS 해킹은 항상 마지막 옵션이어야합니다. 최대한 CSS를 사용하려고합니다. 그러나이 jQuery 플러그인이 유용 할 수도 있습니다. http://imakewebthings.github.com/jquery-waypoints/ –

+0

맞습니다. 실제로 플러그인을 확인했습니다. 내가 뭘 하려는지 : 페이지 아래로 (mousewheel로) 아래로 스크롤 할 때, 나는 anchor div와 관련하여 현재 메뉴 아이템을 동적으로 강조하고 싶었다. 이를 위해 div의 상단 오프셋을 알아야했습니다. 사용자가 다음 div로 스크롤하면 다음 메뉴 항목이 강조 표시됩니다. –

관련 문제