Slide a div from right to left using animate()

拈花ヽ惹草 提交于 2020-01-09 20:40:16

问题


I want a div '.whole' to animate (slide from right to left)

jQuery

$('#menu').click(function() {
      $('.whole').toggleClass('r2');
      $('#slideMenu').toggle();
});

.r2 { right: 200px }

I am not able to use the function animate() properly.


回答1:


This should work:

$('#menu').click(function(event) {
      event.preventDefault(); // because it is an anchor element
      $('.whole').animate({
          right: '200px'
      });
      $('#slideMenu').toggle();
});

But your position property should already be set in CSS or you might not get exactly what you need.

Working JSFiddle

To explain: the function takes a JS object of properties, like this:

{
    right: '200px',
    somethingElse: 'value',
    myboolean: true
}

you can also assign this to a var and pass it to animate:

var cssProperties = { right: '200px' }

$('#menu').click(function() {
  $('.whole').animate(cssProperties);
});

You can pass other arguements as readable in the documentation.



来源:https://stackoverflow.com/questions/16208402/slide-a-div-from-right-to-left-using-animate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!