简体   繁体   English

如果display = none,则删除类

[英]Remove class if display = none

How to add and remove class with the condition of CSS display. 如何在CSS显示条件下添加和删除类。 For example, I have some Div that can be hidden and shown. 例如,我有一些Div可以隐藏和显示。 I want that, if the div is hidden with the display:none; 我想要的是,如果div用display:none;隐藏display:none; , then the class of the div is removed. ,然后删除div的类。

But if the div is shown with the display:block; 但是,如果div与display:block;一起display:block; , I want to add a class to the div. ,我想向div添加一个类。

Here is what I have been trying: 这是我一直在尝试的方法:

  $(document).ready(function(){
       if($('.bigPicture').css('display') == 'block')
            {$('.bigPicture').find('div').addClass('easyzoom easyzoom--overlay');};

       if($('.bigPicture').css('display') == 'none')
            {$('.bigPicture').find('div').removeClass('easyzoom easyzoom--overlay');}
  });



EXPLANATION 说明

I have multiple slideshows in one pages. 我一页有多个幻灯片。 Not all of the slideshow is going to be shown in the page, one of them will only show if the link to that slideshow is clicked. 并非所有幻灯片都将显示在页面中,其中一个只有在单击指向该幻灯片的链接时才会显示。 It sames like auto hide and show function. 就像自动隐藏和显示功能一样。 If one slideshow is shown, then other slideshows are hidden. 如果显示一个幻灯片,则其他幻灯片将被隐藏。

Each slideshow has their own thumbnails to control them. 每个幻灯片都有自己的缩略图来控制它们。

The problem is all of the slideshow has no ID and has the same class, while all the slideshow is run with the same script. 问题是所有幻灯片都没有ID,并且具有相同的类,而所有幻灯片都使用相同的脚本运行。

If the thumbnail in the second slideshow I click, the slideshow doesn't move. 如果单击第二个幻灯片中的缩略图,则幻灯片不会移动。 And I realize it slides only the first slideshow. 而且我意识到它只会幻灯片第一张幻灯片。

SO, the solution is I have to remove the class of the slideshow. 所以,解决方案是我必须删除幻灯片的类。

Try is visible: 尝试可见:

$(document).ready(function(){
    if($('.bigPicture').is(':visible')){
        $('.bigPicture').find('div').addClass('easyzoom easyzoom--overlay');
    } else {
        $('.bigPicture').find('div').removeClass('easyzoom');
    }
});

This is what you are trying to do? 这是您要做什么?

If so, it's easy to convert it to jquery syntax instead. 如果是这样,则很容易将其转换为jquery语法。

It would be easier if you had provided a jsFiddle of your problem. 如果您为问题提供了jsFiddle,那会更容易。

CSS 的CSS

.bigPicture {
    width: 50px;
    height: 20px;
    border-style: solid;
    border-width: 2px;
}
.hidden {
    display: none;
}
.easyzoom {
    background-color: red;
}

HTML 的HTML

<div class="bigPicture">
    <div>One</div>
</div>
<div class="bigPicture hidden">
    <div class="easyzoom easyzoom--overlay">Two</div>
</div>
<div class="bigPicture hidden">
    <div class="easyzoom easyzoom--overlay">Three</div>
</div>

Javascript Java脚本

document.addEventListener('load', function () {
    Array.prototype.forEach.call(document.querySelectorAll('.bigPicture'), function (bigPicture) {
        var div = bigPicture.querySelector('div:first-of-type');

        if (div) {
            if (window.getComputedStyle(bigPicture).display === 'none') {
                div.classList.remove('easyzoom', 'easyzoom--overlay');
            } else {
                div.classList.add('easyzoom', 'easyzoom--overlay');
            }
        }
    });
}, true);

On jsFiddle jsFiddle上

I think your code is somewhat working. 我认为您的代码有些正常。 Here is a fiddle where I have shown and hidden on the click of the buttons. 单击此处显示和隐藏的小提琴在这里。

Code Snippet: 代码段:

$(document).on('click','#addBigPicture',function(){
    $('.bigPicture').css('display','block');
    if($('.bigPicture').css('display') == 'block')
      {$('.bigPicture').find('div').addClass('easyzoom easyzoom--overlay');};
});

$(document).on('click','#hideBigPicture',function(){
    $('.bigPicture').css('display','none');
    if($('.bigPicture').css('display') == 'none')
         {$('.bigPicture').find('div').removeClass('easyzoom easyzoom--overlay');}
});

The if loops are as desired by you. if循环是您所需要的。 Hope it helps!! 希望能帮助到你!!

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

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