最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论