简体   繁体   English

带有CSS值的jQuery / javascript if语句

[英]jQuery/javascript if-statement with css values

I'm slightly new to the if statements and I'm working on an animation where: 我对if语句有点陌生,并且正在制作动画,其中:

If <div id="headlights"> displays "none" in the css (which is constantly fading in and out), I want <div id="speech-bubble-dark"> to fade in. 如果<div id="headlights">在css中显示为“ none”(不断淡入和淡出),我希望<div id="speech-bubble-dark">淡入。

And when the <div id="headlights"> displays "block" in the css, I want <div id="speech-bubble-sun"> to fade in. <div id="headlights">在CSS中显示“阻止”时,我希望<div id="speech-bubble-sun">淡入。

function bubbleTest(){
if($("#headlights").css("display") == "none" ) {
    $("#speech-bubble").fadeOut('600');
    $("#speech-bubble-sun").fadeOut('600', function(){
        $("#speech-bubble-dark").fadeIn('600');
    });

}   else{
        $("#speech-bubble").fadeOut('600');
        $("#speech-bubble-dark").fadeOut('600', function(){
            $("#speech-bubble-sun").fadeIn('600');              
        });

    }
}

I've tested it and the fades do not work, and dependent on the display condition being "none" or "block" it displays a different div. 我已经对其进行了测试,并且淡入淡出功能不起作用,并且取决于显示条件为“无”或“阻止”,它会显示不同的div。

What would you suggest I do? 你会建议我做什么?

I think you are looking for this. 我想您正在寻找这个。 Try it 试试吧

function bubbleTest(){
   if(!$("#headlights").is(":visible")){//This will check for headlights is visible or not
      $("#speech-bubble").fadeOut(600);
      $("#speech-bubble-sun").fadeOut(600, function(){
        $("#speech-bubble-dark").fadeIn(600);
      });
   }
   else{
      $("#speech-bubble").fadeOut(600);
      $("#speech-bubble-dark").fadeOut(600, function(){
        $("#speech-bubble-sun").fadeIn(600);              
      });
}

The javascript originally posted: 最初发布的javascript:

function bubbleTest(){
if($("#headlights").css("display") == "none" ) {
    $("#speech-bubble").fadeOut('600');
    $("#speech-bubble-sun").fadeOut('600', function(){
        $("#speech-bubble-dark").fadeIn('600');
    });

}   else{
        $("#speech-bubble").fadeOut('600');
        $("#speech-bubble-dark").fadeOut('600', function(){
            $("#speech-bubble-sun").fadeIn('600');              
        });

    }
}
bubbleTest();

surprisingly works as intended, here is proof: http://codepen.io/anon/pen/zoNevZ 意外地按预期工作,这是证明: http : //codepen.io/anon/pen/zoNevZ

The problem must have been how I was calling the function, which I did not provide at the time (5 years ago). 问题一定是我如何调用该函数,而当时(5年前)我没有提供。

Try replacing this: 尝试替换此:

$("#headlights").css("display") == "none"

with this: 有了这个:

if (!$("#headlights").is(':visible'))

:visible is a jQuery "selector" (read the docs on them if you haven't, they're highly informative). :visible是jQuery的“选择器”(如果没有,请阅读文档,它们非常有用)。 You can use the is() method to do things like check to see if the visible selector is true. 您可以使用is()方法执行诸如检查可见选择器是否为true的操作。 Likewise, you can do things like this for form elements: 同样,您可以对表单元素执行以下操作:

if ($('#optInCheckbox').is(':checked'))

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

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