【问题标题】:jQuery - How to run an .animate while the mouse travels a timeline?jQuery - 如何在鼠标移动时间轴时运行 .animate?
【发布时间】:2013-02-05 12:49:22
【问题描述】:

我希望 div 在鼠标在时间轴上移动时运行此动画。 div 以width/height 100px 开头,当鼠标接近时间线的一半时,div 同时调整为width/height 200px。当鼠标接近时间线的末尾时返回到 div width/height 100px

$('#timeline').mousemove(function(e){
    var position = e.clientX;
    $('#mark').css("left", position - 50);
});


$('#animation').animate({
    width: "200",
    height: "200"
}, 2000).animate({
    width: 100,
    height: 100
}, 2000);

jsfiddle.net

请提出建议。

【问题讨论】:

  • 你是想让方块随着你在时间线上的位置按比例变大,还是只在你越过某个阈值时触发动画?
  • 按比例变大并变小。完整的动画放大到 200px,然后缩小到 100px。因此,当标记是时间线的一半时,将是他达到 200px 的时刻,并且当我们接近时间线的尽头时,将按比例减小到 100px
  • 现在正在研究解决方案。如果您不耐烦,需要使用 Math.abs()。
  • 老兄,谢谢。我在焦急地等待

标签: jquery jquery-animate offset timeline


【解决方案1】:

你需要做一些Math,宝贝!具体来说,abs 函数可用于计算您距离中点的距离。使用以下方法计算与中心线的距离

var timelineWidth = $('#timeline').width;
var dist = Maths.abs((width/2) - e.ClientX); // distance from the middle

您的最终函数将如下所示:

$('#timeline').mousemove(function(e){
    var timelineWidth = $('#timeline').width();
    var position = e.clientX;
    /* Calculate the distance from the mid-point */
    var dist = Math.abs((position - 50) - timelineWidth/2);
    /* Work out the percentage increase needed for your square */
    var squareSide = ((timelineWidth-dist)/timelineWidth) * 200;
    $('#mark').css("left", position - 50);
    /* Apply the transformation to your square */
    $('#animation').height(squareSide);
    $('#animation').width(squareSide);
});

希望有帮助!

【讨论】:

  • PS:我没有使用animate 函数,因为这样转换会滞后于用户输入。如果您仍然想使用动画,但这应该不是问题。
  • 老兄,非常感谢你。来自巴西的问候
猜你喜欢
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多