jQuery,我该如何切换边距

MCS*_*CSI 1 css jquery jquery-ui

我正在使用jQuery-ui; 我怎么能首先'切换''保证''我'-160px'然后我把'33px'我想要每次点击替换为'.LoginButton'

CSS

.MyBox {
            font-family:Arial;
            color:#FFF;
            width: 400px;
            height: 150px;
            margin-top:-160px;
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

 $('.LoginButton').click(function () {
      $('.MyBox').animate({
           marginTop: '33px'
           }, {
           duration: 2000,
           specialEasing: {
           marginTop: 'easeOutBounce'
           },
           complete: function () {
           }
         });
 });
Run Code Online (Sandbox Code Playgroud)

谢谢!!

bbb*_*bco 6

或者您可以使用.toggle() http://api.jquery.com/toggle-event/

$('.LoginButton').toggle(
  function () {
    $('.MyBox').animate({
       marginTop: '-160px'
       }, {
       duration: 2000,
       specialEasing: {
       marginTop: 'easeOutBounce'
       },
       complete: function () {
       }
    });
  },
  function () {
    $('.MyBox').animate({
       marginTop: '33px'
       }, {
       duration: 2000,
       specialEasing: {
       marginTop: 'easeOutBounce'
       },
       complete: function () {
       }
    });
  }
);
Run Code Online (Sandbox Code Playgroud)