简体   繁体   English

隐藏元素时show()不起作用

[英]show() doesn't work when element is hidden

In this jsfiddle the objective is to show the rectangle when the mouse hovers over it, else hide it. 在此jsfiddle中 ,目标是在鼠标悬停在矩形上时显示该矩形,否则将其隐藏。 The problem is that once the rectangle is hidden, it doesn't show up anymore on hover. 问题在于,一旦矩形被隐藏,则悬停时将不再显示该矩形。 Any ideas? 有任何想法吗?

This is the code snippet: 这是代码片段:

 var paper = Raphael("canvas", 200, 200); var r = paper.rect(5,5, 20, 20); r.attr({ "fill" : "red" }); r.hover( function() { this.show(); // mouse hovers in }, function() { this.hide(); // mouse hovers out }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.2/raphael-min.js"></script> <div id="canvas"></div> 

When you hide an element, You're not technically hovered on that element anymore. 隐藏元素时,从技术上讲,您不再需要在该元素上徘徊。 That's because hide() sets display:none 那是因为hide()设置display:none

You could make it transparent instead of hide it by using opacity 您可以使它透明而不是通过opacity将其隐藏

http://jsfiddle.net/gnbkqhus/1/ http://jsfiddle.net/gnbkqhus/1/

 var paper = Raphael("canvas", 200, 200); var r = paper.rect(5,5, 20, 20); r.attr({ "fill": "red", "opacity" : "0" }); r.hover( function() { r.attr({ "opacity" : "1" }); }, function() { r.attr({ "opacity" : "0" }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.2/raphael-min.js"></script> <div id="canvas"></div> 

You cannot hover over an invisible element or an undisplayed element. 您不能将鼠标悬停在不可见元素或未显示元素上。 You can hover over a visible element and then use that to show a different previously hidden element. 您可以将鼠标悬停在可见元素上,然后使用它来显示其他先前隐藏的元素。 Or you can hover over a transparent element and make it opaque. 或者,您可以将鼠标悬停在透明元素上并使其不透明。 So instead of 所以代替

r.hover(
    function() {
        this.show();  // mouse hovers in
    },
    function() {
        this.hide(); // mouse hovers out
    }
);

you need to do 你需要做

r.hover(
    function() {
        r.attr({ "opacity" : "1" });
    },
    function() {
        r.attr({ "opacity" : "0" });
    }
);

One thing to keep in mind, calling hide() sets the "display" value to "none". 要记住的一件事是,调用hide()会将“ display”值设置为“ none”。 And "display:none" elements do not occupy any space on the screen. 而且“ display:none”元素不会在屏幕上占据任何空间。 Therefore any mouse action do not work on them because they have zero height and width. 因此,任何鼠标操作都不会对其起作用,因为它们的高度和宽度为零。

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

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