简体   繁体   English

检查div是否动画,并找出div的位置

[英]Check if div is animated and find out the position of the div

I have animated my div element to move to the right side when the 'Sign in' button is clicked. 单击“登录”按钮后,我使div元素移动到右侧。

However when the sign up button is clicked I want the div to move back over to the right. 但是,当单击“注册”按钮时,我希望div移回右侧。

I just wanted to know if there is any way I could check to see if the div has been animated to the left. 我只是想知道是否有什么方法可以检查div的动画是否在左侧。

 var $width = $("body").width() / 2;; var $animatedDiv = $(".hero-container"); $("#sign-in").click(function() { $animatedDiv = $(".hero-container").animate({ "left": $width }, "slow"); $("#welcome").html("Sign In"); $(".form").show(); }); $("#sign-up").click(function() { // if the div is on the right, then move it to the left if ($animatedDiv.is(':animated')) { $animatedDiv = $(".hero-container").animate({ "right": $width }, "slow"); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section class="welcome"> <div class="hero-container"> <h1 id="welcome">Welcome!</h1> <p>Welcome back</p> <button id="sign-in">Sign in</button> <button id="sign-up">Sign up</button> </div> <div class="form"> <form class="sign-in"> <h1 id="signIn-heading">Sign In</h1> <input type="text" name="email" placeholder="Email"> <input type="text" name="pass" placeholder="Password"> </form> </div> </section> 

If the div is on the right I need to move it to the left when the 'Sign up' button is clicked, therefore the sign up form will display. 如果div位于右侧,则在单击“注册”按钮时需要将其向左移动,因此将显示注册表格。

I'm sure there are multiple ways to accomplish your goal. 我相信有多种方法可以实现您的目标。 One simple solution is to set a variable once the animation has completed: 一种简单的解决方案是在动画完成后设置一个变量:

    var animated_pos = "left";

    var $width =  $("body").width()/2;;
    var $animatedDiv = $(".hero-container");
    $("#sign-in").click(function(){

    $(".hero-container").animate({"left": $width,done:function(){animated_pos = "left";}}, "slow");

    $("#welcome").html("Sign In");

    $(".form").show();

    });

    $("#sign-up").click(function(){

    // if the div is on the right, then move it to the left

    if(animated_pos == "left"){
$(".hero-container").animate({"right": $width,done:function(){animated_pos = "right";}}, "slow");

    }


     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM