【问题标题】:Animate div from right to left and then from left to right again动画 div 从右到左然后再从左到右
【发布时间】:2016-03-01 10:33:05
【问题描述】:

如果单击按钮,我会尝试将 div 从其原始位置设置为屏幕左侧的动画。如果单击另一个按钮,那么我希望它动画回到它的原始位置。我能够弄清楚如何从右到左对其进行动画处理,但我无法将其动画化回原来的位置。

var left = $('#coolDiv').offset().left;

$("#b1").click(
  function() {
    $("#coolDiv").css({
      left: left
    }).animate({
      "left": "0px"
    }, "slow");
  }
);

$("#b2").click(
  function() {
    $("#coolDiv").css({
      right: right
    }).animate({
      "right": "0px"
    }, "slow");
  }
);
#coolDiv {
  width: 50px;
  height: 50px;
  position: absolute;
  right: 0;
  background: yellow;
}
#b1 {
  margin-top: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="coolDiv">cool</div>
<button id="b1">left</button>
<button id="b2">right</button>

http://jsfiddle.net/XqqtN/5385/

【问题讨论】:

  • 我的建议是将“过渡”的东西委托给 CSS,将点击逻辑委托给 JS。
  • 检查我的答案和小提琴。

标签: javascript jquery html css animation


【解决方案1】:

你能试试这样简单的东西吗?

$(function () {
  $(".animateable").animate({
    left: $(window).innerWidth() - 50
  }, 1000, function () {
    $(".animateable").animate({
      left: 0
    }, 1000);
  });
});
* {font-family: Segoe UI; line-height: 1;}
.animateable {position: absolute; width: 50px; text-align: center; background-color: #ccf; line-height: 2em;}
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<div class="animateable">
  Hi
</div>

您可以使用setInterval 使其多次工作。

$(function () {
  setInterval(function () {
    $(".animateable").animate({
      left: $(window).innerWidth() - 50
    }, 1000, function () {
      $(".animateable").animate({
        left: 0
      }, 1000);
    });
  }, 2000);
});
* {font-family: Segoe UI; line-height: 1;}
.animateable {position: absolute; width: 50px; text-align: center; background-color: #ccf; line-height: 2em;}
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<div class="animateable">
  Hi
</div>

【讨论】:

    【解决方案2】:

    left CSS 设置为窗口宽度减去框的宽度。

    $("#b1").click(function() {
      // Get the current left of the div
      var left = $('#coolDiv').offset().left;
    
      $("#coolDiv").css({
        left: left
      }).animate({
        left: 0
      }, "slow");
    });
    
    $("#b2").click(function() {
      var left = $(window).width() - $('#coolDiv').width();
    
      $("#coolDiv").css({
        left: 0
      }).animate({
        left: left
      }, "slow");
    });
    #coolDiv {
      width: 50px;
      height: 50px;
      position: absolute;
      right: 0;
      background: yellow;
    }
    #b1 {
      margin-top: 100px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="coolDiv">cool</div>
    <button id="b1">Left</button>
    <button id="b2">Right</button>

    优化代码:

    // Common function to animate Div on both button clicks
    function animateDiv(left, right) {
      $('#coolDiv').css({
        left: left
      }).stop(true, true).animate({
        left: right
      }, 'slow');
    }
    
    $('#b1').click(function() {
      animateDiv($('#coolDiv').offset().left, 0);
    });
    
    $('#b2').click(function() {
      animateDiv(0, $(window).width() - $('#coolDiv').width());
    });
    #coolDiv {
      width: 50px;
      height: 50px;
      position: absolute;
      right: 0;
      background: yellow;
    }
    #b1 {
      margin-top: 100px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="coolDiv">cool</div>
    <button id="b1">Left</button>
    <button id="b2">Right</button>

    【讨论】:

      【解决方案3】:

      这是您更新后的代码。进行如下更改:

      $("#b1").click(
        function() {
          $("#coolDiv").animate({
              "left": "0px"
            },
            "slow"
          );
          $("#coolDiv").css('right', '');
        }
      );
      
      $("#b2").click(
        function() {
      
          $("#coolDiv").animate({
              "right": "0px"
            },
            "slow"
          );
          $("#coolDiv").css('left', '');
        }
      );
      #coolDiv {
        width: 50px;
        height: 50px;
        position: absolute;
        right: 0;
        background: yellow;
      }
      #b1 {
        margin-top: 200px;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
      <div id="coolDiv">cool</div>
      <button id="b1">B1</button>
      <button id="b2">B2</button>

      希望对你有帮助。

      【讨论】:

      • @EdwardBlack 请注意,此答案不会为 Div 设置动画,也请尝试多次单击该按钮。
      • 但是 div 在移动,不是吗?
      猜你喜欢
      • 2018-09-06
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多