简体   繁体   English

使用JavaScript隐藏基于CSS条件的文本

[英]Hide text based on CSS conditionals with JavaScript

Is there a way to use the if statement of JavaScript to hide CSS elements based on other CSS styles? 有没有一种方法可以使用JavaScript的if语句隐藏基于其他CSS样式的CSS元素? I'm working in SharePoint and I'm applying some CSS codes to an image I have with some text with background in top of it. 我正在SharePoint中工作,并且正在将一些CSS代码应用于图像中带有背景文本的图像。 This image has a hover/animation effect and I would like to hide the text and background when the hover effect begins. 该图像具有悬停/动画效果,当悬停效果开始时,我想隐藏文本和背景。 Something like if(image3.opacity="1") then(h2, h2 span, h2 span.spacer)=hide . 类似于if(image3.opacity="1") then(h2, h2 span, h2 span.spacer)=hide I'm a total newbie in javascript, so I would really appreciate your help! 我是javascript的新手,非常感谢您的帮助!

the CSScode I've made so far is the next one: 我到目前为止制作的CSScode是下一个:

#image3 {
opacity: 0.4;
filter: alpha(opacity=40);
box-shadow: 0px 0px 15px grey;
alt: missing;
    transition-property: opacity;
    transition-duration: .2s;
}
#image3:hover {
    opacity: 1.0;
    filter: alpha(opacity=100); 
}
.image { 
   position: relative; 
   width: 100%;
}
h2 { 
   position: absolute; 
   bottom: 50px; 
   left: 5px; 
   width: 100%; 
}
 h2 span { 
   color: white; 
   font: bold 24px/45px Helvetica, Sans-Serif; 
   letter-spacing: 0px;  
   background: rgb(0, 0, 0);
   background: rgba(0, 0, 0, 0.6);
   padding: 9px; 
}
h2 span.spacer {
   padding:0 5px;
   background: rgba(0, 0, 0, 0);
}

You could use just CSS, depending on how your elements are arranged in the DOM. 您可以只使用CSS,具体取决于元素在DOM中的排列方式。 Assuming here that the h2 is a sibling of #image3 : 假设h2#image3的同级兄弟:

#image3:hover + h2,
#image3:hover + h2 span,
#image3:hover + h2 span.spacer {
    display: none;
}

If you want the elements to fade out instead of just disappear, you could use opacity and transition: 如果您希望元素淡出而不是消失,则可以使用不透明度和过渡:

#image3 + h2,
#image3 + h2 span,
#image3 + h2 span.spacer {
    opacity: 1;
    transition: opacity 150ms ease 1s; /* 1s delay */
}

#image3:hover + h2,
#image3:hover + h2 span,
#image3:hover + h2 span.spacer {
    opacity: 0;
}

If you are able to get a hold of and attach the JQuery library, the following should work. 如果您能够掌握并附加JQuery库,则应该可以进行以下操作。

if ($("#image3").css("opacity") == "1") {
    $("h2").hide();
    $("h2 span").hide();
    $("h2 span.spacer").hide();
}

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

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