javascript - Animate div on click to bottom and back to original position - Stack Overflow

I am trying to move a div to a certain position. The following piece of code works fine:$('#image1

I am trying to move a div to a certain position. 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. Does anybody have an easy to use idea for this? Thanks

I am trying to move a div to a certain position. 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. Does anybody have an easy to use idea for this? Thanks

Share Improve this question asked Apr 4, 2016 at 11:12 cldeclde 2193 silver badges12 bronze badges 4
  • what is the initial margin top? – Rajaprabhu Aravindasamy Commented Apr 4, 2016 at 11:13
  • Its 380px margin top – clde Commented Apr 4, 2016 at 11:15
  • How about keeping it as attribute.. data-margin = '200' – Rayon Commented Apr 4, 2016 at 11:17
  • If you can and want to use the additional jQuery UI you can use .toggleClass() with the second paramter to animate the switch between classes – empiric Commented Apr 4, 2016 at 11:20
Add a ment  | 

6 Answers 6

Reset to default 2

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="image1">image1</div>

<div id="div1"></div>

You can use a class with css transition for this. Example code -

HTML

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

CSS

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

.set_margin {
  margin-top:100px;
}

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

You can add class to #img! its clicked first time, and remove on the second time, using is as an indicator of dive being clicked. 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./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

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

By using toggleClass we can achieve this animation.

<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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745671573a4639435.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信