简体   繁体   English

通过.animate()和.css()设置宽度之间的差异

[英]Discrepancy between setting width through .animate() and .css()

I was trying to reset the width of an element through JQuery (for which i set the width already - Similar to Toggle behaviour). 我试图通过JQuery重置元素的宽度(为此我已经设置了宽度-与Toggle行为类似)。 I experienced an interesting issue. 我遇到了一个有趣的问题。

If I do this, IT WORKS , ofcourse without any animation: 如果我这样做,那么它当然没有任何动画效果:

$(this).parents(".galbox_viewpic_cont:first").css("width","");

If I do this, IT DOES NOT WORK PROPERLY . 如果执行此操作,将无法正常工作 It sets the width to 100% of the entire document. 它将宽度设置为整个文档的100%。

$(this).parents(".galbox_viewpic_cont:first").animate({"width":""},function(){});

I also tried specifying auto but it also does not work. 我也尝试指定自动,但它也不起作用。

$(this).parents(".galbox_viewpic_cont:first").animate({"width":"auto"},function(){});

Please help me out with this. 这个你能帮我吗。 I want to have an animation and still want it to work. 我想要一个动画,但仍然希望它能正常工作。

You have to animate to numeric values, "" and "auto" are not numeric so jquery or any other library cannot interpolate the values from one frame to the other. 您必须为数字值设置动画,“”和“自动”不是数字,因此jquery或任何其他库无法将值从一帧插值到另一帧。

Setting the width to "" via the css method actually clears the width property, letting the element take the width according to the inherited properties. 实际上,通过css方法将width设置为“”会清除width属性,让元素根据继承的属性获取宽度。 Usually this means "" is the same as "auto". 通常,这意味着“”与“自动”相同。 You can check this in the web developer inspector in the browser. 您可以在浏览器的Web开发人员检查器中进行检查。

Alternative 1 选择1

If your scenario allows it, define a starting and ending width and use those values to animate. 如果您的方案允许,请定义开始和结束宽度,并使用这些值进行动画处理。 This should be a viable option if the width of the element does not depend on the width of the browser, its content or any other responsive behavior. 如果元素的宽度不依赖于浏览器的宽度,其内容或任何其他响应行为,则这应该是一个可行的选择。

// start
$(this).parents(".galbox_viewpic_cont:first").css({"width":"300px"});

// end
$(this).parents(".galbox_viewpic_cont:first").animate({"width":"600px"},function(){});

Alternative 2 选择2

You could also set the width via the .css method and set a css transition to the element. 您也可以通过.css方法设置宽度,并为该元素设置CSS过渡。 This way altering the width in any way will result in a smooth animation. 这种以任何方式改变宽度的方式都将产生平滑的动画。

.galbox_viewpic_cont:first{
    -webkit-transition: width 500ms ease-in;
    -moz-transition: width 500ms ease-in;
    transition: width 500ms ease-in;
}

// then via js
$(this).parents(".galbox_viewpic_cont:first").css({"width":"300px"});

// a bit later
$(this).parents(".galbox_viewpic_cont:first").css({"width":"600px"});

This results in an animated transition. 这将产生动画过渡。 However, setting .css to "auto" or "" won't animate it either. 但是,将.css设置为“ auto”或“”也不会为其设置动画。

Alternative 3 选择3

You could then automate the setting of the numeric start value like this. 然后,您可以像这样自动设置数字起始值。

// start
var initialWidth = $(this).parents(".galbox_viewpic_cont:first").css('width');

// then do your thing
// then animate to a target width
 $(this).parents(".galbox_viewpic_cont:first").animate({"width":"600px"},function(){});

// then to reset the animation, set it to the initial width
$(this).parents(".galbox_viewpic_cont:first").animate({"width":initialWidth+'px'},function(){});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM