【问题标题】:Animation on browser scrolling浏览器滚动动画
【发布时间】:2016-06-14 09:05:21
【问题描述】:

我已经使用 jQuery 像下面的代码行

  $(window).load(function() {
  $(".shuffle").each(function() {
  $(this).circulate({
        speed: Math.floor(Math.random()*300) + 100,
        height: Math.floor(Math.random()*100) - 70,
        width: Math.floor(Math.random()*100) - 70
         });
      });
   });

循环动画在页面加载时效果很好。

html 是这样的

  <div id="target">
  <div class="shuffle"></div>
  <div class="shuffle"></div>
  <div class="shuffle"></div>
  <div class="shuffle"></div>
  </div>

现在我希望当用户向下滚动浏览器时,当他到达带有 id 目标部分的 div 时,动画应该开始......

请帮忙!!!

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-plugins


    【解决方案1】:
    var isElementInViewport = function ($element) {
        //element has to be a jQuery element with length > 0
        var domElement = $element[0];
        var height = $element.outerHeight();
        var rect = domElement.getBoundingClientRect();
        return (
            rect.top >= 0 - height &&
            rect.bottom <= $(window).height() + height
        );
    };
    
    var shuffle = function($element) {
     //only shuffle elements within the target
     $(".shuffle", $element).each(function() {
        $(this).circulate({
          speed: Math.floor(Math.random()*300) + 100,
          height: Math.floor(Math.random()*100) - 70,
          width: Math.floor(Math.random()*100) - 70
         });
      });
    }
    
    var flag = false;
    var $target = $('#target');
    $(document).on('scroll', function(){
      if(isElementInViewport($target)){
        if (!flag) {
          flag = true;
          shuffle($target);
        }
      }
    });
    

    【讨论】:

    • 它在控制台中为这一行 domElement.getBoundingClientRect(); 抛出错误消息;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多