所以我只是尝试调试以下错误:
<script>
$(function() {
div = $('<div />');
div.text('test');
div.hide(0);
div.appendto('body');
});
</script>
Run Code Online (Sandbox Code Playgroud)
当我执行此操作时,显示DIV.尽管我隐藏(在我们添加到DOM之前)它.以下代码:
<script>
$(function() {
div = $('<div />');
div.text('test');
div.hide();
div.appendto('body');
});
</script>
Run Code Online (Sandbox Code Playgroud)
确实隐藏了DIV.
当我进入jQuery的hide函数源代码时,我看到了:
hide: function( speed, easing, callback ) {
if ( speed || speed === 0 ) {
return this.animate( genFx("hide", 3), speed, easing, callback);
} else {
for ( var i = 0, j = this.length; i < j; i++ ) {
var display = jQuery.css( this[i], "display" );
if ( display !== "none" ) {
jQuery.data( this[i], "olddisplay", display );
}
}
// Set the display of the elements in a second loop
// to avoid the constant reflow
for ( i = 0; i < j; i++ ) {
this[i].style.display = "none";
}
return this;
}
},
Run Code Online (Sandbox Code Playgroud)
为什么要检查
if ( speed || speed === 0 ) {
Run Code Online (Sandbox Code Playgroud)
特别是,速度=== 0.我认为,当速度为零时.可以完全跳过animate函数,只需添加display:none; 对元素.
PS.我假设因为我们将一个dom中不存在的元素赋予animate函数,它就会失败.没有什么可以动画的.因此,动画功能实际上并不隐藏DIV.
感谢前进中的任何答案:)正在搜索大约10分钟的正确语法(hide())而不是hide(0))但仍然发现它不合逻辑.因此问题:)
如果你想跳过动画,你只需要.hide()...通过允许.hide(0),你可以在 没有持续时间的情况下排队 ......所以它为这个功能增加了很多实用工具.
例如:
$(".myElem").delay(2000).hide(0);
Run Code Online (Sandbox Code Playgroud)
在哪里这不起作用:
$(".myElem").delay(2000).hide();
Run Code Online (Sandbox Code Playgroud)
....因为.hide()没有参数不是任何fx队列的一部分,并且会立即发生.
| 归档时间: |
|
| 查看次数: |
1580 次 |
| 最近记录: |