如何控制单击按钮时的动画功能
How to control animate function on button click?
我有两个名为"arrow"answers"inner"的div。我试图在单击div时控制animate slide函数,但很不幸。当用户停止单击"箭头"div后,快速单击该div时,问题会很明显。div仍在滑动。我在一个小延迟下设置了animate函数,但我仍然会遇到延迟。这是我的示例代码:
<script language="javascript" src="http://code.jquery.com/jquery-1.5.2.js"></script>
<script language="javascript">
$(document).ready(function() {
var out = 0;
$("#arrow").click(function(){
if(out==0)
{
$("#inner").animate({marginRight: "0px"}, 500 );
out=1;
}
else
{
$("#inner").delay(400).animate({marginRight: "-100px"}, 500 );
out=0;
}
});
});
</script>
<div style="background-color: rgb(204, 204, 204); height: 300px; width: 300px; overflow: hidden; position: relative;">
<div id="inner" style="height: 100px; width: 150px; background-color: rgb(0, 204, 102); float: right; margin-right:-150px;" >Form is here</div>
<div id="arrow" style="height: 100px; width: 50px; background-color: rgb(255, 0, 0); float: right; cursor: pointer; position: absolute; top: 0; right: 0;" >Arrow is here</div>
</div>
只需更改代码
$("#inner").animate({marginRight: "0px"}, 500 );
至
$("#inner").stop(true, true).animate({marginRight: "0px"}, 500 );
和
$("#inner").animate({marginRight: "-100px"}, 500 );
至
$("#inner").stop(true, true).animate({marginRight: "-100px"}, 500 );
例如,请参阅以下链接:http://jsfiddle.net/UAYTw/1/
您也可以根据自己的选择使用$("#inner").stop(true,false).animate()代替$("#inner"。
使用拉维的代码道具-在我看来切换更干净
演示
$(document).ready(function() {
$("#arrow").toggle(
function(){
$("#inner").stop(true, true).animate({marginRight: "0px"}, 500 );
},
function(){
$("#inner").stop(true, true).animate({marginRight: "-100px"}, 500 );
}
);
});
您可以使用.animate().stop()
方法来停止动画。
演示:http://jsfiddle.net/YKn7c/
这可能会有所帮助:
$("#button").hover(
function () {
$("#effect").stop().animate({ opacity: '1', height: '130' }, 300);
},
function () {
$("#effect").stop().animate({ opacity: '0', height: '130' }, 300);
}
);
编辑:
如果你不想被关闭:
$("#button").hover(
function () {
$("#effect").stop().animate({ opacity: '1', height: '130' }, 300);
},
null
);
相关文章:
- 使用JavaScript使按钮做出反应(添加动画)
- 在jquerymobile中设置按钮css属性的动画
- bootstrap在类更改时动画按钮样式
- 如何使用Javascript创建带有精灵表的动画按钮
- 如何实现 ladda-bootstrap 按钮加载动画
- 从导航按钮 JavaScript 中删除动画
- 使用动画时Jquery后退按钮不起作用
- scrollTop按钮没有't设置动画
- 用JavaScript制作一个按钮的动画,从按钮到屏幕中心弹出一个模式对话框
- 如果不使用HTML按钮,就无法通过编程方式触发SVG动画
- Jquery将三个按钮动画化以打开关闭开关
- 如何重新创建移动Apple.com菜单按钮动画
- 如何获得内置进度条的工作按钮动画
- 停止最后一张幻灯片上的jQuery按钮动画
- 视频播放暂停按钮动画
- 谷歌I'm感觉幸运按钮动画javascript
- 我如何用javascript创建png按钮动画
- 多个同时按钮动画
- HTML Web中没有材质按钮动画
- 使用AngularJS制作按钮动画ng重复