【问题标题】:jQuery animation always same speedjQuery 动画总是相同的速度
【发布时间】:2014-02-25 23:11:29
【问题描述】:

我想更改 jQuery 动画的速度,使其始终保持相同的速度。我有 4 个 div 为 mouseenter 上的背景图像位置设置动画。我现在看到动画总是有不同的速度,总是需要 2500 毫秒。我希望速度总是一样的,这怎么可能?

HTML:

<div id="imgholder">
<div class="imggridcol" id="linksboven"></div>
<div class="imggridcol" id="rechtsboven"></div>
<div class="imggridcol" id="linksonder"></div>
<div class="imggridcol" id="rechtsonder"></div>
</div>

CSS:

#imgholder {
width:960px;
height:380px;
overflow:hidden;
background: url("http://crispme.com/wp-content/uploads/4379.jpg?pass") no-repeat;
background-position:50% 50%;
background-size:150% 150%;
}

.imggridcol {
width:50%;
height:50%;
margin:0px;
float:left;
}

jQuery:

$("#linksboven").mouseenter(function() {
  $(this).parent().stop().animate({
  backgroundPositionX: '0%',
  backgroundPositionY: '0%'
  }, 2500, "linear");
});

$("#rechtsboven").mouseenter(function() {
  $(this).parent().stop().animate({
  backgroundPositionX: '100%',
  backgroundPositionY: '0%'
  }, 2500, "linear");
});

$("#linksonder").mouseenter(function() {
  $(this).parent().stop().animate({
  backgroundPositionX: '0%',
  backgroundPositionY: '100%'
  }, 2500, "linear");
});


$("#rechtsonder").mouseenter(function() {
  $(this).parent().stop().animate({
  backgroundPositionX: '100%',
  backgroundPositionY: '100%'
  }, 2500, "linear");
});

$(".imggridcol").mouseleave(function() {
 $(this).parent().stop();   
});

JSFiddle demo

【问题讨论】:

  • 我认为速度不同的原因是因为您正在设置动画从当前位置到结束帧所需的时间。如果你已经到了一半,速度会快一倍,因为还有一半的距离,这里有一个链接可能会有所帮助stackoverflow.com/questions/2331234/…
  • 那是因为animate 在你付出的时间里做任何事。在您的情况下,您将在 2.5 秒内将背景移动到一侧或另一侧。例如,如果您从图像的左侧开始动画,则将整个图像向右移动将持续 2.5 秒。如果您从中心开始动画(图像居中),它将持续相同的时间前进一半像素,因此效果会变慢。

标签: javascript jquery html css animation


【解决方案1】:

http://lukeshumard.github.io/supremation/

根据我的评论,这实际上似乎是答案。这个 jQuery 插件应该会有所帮助。只需调用,例如:


$("#linksboven").mouseenter(function() {
      $(this).parent().stop().supremate({
      backgroundPositionX: '0%',
      backgroundPositionY: '0%'
      }, 2500, "linear");
});

代替 animate 函数,它会处理其余部分

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-06-18
  • 2012-02-04
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
相关资源
最近更新 更多