简体   繁体   English

糟糕的Javascript和JQuery性能

[英]Poor Javascript and JQuery performance

I am experiencing extreme lag issues with my javascript code. 我的JavaScript代码遇到了极端滞后问题。 Especially parallaxing is very slow. 尤其是视差非常慢。 I expect that this results from multiple executions of the functions. 我希望这是由于函数的多次执行导致的。 Here is my code: 这是我的代码:

function tada() {
  $(".arrow").addClass("tada");
  setTimeout(function () {
      $(".arrow").removeClass("tada");
  }, 1000);
}

var j = 0;

function thumb() {
    if(j < 18) {
      setInterval(function () {
        $('.equip-thumb').eq(j).css('opacity', '1');
        j++;
      }, 100);
  }
}

$(document).ready(function(){
  for (var i = 0; i < 18; i++) {
    var color = "#1b1f25";
    if ((i%3) === 0) {
      color = "#1b222c";
    }
    if ((i%3) === 1) {
      color = "#171c23";
    }
    if ((i%3) === 2) {
      color = "#2a313b";
    }
    $('.equip-thumb').eq(i).css("background-color", color);
  }
});

var fired = 0;

$(window).scroll(function(){

  var wScroll = $(this).scrollTop();
  var wHeight = $(this).height();

  $(".arrow").css({
    'opacity' : 1-wScroll/wHeight/0.5
  });

  $("#splash").css({
    'transform' : 'translate(-'+ wScroll /10 +'% , 0px)',
    'opacity' : 1-wScroll/wHeight/0.5
  });

  if(wScroll > ($('.section-equipment').offset().top - 0.6*wHeight)) {
      if (fired === 0) {
        fired = 1;
        thumb();
      }
    }
});


$(function() {
  setInterval(function () {
    tada();
  }, 4000);

  $('.equip-thumb').on({
    mouseover: function(){
      $(this).children().css('transform', 'translate(0px, 0px)');
    },
    mouseleave: function() {
      $(this).children().css('transform', 'translate(0px, 100%)');
    },
    click: function(){
      $(this).siblings().children().css('transform', 'translate(0px, 100%)');
      $(this).children().css('transform', 'translate(0px, 0px)');
    }
  });

  $('#portfolio-a').click(function (){
    $('html, body').animate({
      scrollTop: $('.section-portfolio').offset().top - 65
    }, 1000);
  });

  $('#equipment-a').click(function (){
    $('html, body').animate({
      scrollTop: $('.section-equipment').offset().top - 65
    }, 1000);
  });

  $('#contact-a').click(function (){
    $('html, body').animate({
      scrollTop: $('.section-contact').offset().top - 65
    }, 1000);
  });

});

How could I improve it? 我该如何改善?

The code below will run forever. 下面的代码将永远运行。 Because j < 18 initially, it will execute the setInterval function. 因为最初j <18,它将执行setInterval函数。 However, there is nothing that is stopping the function from ending. 但是,没有什么可以阻止该函数结束。 Therefore, you are executing $('.equip-thumb').eq(j).css('opacity', '1') 10 times a second forever! 因此,您将永远每秒执行10次$('。equip-thumb')。eq(j).css('opacity','1')!

setInterval(function () {
    $('.equip-thumb').eq(j).css('opacity', '1');
    j++;
  }, 100);

In order to fix this, you should create a for loop instead (to keep things simple) and use setTimeout instead of setInterval. 为了解决这个问题,您应该改为创建一个for循环(为使事情简单),并使用setTimeout而不是setInterval。 I hope this helps! 我希望这有帮助!

You should contemplate using requestAnimationFrame for animation, as the browser will invoke your callback before each repaint, thus it's a better guarantee that animations will be in sync with your monitor's refresh rate, Also, some browsers will make optimisations which ultimately result in more performant code. 您应该考虑使用requestAnimationFrame进行动画处理,因为浏览器将在每次重绘之前调用您的回调,因此可以更好地保证动画将与监视器的刷新率保持同步。此外,某些浏览器将进行优化,最终导致性能更高的代码。

Aside from the answers surrounding your use of setInterval , your scroll event callback could be wrapped in an invocation of requestAnimationFrame : 除了使用setInterval的答案之外,您的滚动事件回调还可以包装在requestAnimationFrame的调用中:

$(window).scroll(function () {
    requestAnimationFrame(function (lastUpdate) {
        var wScroll = $(this).scrollTop();
        var wHeight = $(this).height();

       $(".arrow").css({
            'opacity' : 1-wScroll/wHeight/0.5
        });
    });
});

The lastUpdate parameter is a timestamp representing when queued callbacks begin to fire, so you could even use this to throttle your logic. lastUpdate参数是一个时间戳,表示排队的回调何时开始触发,因此您甚至可以使用它来限制逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM