How to write - If Visible selector?
Not sure how to write this;
If .sub1 is visible, .homepage fadeTo .25
Sub1 has a FadeIn and I want the homepage opacity to drop to .25 if the sub1 is open?
I have this;
$("#cat").click(function(){
if ($('.sub1').is(':visible') ) {
$(".homepage").fadeTo(500, .25);}
else {
$(".homepage").fadeTo(500, 1);
}
The actual website I am making > Website Mockup > Clicking Categories fade开发者_如何学运维s in the sub menu and makes homepage opacity 25%, clicking categories again, makes submenu fade Out making homepage 100%.....But clicking categories > Fashion > Mens Fashion > Smart, Brings up 'Mens Smart Fashion div" but clicking categories again fades the Mens Smart fashion div out, and brings .sub1 back, but the .homepage is 100% and not 25% when the .sub1 is open
You can filter the visible .sub1
and check the length, this may not be so great if there are multiple .sub1's
, maybe add a little specificity to the selector.
This technique will filter out elements with css visibility:hidden
or opacity: 0
for the first .sub1
(':eq(0)'
). If the length is 0 the condition will return false.
if($('.sub1:eq(0):visible').length) {
//.homepage fade to .25
}
Revised:
$("#cat").click(function(){
if ($('.sub1').css('opacity') == .25 ) {
$('.homepage').fadeTo(500, 1);
} else {
$('.homepage').fadeTo(500, .25);
}
});
I think the reason the it would not fade back in is because your condition was checking for opacicty:0 which you never reached when fading to .25. You should set the condition to check a number .25
to avoid checking multiple strings such as '.25'
or '0.25'
. Firefox reports '0.25'
.
精彩评论