jQuery 使用 animate() 从右向左滑动一个 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16208402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Slide a div from right to left using animate()
提问by Nagaraj Chandran
I want a div '.whole'
to animate (slide from right to left)
我想要一个 div'.whole'
动画(从右向左滑动)
jQuery
jQuery
$('#menu').click(function() {
$('.whole').toggleClass('r2');
$('#slideMenu').toggle();
});
.r2 { right: 200px }
I am not able to use the function animate() properly.
我无法正确使用 animate() 函数。
回答by MarioDS
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.
但是您的position
属性应该已经在 CSS 中设置,否则您可能无法完全获得所需的内容。
To explain: the function takes a JS object of properties, like this:
解释一下:该函数接受一个 JS 属性对象,如下所示:
{
right: '200px',
somethingElse: 'value',
myboolean: true
}
you can also assign this to a var and pass it to animate
:
您还可以将其分配给 var 并将其传递给animate
:
var cssProperties = { right: '200px' }
$('#menu').click(function() {
$('.whole').animate(cssProperties);
});
You can pass other arguements as readable in the documentation.
您可以传递文档中可读的其他论点。