jQuery 在 .animate 之后多次更改颜色和文本

标签 jquery html css

问题是当我点击按钮时,div应该移动三次,每次移动后改变背景和文本,但是当我这样做的时候 div 只分配最后一个 .css 和 .html

$(document).ready(function () {
  $("#button").click(function () {
    $(".square").animate({top: '250px'});
    $(".square").html("My friend");
    $(".square").css({
      backgroundColor: "#aa0000",
      color: "white"
    });
  //SECOND MOVE
  $(".square").animate({left: '250px'});
  $(".square").css({
    backgroundColor: "yellow",
    color: "white"
  });
  $(".square").html("NO");
  //THIRD MOVE
  $(".square").animate({top: '-10px'});
  $(".square").html("YESSSSSSSSSSSSSSS");
  //FOURTH MOVE
  $(".square").animate({left: '-10px'});
  $(".square").css({
    backgroundColor: "Black",
    color: "white"
  });
  $(".square").html("NOO")
});
<!-- Here's the html:  -->

<div id="content" class="wrapper box">
    <button id="button">Go</button>
    <article>
        <div class="square">Hello</div>
        <div class="output"></div>
    </article>
    <input type="text" id="number"/>
    <button onclick="calculate();">Check</button>
    <button onclick="sortArray();">Input array</button></div>

最佳答案

您需要在每个动画结束后触发的动画完成事件中为每个步骤添加一个函数。

参见:jQuery .animate() complete function:

尝试这样的事情:

$(document).ready(function () {
    $("#button").click(function () {

        $(".square").animate({top: '250px'}, 'slow', function() {

            $(".square").html("My friend");
            $(".square").css({
                backgroundColor: "#aa0000",
                color: "white"
            });

            //SECOND MOVE
            $(".square").animate({left: '250px'}, 'slow', function() {
                $(".square").css({
                    backgroundColor: "yellow",
                    color: "white"
                });

                $(".square").html("NO");

                //THIRD MOVE
                $(".square").animate({top: '-10px'}, 'slow', function() {
                	$(".square").html("YESSSSSSSSSSSSSSS");

                    //FOURTH MOVE
                    $(".square").animate({left: '-10px'}, 'slow', function() {
                        $(".square").css({
                            backgroundColor: "Black",
                            color: "white"
                        });
                        $(".square").html("NOO")
                    });

                });
                
            });

        });
    });
});

function calculate() { console.log("calculate");}
function sortArray() { console.log("sortArray");}
.square {
position:absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content" class="wrapper box">

<button id="button">Go</button>

<article>
    <div class="square">Hello</div>
    <div class="output"></div>
</article>
<input type="text" id="number"/>
<button onclick="calculate();">Check</button>
<button onclick="sortArray();">Input array</button></div>

编辑:

要更改 duration(速度),只需将速度作为第二个参数添加到 animate() 函数中,这样动画持续时间为 3000 毫秒,或者值越小动画越快:

$(".square").animate({top: '250px'}, 3000, function() {

关于jQuery 在 .animate 之后多次更改颜色和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40627189/

相关文章:

jquery - 如何控制 div 重叠?

javascript - 网页-button.onclick = func();与 <button onclick = "func()"?

java - 带有 servlet 的 jQuery 自动完成 UI 不返回任何数据

javascript - Polymer - 访问自定义元素内的 DOM 输入元素

jquery - 如何将图像添加到此 JQueryUI 自动完成插件的结果中?

html - 如何对齐文本旁边的图像?

html - 在我的案例中如何使用 Bootstrap 显示我的元素

css - 仅使用 CSS 将一个 DIV 放在另一个之上

Css 专注于出现的输入 div

Javascript:意外的 token 非法