简体   繁体   English

使用javascript setTimeout()设置z-index

[英]Set z-index with javascript setTimeout()

What I'm trying to do is to have 4 expandable divs overlapping each other with transition set in css for 0.6s. 我想做的是让4个可扩展的div相互重叠,并在CSS中设置0.6s的过渡。 Because the expanding lasts for 0.6s I'd like to have the expanded div lose it's higher z-index after it's done collapsing, otherwise it looks silly. 因为扩展持续时间为0.6s,所以我希望扩展后的div崩溃后会丢失较高的z-index,否则它看起来很愚蠢。 However, it doesn't work, the z-index remains unchanged. 但是,它不起作用,z索引保持不变。 It's probably somethings stupid, but I just can't find it. 这可能有点愚蠢,但是我找不到它。 Thanks! 谢谢!

<div class="wrapper" style="position: relative;">
<div id="one" style="position: absolute;" onmouseover="
this.style.height='250px';
this.style.zIndex='1';
" onmouseout="
this.style.height='50px';
setTimeout(function() {this.style.zIndex='0';},600);
">blahblahblahblah</div>
<div id="two" style="position: absolute;" onmouseover="
this.style.height='250px';
this.style.zIndex='1';
" onmouseout="
this.style.height='50px';
setTimeout(function() {this.style.zIndex='0';},600);
">blahblahblahblah</div>
</div>

Rather than finding the bug in your code, perhaps it would be useful to discuss how you could find it yourself. 与其找到代码中的错误,不如讨论自己如何找到它,可能会很有用。 The first step is to open your devtools window (F12) and open the console (press the little icon that looks like a greater than sign with three horizontal bars to the right). 第一步是打开devtools窗口(F12)并打开控制台(按看起来像一个大于号的小图标,并在右边带三个水平条)。 When you run your program you are likely to see the following: 运行程序时,您可能会看到以下内容:

TypeError: Cannot set property 'zIndex' of undefined

The debugger will stop at the line 调试器将在该行停止

setTimeout(function() {this.style.zIndex='0';},600);

Hmmm. Apparently this.style is undefined. 显然, this.style是未定义的。 How could that be? 怎么可能 Go into the console (which is in the context in which the error occurred, in other words, inside the function you passed to setTimeout) and type 进入控制台(在发生错误的上下文中,换句话说,在传递给setTimeout的函数内部)并键入

this.style

and indeed you will see that it is undefined. 实际上,您会看到它是未定义的。 Why is that? 这是为什么? Well, what is this ? 那么,什么是this

this

and you will see something like 你会看到类似

Window {top: Window, window: Window, location: Location, ...}

Why would this be the window instead of what I think it should be? 为什么this是窗口,而不是我认为的窗口? Well, this in general is set to window for a function called without an explicit this , as in elt.set_style() . 好吧,通常this设置为不带显式this的函数的window ,如elt.set_style() That's what's happening here. 这就是这里发生的事情。

How can I set this within a function that is called by the system, such as the one passed to setTimeout ? 如何在系统调用的函数(例如传递给setTimeout的函数)中设置this函数? You can do what @Satpal recommended, and store the value of this outside the function under another name such as self , and explicitly refer to that. 你可以做什么@Satpal建议,并存储的值this另一个名字外的功能,如self ,并明确提到这一点。 Or, you could bind the function to this , as in 或者,您可以将函数绑定到this ,如

function timeout_func(){
    this.style.zIndex=0;
}
setTimeout(timeout_func.bind(this));

In addition to using the console to see error messages and type in things to be evaluated, you'll find the "scope variables" and "watch expressions" windows very useful. 除了使用控制台查看错误消息并输入要评估的内容外,您还将发现“作用域变量”和“监视表达式”窗口非常有用。

In general, problems such as this can be solved easily using devtools. 通常,使用devtools可以轻松解决此类问题。 If you don't know what it is, Google Chrome devtools (start off by hitting F12). 如果您不知道它是什么,请使用Google Chrome devtools(首先按F12键)。 Not using this is literally like programming in the dark. 实际上,不使用它就像在黑暗中编程。

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

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