JQuery Animate 只能运行一次

发布于 2024-12-03 08:21:46 字数 1299 浏览 1 评论 0原文

您好,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十雾 2024-12-10 08:21:46

因为在 close 函数中,您只是隐藏该元素(此处: dvBody.css("visibility", "hidden"); ),但您应该将其高度设置为 0 。 喜欢:

dvBody.css({"visibility":"hidden", height:0});

because in close function, you just hide the element (here: dvBody.css("visibility", "hidden"); ) , but you should set its height to 0. like:

dvBody.css({"visibility":"hidden", height:0});
乱了心跳 2024-12-10 08:21:46

我认为这可能是因为您第一次执行 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.

怼怹恏 2024-12-10 08:21:46

这部分只工作一次:

if (color) { color = "#" + color ; } else { color = "#0066cc"  } ;  
    dvHead.css("background", color ) ;

因为它将遵循这个序列,这在第一次迭代后无效:

ABCDEF -> #ABCDEF -> ##ABCDEF -> ###...

我已经压缩了你的代码一点,所以看看这是否有效:

$(document).ready(function() {
  $('#buttonx').click(function() {
    DisplayUniDiv(40, 250, 300)
  });

  $('#UniDivHead').click(function() {
    $('#UniDivHead, '#UniDivBody').hide();
  });
});



function DisplayUniDiv(top, width, height, color) {
  $('#UniDivHead').show();

  if (color.index('#') == -1) {
    color = '#' + color;
  } else {
    color = '#0066cc'
  };
  $('#UniDivHead').css('background', color);
  $('#UniDivHead').css('top', top);
  $('#UniDivHead, #UniDivBody').width(width);
  $('#UniDivHead').css('margin-left', MarginLeft)
  $('#UniDivHead').append('<span>Close</span>');

  $('#UniDivBody').show();
  $('#UniDivBody').css('background', '#CCC');
  $('#UniDivBody').css('top', top + $('#UniDivHead').height());
  $('#UniDivBody').css('margin-left', -(width / 2))
  $('#UniDivBody').animate({height: height}, 500);
}

基本上,当你隐藏你的元素时,设置 visibility :hidden; 大多数时候并不能解决问题。 jQuery 有 .hide(),它设置 display: none;

This part only works once:

if (color) { color = "#" + color ; } else { color = "#0066cc"  } ;  
    dvHead.css("background", color ) ;

As it will follow this sequence, which is invalid after the first iteration:

ABCDEF -> #ABCDEF -> ##ABCDEF -> ###...

I've condensed your code a little, so see if this works:

$(document).ready(function() {
  $('#buttonx').click(function() {
    DisplayUniDiv(40, 250, 300)
  });

  $('#UniDivHead').click(function() {
    $('#UniDivHead, '#UniDivBody').hide();
  });
});



function DisplayUniDiv(top, width, height, color) {
  $('#UniDivHead').show();

  if (color.index('#') == -1) {
    color = '#' + color;
  } else {
    color = '#0066cc'
  };
  $('#UniDivHead').css('background', color);
  $('#UniDivHead').css('top', top);
  $('#UniDivHead, #UniDivBody').width(width);
  $('#UniDivHead').css('margin-left', MarginLeft)
  $('#UniDivHead').append('<span>Close</span>');

  $('#UniDivBody').show();
  $('#UniDivBody').css('background', '#CCC');
  $('#UniDivBody').css('top', top + $('#UniDivHead').height());
  $('#UniDivBody').css('margin-left', -(width / 2))
  $('#UniDivBody').animate({height: height}, 500);
}

Basically, when you were hiding your elements, setting visibility: hidden; doesn't do the trick most of the time. jQuery has .hide() which sets display: none;.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文