简体   繁体   English

可见性if语句:如果css display不是none,则执行

[英]Visability if statement: if css display is not none, do

I have this Javascript function and it does not work. 我有这个Javascript函数,它不起作用。 Got no errors from brackets. 括号内没有错误。 I am aware, that this also can be done with visibility, but in my case this seems better and I want to stay with it if that is possible. 我知道,这也可以通过可见性完成,但就我而言,这似乎更好,如果可能的话,我希望继续使用它。

I have three buttons and by hovering each, the matching div will fadeIn (.one, .two, .three). 我有三个按钮,通过将它们悬停在每个按钮上,匹配的div将淡入(.one,.two,.three)。 That code (not shown here) already works great. 该代码(此处未显示)已经很好用。

Now I want to show .hello, when all three divs are faded in (aka display is not 'none' anymore like I wrote in the CSS file, because Javascript changed that by fading them in, right?). 现在,我想显示.hello,这时所有三个div都淡入(aka显示不再像我在CSS文件中写的那样“消失”,因为Javascript通过将它们淡入来改变了它,对吗?)。

CSS: CSS:

.hello {
    display: none; }

Javascript: 使用Javascript:

jQuery(function() {

    if (jQuery('.one').css('display') !== ('none') &&
        jQuery('.two').css('display') !== ('none') &&
        jQuery('.three').css('display') !== ('none')) {

            jQuery('.hello').fadeIn();
        }
});

Is something wrong with that? 这有什么问题吗?

I'm sure there is a better solution but it came to my mind at first. 我敢肯定有更好的解决方案,但是起初我想到了。 The function should be executed on change not once. 该功能不应在更改时执行一次。

 //you did this part already. $("#1st").hover(function(){ $(".one").fadeIn(); }); $("#2nd").hover(function(){ $(".two").fadeIn(); }); $("#3rd").hover(function(){ $(".three").fadeIn(); }); //checks when hover $("#1st,#2nd,#3rd").hover(function(){ // same code if (jQuery('.one').css('display') !== ('none') && jQuery('.two').css('display') !== ('none') && jQuery('.three').css('display') !== ('none')) { jQuery('.hello').fadeIn(); } }) 
 .one,.two,.three,.hello{ display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" id="1st" value="1"> <input type="button" id="2nd" value="2"> <input type="button" id="3rd" value="3"> <br><br> <div class="one">1.content</div> <div class="two">2.content</div> <div class="three">3.content</div> <br><br> <div class="hello">Hello!</div> 

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

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