JQuery.click.animate to move down then back up

So far I have managed to do this so that it moves down with a click in increments of 120, but I want it to go up and down, and not down and down ... I hope I explained this to someone, they’ll understand .

  <script>
  $(document).ready(function() {
    $('#footertab').click(function() {
      $('#footer').animate({
        bottom: '-=120'
      }, 1000, function() {
        // Animation complete.
      });
    });
  });
  </script>
+2
source share
4 answers

If I get it right, you want to switch the position of the footer. This can be done using the .toggle () function:

$(document).ready(function() {
  $('#footertab').toggle(function() {
    $('#footer').animate({
      bottom: '-=120'
    }, 1000);
  },function() {
    $('#footer').animate({
      bottom: '+=120'
    }, 1000);
  })
});
+6
source

You can use .toggle()for this:

$(function() { //shortcut for $(document).ready(function() {
  $('#footertab').toggle(function() {
    $('#footer').animate({ bottom: '-=120' }, 1000);
  }, function() {
    $('#footer').animate({ bottom: '+=120' }, 1000);
  });
});

.toggle(), click -= +=. , , , .

+2

Sounds like slideToggle () http://api.jquery.com/slideToggle/ may work for you.

+1
source
<script>
  $(document).ready(function() {
    $('#footertab').toggle(
      function() {
        $('#footer').animate({
          bottom: '-=120'
        }, 1000, function() {
          // Animation complete.
        });
      },
      function() {
        $('#footer').animate({
          bottom: '+=120'
        }, 1000, function() {
          // Animation complete.
        });
      },
    );
  });
  </script>
0
source

Source: https://habr.com/ru/post/1783892/


All Articles