首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jquery动画与css动画之间的滞后

jquery动画与css动画之间的滞后
EN

Stack Overflow用户
提问于 2018-12-27 19:55:02
回答 3查看 73关注 0票数 0

我堆叠了5张图片,把它们都归为一个绝对位置,具有相同的上、左坐标和一个不同的z索引。

我的目标是旋转、平移并将具有最高z指数的图像的不透明度变为0,并将所有其他图像的z索引增加1。我用CSS转换将具有最高索引的图像动画化,并用jquery更改其他图像的z索引。我的代码如下:

代码语言:javascript
运行
复制
var i = 1;

function swypeStyle(){
  var imageLength = $('.my-image').length;

  $('.my-image').each(function(){
    $(this).removeClass('rotated');
  });

  $("#image"+i).addClass('rotated'); 

  setTimeout(function(){
    for(var j = 1; j <= imageLength; j++){

      var currentZindex = $('#image'+j).css('z-index');

      if(currentZindex == imageLength){
        currentZindex = 1;
      }else{
        currentZindex++;
      }

      $('#image'+j).css('z-index',currentZindex);
    }
  }, 1100);


  if(i == imageLength){
    i = 1;
  }else{
    i++;
  }
}

window.setInterval(function () {
	swypeStyle(); 
}, 3000);
代码语言:javascript
运行
复制
.my-image{
  width: 144px;
	left: 0px;
  top: 0px;
  position: absolute;
}

.my-image.rotated {
	left: -150px;
	top: 25px; 
	-webkit-transform: rotate(-30deg);
	opacity: 0;
	-webkit-transition: all 1s ease-in-out;
}
代码语言:javascript
运行
复制
<img src="https://www.otop.sg/retailer/images/image1.jpg" class="my-image" id="image1" style="z-index: 5;">
<img src="https://www.otop.sg/retailer/images/image2.jpg" class="my-image" id="image2" style="z-index: 4;">
<img src="https://www.otop.sg/retailer/images/image3.jpg" class="my-image" id="image3" style="z-index: 3;">
<img src="https://www.otop.sg/retailer/images/image4.jpg" class="my-image" id="image4" style="z-index: 2;">
<img src="https://www.otop.sg/retailer/images/image5.jpg" class="my-image" id="image5" style="z-index: 1;">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

我的动画效果很好,问题是当我切换并花费一些时间在另一个chrome选项卡上时,当我回到我的script chrome选项卡时,我的css动画和jquery动画之间会有一个滞后。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-28 11:11:56

我使用css来做我的动画,因为您不能用jQuery动画函数来动画旋转。

多亏了这篇文章,带有jquery.animate()的CSS旋转交叉浏览器,我找到了一份工作,现在我的动画是100%的jQuery,没有任何滞后。

代码语言:javascript
运行
复制
var i = 1;

  function AnimateRotate(angle,elem) {
    $({deg: 0}).animate({deg: angle}, {
      duration: 700,
      step: function(now) {
        elem.css({
        transform: 'rotate(' + now + 'deg)'
      });
    }
    });
  }

function swypeStyle(){
  var imageLength = $('.my-image').length;

  $('.my-image').each(function(){
      $(this).css({
        "left": 0,
        "top": 0,
        "opacity": 1,
        WebkitTransform: 'rotate(0deg)',
        '-moz-transform': 'rotate(0deg)'
      })
    });

    AnimateRotate("-30",$("#image"+i));

    $("#image"+i).animate({
      "left": "-150px",
      "top": "25px",
      "opacity": 0
    },700); 

  setTimeout(function(){
    for(var j = 1; j <= imageLength; j++){

      var currentZindex = $('#image'+j).css('z-index');

      if(currentZindex == imageLength){
        currentZindex = 1;
      }else{
        currentZindex++;
      }

      $('#image'+j).css('z-index',currentZindex);
    }
  }, 1100);


  if(i == imageLength){
    i = 1;
  }else{
    i++;
  }
}

window.setInterval(function () {
	swypeStyle(); 
}, 3000);
代码语言:javascript
运行
复制
.my-image{
	width: 144px;
	left: 0px;
	top: 0px;
	position: absolute;
}
代码语言:javascript
运行
复制
<img src="https://www.otop.sg/retailer/images/image1.jpg" class="my-image" id="image1" style="z-index: 5;">
<img src="https://www.otop.sg/retailer/images/image2.jpg" class="my-image" id="image2" style="z-index: 4;">
<img src="https://www.otop.sg/retailer/images/image3.jpg" class="my-image" id="image3" style="z-index: 3;">
<img src="https://www.otop.sg/retailer/images/image4.jpg" class="my-image" id="image4" style="z-index: 2;">
<img src="https://www.otop.sg/retailer/images/image5.jpg" class="my-image" id="image5" style="z-index: 1;">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

票数 0
EN

Stack Overflow用户

发布于 2018-12-27 20:11:58

问题是,当您的选项卡不活动时,Chrome将挂起像动画这样的项目,以降低资源利用率。

我的建议是只执行CSS中的所有转换/动画,并使用Javascript只添加/删除类;这样动画将保持同步。

票数 1
EN

Stack Overflow用户

发布于 2018-12-27 21:18:43

如果你真的不能放弃你的jQuery动画,你可以尝试包装你的动画在一个requestAnimationFrame()循环。As MDN解释

与CSS转换和动画一样,当将当前选项卡推入后台时,requestAnimationFrame()会暂停。

下面是一个示例:

用法:

代码语言:javascript
运行
复制
// Get element.
const elem = document.querySelector( '#elementID' )
// Get current size.
const startSize = elem.width
// Set end size.
const endSize = startSize * 2 // Double the size of the element.
// Set animation duration.
const duration = 1000 // 1000ms -> 1s

animate( ratio => {
  setWidth( elem, lerp( startSize, endSize, ratio ))
}, duration )

执行情况:

代码语言:javascript
运行
复制
// Resolves a promise once an animation is complete.
// Allows you to specify an animation duration, start and end values,
// and automatically handles frame interpolation.
function animate( callback, duration ) {
  return new Promise( resolve => {
    let
      // Amount of time in ms since last frame
      delta = 0,
      // Used to calculate delta
      lastTimestamp = 0,
      // Ratio for use in linear interpolation.
      // Passed through to the callback and incremented
      // with respect to duration.
      // The ratio determines how far along the start and end values we are.
      ratio = 0,
      // Speed is an inverse function of time, since ratio is normalized.
      speed = 1 / duration

    // Start the update loop
    update( performance.now())

    // Update loop
    function update( timestamp ) {
      if ( ratio < 1 ) {
        // Calculate frame delta and update last frame's timeStamp.
        // In the first frame, set lastFrame to this frame's timeStamp.
        delta = timestamp - ( lastTimestamp || timestamp )
        lastTimestamp = timestamp
        // Update ratio as a function of speed and frame delta
        ratio = clamp( ratio + ( speed * delta ))
        // Execute callback
        callback( ratio )
        // Request next frame
        requestAnimationFrame( update )
      }
      // Make good on the promise
      else resolve()
    }
  })
}

/** Linear interpolation between two scalars */
function lerp( min, max, t ) {
  /*
   * Get difference, multiply by `t` and add min.
   *
   * The ratio `t` represents the percentage of that distance
   * we add back to min, so that a fraction of 1 adds 100%
   * of the distance, which equals the max value.
   *
   * Precise form of linear interpolation,
   * ensures t=1 always equals max.
   */
  return ( 1 - t ) * min + t * max
}

// Set width of an element
function setWidth( element, width ) {
  element.style.width = `${ width }px` 
}

requestAnimationFrame()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53950209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档