简体   繁体   English

点击底部并返回原始位置动画div

[英]Animate div on click to bottom and back to original position

I am trying to move a div to a certain position. 我试图将div移动到某个位置。 The following piece of code works fine: 以下代码可以正常工作:

$('#image1').click(function() {
  $('#div1').animate({
  'marginTop' : "+=160px" 
  });
});

However, I would like to animate the div to its original position once the image1 is clicked again. 但是,一旦再次单击image1,我想将div设置为原始位置的动画。 Does anybody have an easy to use idea for this? 有没有人有一个易于使用的想法? Thanks 谢谢

You can use a class with css transition for this. 您可以使用具有css transitionclass Example code - 示例代码 -

HTML HTML

<div class="main">
    Hello
</div>

CSS CSS

.main {
  background: green;
  width: 100px;
  height:100px;
  margin-top:0px;
  transition: margin-top 1s;
}

.set_margin {
  margin-top:100px;
}

jQuery jQuery的

$('.main').on('click', function() {
    $(this).toggleClass('set_margin'); // toggle class on click event
})

You can implement it like - 你可以像 -

$('#image1').click(function() {
    $('#div1').toggleClass('set_margin');
});

Fiddle 小提琴

Another way: 其他方式:

 function firstClick() { $('#div1').animate({ 'marginTop' : "380px" }); $(this).one("click", secondClick); } function secondClick() { $('#div1').animate({ 'marginTop' : "0px" }); $(this).one("click", firstClick); } $("#image1").one("click", firstClick); 
 #div1 { width: 200px; height: 200px; background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="image1">image1</div> <div id="div1"></div> 

You can add class to #img! 您可以将类添加到#img! its clicked first time, and remove on the second time, using is as an indicator of dive being clicked. 它第一次点击,第二次删除,使用is作为潜点击的指示。 Example: 例:

$('#image1').click(function() {
  if($('#image1').hasClass("clicked")){
      $('#div1').animate({
         'marginTop' : "-=160px" 
      });
      $('#image1').removeClass("clicked");
  }
  else{
    $('#image1').addClass("clicked");
        $('#div1').animate({
         'marginTop' : "-=160px" 
      });
  }
});

One more way 还有一种方法

 var toggle = true; $('#image1').click(function() { if (toggle) { $('#div1').animate({ 'marginTop': "+=160px" }); toggle = false; } else { $('#div1').animate({ 'marginTop': "-=160px" }); toggle = true; } }); 
 #div1 { height: 50px; width: 50px; background: black; border-radius: 50%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="image1">Toggle</button> <div id="div1"></div> 

Here and example with codepen live editor 这里和codepen实时编辑器的例子

 var move = true $('#image1').click(function() { if (move) { $('#div1').animate({ 'margin-left': "+=160px" }, move = false); } else { $('#div1').animate({ 'margin-left': "0px" }, move = true); } }); 
 #image1 { width:100px; height:100px; background:red; cursor:pointer } #div1 { width:100px; height:100px; background:green; margin-left:0px; } 
 <div id="image1">ClickMe</div> <div id="div1"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

By using toggleClass we can achieve this animation. 通过使用toggleClass我们可以实现这个动画。

<style type="text/css">
    .animation{
        margin-top:160px;
    }
</style>
<script type="text/javascript">

 $(document).ready(function(){
    $('#image1').click(function(){
        $('#div1').toggleClass("animation");
    })
 })
</script>

Here is JsFiddle link animate 这是JsFiddle链接动画

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

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