0

下面的函数通过获取窗口高度并将其除以窗口高度来计算用户滚动了多远。随着该百分比的增加,我想增加 div 'arrow' 的 css 'height' 数量。我究竟做错了什么?

$(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
      });  
4

1 回答 1

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();
  });
于 2012-12-16T07:57:08.647 回答