JQuery Animate 只能运行一次
您好,我正在使用 JQuery 为我的页面上的 2 个 div 框设置动画,这第一次工作正常,但随后的时间我只是得到可见的框,没有任何动画效果,下面的代码是由一个名为 Buttonx 的按钮触发的。感谢您提前提供的任何帮助
$(document).ready(function(){
// lWhen document loaded
$("#buttonx").click(function() { DisplayUniDiv( 40,250,300 ) });
$('#UniDivHead').click(function() { CloseDiv() });
});
function DisplayUniDiv(top,width,height,color) {
dvHead = $('#UniDivHead') ;
dvBody = $('#UniDivBody') ;
// Display the Div Head
dvHead.css("visibility", "visible");
if (color) { color = "#" + color ; } else { color = "#0066cc" } ;
dvHead.css("background", color ) ;
dvHead.css("top", top + 'px' );
dvHead.css("width", width + 'px') ;
MarginLeft = (width / 2) ;
MarginLeft = "-" + MarginLeft ;
dvHead.css("marginLeft", MarginLeft)
dvHead.append("Close") ;
// Display the body
dvBody.css("visibility", "visible");
dvBody.css("background", '#CCC' ) ;
HeadHeight = dvHead.height() ;
dvBody.css("top", top + HeadHeight + 'px' );
dvBody.css("width", width + 'px') ;
dvBody.css("marginLeft", MarginLeft)
dvBody.animate({height:height},500);
}
function CloseDiv() {
dvHead = $('#UniDivHead') ;
dvBody = $('#UniDivBody') ;
dvHead.css("visibility", "hidden");
dvBody.css("visibility", "hidden");
}
Hi I am using JQuery to animate 2 div boxes on my page , This does work fine first time but subsequent times I just get visible boxes without any animation effects , The code is below which is triggered from a button called buttonx. Thanks for any help in advance
$(document).ready(function(){
// lWhen document loaded
$("#buttonx").click(function() { DisplayUniDiv( 40,250,300 ) });
$('#UniDivHead').click(function() { CloseDiv() });
});
function DisplayUniDiv(top,width,height,color) {
dvHead = $('#UniDivHead') ;
dvBody = $('#UniDivBody') ;
// Display the Div Head
dvHead.css("visibility", "visible");
if (color) { color = "#" + color ; } else { color = "#0066cc" } ;
dvHead.css("background", color ) ;
dvHead.css("top", top + 'px' );
dvHead.css("width", width + 'px') ;
MarginLeft = (width / 2) ;
MarginLeft = "-" + MarginLeft ;
dvHead.css("marginLeft", MarginLeft)
dvHead.append("Close") ;
// Display the body
dvBody.css("visibility", "visible");
dvBody.css("background", '#CCC' ) ;
HeadHeight = dvHead.height() ;
dvBody.css("top", top + HeadHeight + 'px' );
dvBody.css("width", width + 'px') ;
dvBody.css("marginLeft", MarginLeft)
dvBody.animate({height:height},500);
}
function CloseDiv() {
dvHead = $('#UniDivHead') ;
dvBody = $('#UniDivBody') ;
dvHead.css("visibility", "hidden");
dvBody.css("visibility", "hidden");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为在
close
函数中,您只是隐藏该元素(此处:dvBody.css("visibility", "hidden");
),但您应该将其高度设置为 0 。 喜欢:because in
close
function, you just hide the element (here:dvBody.css("visibility", "hidden");
) , but you should set its height to 0. like:我认为这可能是因为您第一次执行 DisplayUniDiv() 时,它实际上操纵了尺寸和其他因素,并使对象可见。
当您执行 CloseDiv() 时,它会使对象不可见。
当您第二次执行 DisplayUniDiv() 时,它确实使对象可见,但所有其他属性都已处于您尝试将它们设置为动画的状态。动画仍然“运行”,但没有任何作用。
如果您想拥有可见的第二个动画,除了隐藏它们之外,还可以尝试重置其余的对象属性。这可能会解决你的问题。
I think this might be that the first time you execute DisplayUniDiv(), it actually manipulates the dimensions and other factors, as well as make the objects visible.
When you execute CloseDiv(), it makes the objects invisible.
When you execute DisplayUniDiv() the second time, it does make the objects visible, but all the other properties are already in the state that you try to animate them to. The animation still "runs" but it doesn't have anything to do.
If you want to have a visible 2nd animation, try to reset the remaining object properties besides only hiding them. That might solve your problem.
这部分只工作一次:
因为它将遵循这个序列,这在第一次迭代后无效:
我已经压缩了你的代码一点,所以看看这是否有效:
基本上,当你隐藏你的元素时,设置
visibility :hidden;
大多数时候并不能解决问题。 jQuery 有.hide()
,它设置display: none;
。This part only works once:
As it will follow this sequence, which is invalid after the first iteration:
I've condensed your code a little, so see if this works:
Basically, when you were hiding your elements, setting
visibility: hidden;
doesn't do the trick most of the time. jQuery has.hide()
which setsdisplay: none;
.