jQuery:将参数作为属性传递到 animate 方法中
jQuery: passing arguments into the animate method as properties
我对jQuery的问题是,当我使用参数dir
时,动画不起作用,但是当我平坦地使用类型left
或right
(与dir
传递的值完全相同)时,它似乎很好。
function update(dir) {
$('#label').animate({
dir: '200px'
}, 'slow');
}
$('#goLeft').click(function() {
update('left');
});
您可以使用括号表示法将变量作为属性传递。
在这种情况下,您将使用 [dir]
:
function update(dir) {
$('#label').animate({
[dir]: '200px'
}, 'slow');
}
基本示例:
function update(dir) {
$('#animate').animate({
[dir]: '100px'
}, 'slow');
}
$('#go-left').click(function() {
update('left');
});
$('#go-down').click(function() {
update('top');
});
#animate { position: relative; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="go-left">Go left</button>
<button id="go-down">Go down</button>
<div id="animate">Animate me</div>
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 数组在递归方法中设置为null
- 打破承诺链的好方法是什么
- 有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)
- 有没有一种方法可以用步骤递归调用jQuery animate()
- Animate 方法在 jquery 中的属性及其值
- jQuery:将参数作为属性传递到 animate 方法中
- remove() 方法在 animate() 中不起作用
- jQuery+Edge Animate:仅当从单击事件调用时才出错:对象 [对象对象] 没有方法“基础”
- 可以jQuery's.animate()方法可以影响变量,而不是CSS属性
- 是什么导致的'未捕获的类型错误:无法调用方法'animate'未定义的'错误
- animate方法函数执行意外延迟
- 当从外部方法调用时,RaphaelJS的animate不起作用
- .apply() 方法不适用于 jQuery .animate()
- 是switch case还是animate方法
- Jquery .animate()方法似乎不能与css top属性一起工作
- 与HTML事件属性(edge animate)中的onload方法相反
- 如何继续.animate方法,而不是在鼠标离开时重新启动它
- JQuery-3.0带有radius的animate方法在firefox中不起作用