2012-12-16 10 views
0

아래 기능은 창 높이를 창 높이로 나눔으로써 사용자가 스크롤 한 거리를 계산합니다. 그 백분율이 증가함에 따라 div의 '화살표'크기의 CSS '높이'를 늘리고 싶습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?요소의 높이를 높이려면 스크롤 막대를 사용하십시오.

$(document).scroll(function(){ 

     // grab the scroll amount and the window height 
      var scrollAmount = $(window).scrollTop(); 
      var documentHeight = $(document).height(); 

     // calculate the percentage the user has scrolled down the page 
      var scrollPercent = (scrollAmount/documentHeight) * 100; 



      function increaseHeight() { 
       $(".arrow").css({ 
        height: scrollPercent + 'px' 
       }); 

       //do something when a user gets 50% of the way down my page 
     }); 
+0

을 당신이 뭔가 잘못하고 있다고 생각되는 이유는 무엇 ? 이제 우리는 당신이 거기에서 무엇을 보는지 추측 할 필요가 있습니다. 추신 : "작동하지 않기 때문에"실제로 내 첫 번째 질문에 대한 답변을하지 않습니다 – zerkms

+0

사용자가 아래로 스크롤 div .arrow 높이가 증가해야합니다 http://jsfiddle.net/q8qPZ/ –

답변

2

이 작동합니다 - http://jsfiddle.net/LsuY4/1/

$(document).scroll(function(){ 
     // grab the scroll amount and the window height 
      var scrollAmount = $(window).scrollTop(); 
      var documentHeight = $(document).height(); 

      // calculate the percentage the user has scrolled down the page 
      var scrollPercent = (scrollAmount/documentHeight) * 100; 

      $(".arrow").css({ 
      height: scrollPercent + 'px' 
      }); 

      // do something when a user gets 50% of the way down my page 
     }); 

을 또는 (난 당신이 여기에 뭘 하려는지 확실하지 않다) :

$(document).scroll(function(){ 
    // grab the scroll amount and the window height 
     var scrollAmount = $(window).scrollTop(); 
     var documentHeight = $(document).height(); 

     // calculate the percentage the user has scrolled down the page 
     var scrollPercent = (scrollAmount/documentHeight) * 100; 

     var fnDoScroll = function() { 
     $(".arrow").css({ 
      height: scrollPercent + 'px' 
     }); 
     }; 

     // do something when a user gets 50% of the way down my page 
     if (scrollPercent >= 50) 
     fnDoScroll(); 
    }); 
+0

난 줄 '.height (scrollPercent)'. – Blender

+0

@Blender - 아마도,하지만 나는 가능한 한 많이 기존 코드를 재사용하려고하고 있습니다. –

+0

완벽한 감사 @HristoYankov - 그냥 불필요한 함수를 제거해야 할 것 같습니다 increaseHeight? –

관련 문제