javascript - 我希望我的动画在屏幕上水平移动并在我按下停止按钮后停止

标签 javascript jquery animation

我想在这里做一些事情:

开始按钮开始动画然后变成停止按钮。 停止按钮然后停止动画,然后变回开始按钮,使我能够从停止的地方继续。

相反,一旦我再次按下开始或停止,动画就会消失。

对于这类事情,我是新手,如果我的意图不清楚,请告诉我,我会尽力澄清。

<html>
<head>
    <meta charset="UTF-8">
    <style>
        div {left: 0px;
             bottom: 100px;}
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script> 
        $(document).ready(function(){
            move();
            Stop();
            });
        function move(){
          $("input").click(function(){
              $(this).val('Stop');
            $("div").css({left:'0%'}).animate({left:'100%'},1000, Stop);
              Stop();
          });
        }
        function Stop(){
          $("input[value='Stop']").click(function(){
              $( ":animated" ).stop(true, true, false);
              $(this).val('Start');
              move();
        });
        }
    </script> 
</head>
<body>        
    <h1 style="text-align:center"> Welcome to the test</h1>
    <input type='button' value='Start' id='oneButton'>
    <div style="height:100px;width:100px;position:absolute;">
        <img id='myRobot' src='myRobot.jpg' width="250px" height="200px"/>
    </div>    
</body>
</html>

最佳答案

您的 JavaScript 代码有点乱。首先你的移动功能:

function move(){
    $("input").click(function(){
        $(this).val('Stop');
        $("div").css({left:'0%'}).animate({left:'100%'},1000, Stop);
        Stop();
    });
}

您的Stop 函数(不是设置为动画函数回调函数的函数)将在div 动画时被调用。你不想要这个。

您可能想要的基本上是三个不同的函数:

  1. 移动
  2. 暂停
  3. 重置

你的移动函数实际上会开始移动你的对象,暂停显然会暂停它,而重置会将你的对象置于初始位置,如果你愿意的话。

假设您的 HTML 文件的结构如下:

<h1 style="text-align:center"> Welcome to the test</h1>
<input type='button' value='Start' id='oneButton' />
<div id="object">
    <img id='myRobot' alt='test' src='http://www.clker.com/cliparts/7/b/d/b/1237099752389782475nicubunu_Soccer_ball.svg.thumb.png'/>
</div>

你的 CSS:

#object {
    position: absolute;
    left: 0px;
    bottom: 100px;
    width: 100px;
    height: 100px;
}

最后是 JS:

var animating = false;

$("#oneButton").on("click", function() {
    if (!animating) {
        $(this).val("pause");
        move();
    } else {
        $(this).val("start");
        pause();
    }
});

function move() {
    animating = true;
    $("#object").animate({left:'100%'},1000, reset);
}

function pause() {
    $("#object").stop();
    animating = false;
}

function reset() {
    animating = false;
    $("#object").css({left: '0%'});
}

这是一个FIDDLE你可以在“行动”中看到的地方。

关于javascript - 我希望我的动画在屏幕上水平移动并在我按下停止按钮后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27135686/

相关文章:

javascript - 调整悬停时内容的垂直对齐方式

javascript - js 回调基于生成器的代码

android - 动画文字字母

javascript - Three.js:重新连接骨骼后如何重新绑定(bind)/重新连接网格骨架?

javascript - 不使用 JQUERY 将脚本 append 到 DOM

javascript - 如何使两个函数在单击时使用react

javascript - 搜索 div

javascript - 返回 Promise 而不是 jquery ajax 调用

javascript - jQuery on(click) 不起作用,但 on(hover) 起作用

javascript - jquery 中的显示/隐藏速度很快。但渲染引擎太慢