简体   繁体   English

如何在CSS中检查条件?

[英]How to check conditional in CSS?

I have a problem when using Master Slider. 使用主滑块时出现问题。

It auto generates a lot of classes. 它会自动生成很多类。

In my case, I load video and picture into the slide. 就我而言,我将视频和图片加载到幻灯片中。

In the code I check with: 在代码中,我检查:

<?php if($item->type == 1): ?>   <!-- is Video -->
    <img class="ms-thumb video_slider" ... />
?>
<?php if($item->type == 2): ?>   <!-- is Picture -->
    <img class="ms-thumb picture_slider" ... />
    <div class="picturediv"></div> <!-- I tried to add manual element like that to assign instead of using: `ms-thumb-ol` but it auto remove when slide is generate -->
?>

It will be generated structure like this: 它将生成如下结构:

            div.ms-thumb-frame
                div.ms-thumb video_slider 
                div.ms-thumb-ol

            div.ms-thumb-frame
                div.ms-thumb picture_slider 
                div.ms-thumb-ol

I want to add a button play into that with code: 我想在代码中添加一个按钮播放:

div.ms-thumb-frame {
    position: relative;
}
div.ms-thumb-frame div.ms-thumb-ol {
    position: absolute;
    display: block;
    background: url(http://www.workbooks.com/sites/default/files/image/play-button-crm-homepage2.png);
    height: 55px;
    width: 57px;
    top: 0;
    left:0;
    right:0;
    bottom:0;
    margin:auto;
}
div.ms-thumb-frame div.ms-thumb-ol:hover {
    background: url(http://www.workbooks.com/sites/default/files/image/play-button-crm-homepage-11.png);
}

You can see, I style only element ms-thumb-ol . 您可以看到,我仅设置了元素ms-thumb-ol样式。

This element is same in another element. 此元素在另一个元素中相同。

Is there any method to check in div.ms-thumb picture_slider and div.ms-thumb-ol concurrent 有什么方法可以同时检查div.ms-thumb picture_sliderdiv.ms-thumb-ol

If you want to edit the ms-thumb-ol only, you can use the css sibling combinator, since it is next to div.ms-thumb picture_slider . 如果只想编辑ms-thumb-ol ,则可以使用css同级组合器,因为它位于div.ms-thumb picture_slider旁边。 So, to modify it, add this selector for your css: 因此,要对其进行修改,请为您的CSS添加以下选择器:

div.ms-thumb picture_slider + ms-thumb-ol{
//Whatever code you want that specific button to have but not the other button
}

What the + combinator does is that it checks if the div.ms-thumb picture_slider class is followed by ms-thumb-ol . +组合器的作用是检查div.ms-thumb picture_slider类后是否跟随ms-thumb-ol If it is, then apply styling. 如果是这样,则应用样式。

Edit: 编辑:

To make it simpler, change your code from what you have, to: 为简化起见,请将代码从现有内容更改为:

div.ms-thumb-frame + div.ms-thumb-ol {
position: absolute;
display: block;
background: url(http://www.workbooks.com/sites/default/files/image/play-button-crm-homepage2.png);
height: 55px;
width: 57px;
top: 0;
left:0;
right:0;
bottom:0;
margin:auto;
}


div.ms-thumb-frame + div.ms-thumb-ol:hover {
background: url(http://www.workbooks.com/sites/default/files/image/play-    button-crm-homepage-11.png);
}

As long as your HTML is set up the way you showed in your question, it will work. 只要您以问题中显示的方式设置HTML,它就可以工作。

Notice the only difference is the sibling combinator + . 注意,唯一的区别是兄弟组合器+

If you want to style only div.ms-thumb-ol that comes right after div.ms-thumb.picture_slider element inside div.ms-thumb-frame you can use CSS Adjacent sibling combinator like that: 如果你想样式仅div.ms-thumb-ol那之后来到div.ms-thumb.picture_slider内部元件div.ms-thumb-frame ,你可以使用CSS相邻兄弟组合子这样的:

div.ms-thumb-frame div.ms-thumb.picture_slider + div.ms-thumb-ol {
    /* ... */
}

If you have only two types of items...use ternary operator like this 如果您只有两种类型的项目...使用像这样的三元运算符

$class= ($item->type == 1)?'ms-thumb video_slider':'ms-thumb picture_slider';

And use $class in img tag as below.. 并在img标签中使用$class ,如下所示。

<img class="<?php echo $class;?>" ... />

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

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