gpt4 book ai didi

html - css ~ 选择器不会将样式应用于所有目标元素(z-index 到下一个/上一个标签)

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:14 25 4
gpt4 key购买 nike

我有以下纯 css 幻灯片,除了缩略图之外,我还尝试实现“下一个/上一个”按钮(两者都是指向 <input type="radio"> 的标签)。

缩略图是内部带有图像的标签,因此我为按钮创建了更多标签。问题是我无法弄清楚如何正确隐藏它们,除了选中的输入旁边的那些。

我试过下面的代码:

.slider input[name='slide_switch'] ~ label.next,
.slider input[name='slide_switch'] ~ label.previous
{
z-index: -1
}
.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
z-index: 1
}

那应该针对所有 <label class="next"><label class="previous">在 HTML 中,但由于某种原因它不起作用,我“可以看到”多个按钮,一个接一个,因为我给了它们 opacity: 0.6而且它们看起来不是半透明的。然而,最后两个显示正确的不透明度并且正在工作,如果我在最后一张图片中,我可以单击上一个按钮,它将带我到第 4 张图片,如果我单击下一张,它将带我到第 5 张(不幸的是,所有图片都会发生这种情况)。

我在这里工作:https://jsfiddle.net/5v6ajfup/

HTML代码:

<div class="slider">
<input type="radio" name="slide_switch" id="id1"/>
<label class="thumbnail" for="id1">
<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100"/>
</label>
<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg"/>
<label class="next" for="id2">></label>

<input type="radio" name="slide_switch" id="id2" checked="checked"/>
<label class="thumbnail" for="id2">
<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100"/>
</label>
<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg"/>
<label class="previous" for="id1">></label>
<label class="next" for="id3">></label>

<input type="radio" name="slide_switch" id="id3"/>
<label class="thumbnail" for="id3">
<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100"/>
</label>
<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg"/>
<label class="previous" for="id2">></label>
<label class="next" for="id4">></label>

<input type="radio" name="slide_switch" id="id4"/>
<label class="thumbnail" for="id4">
<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100"/>
</label>
<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg"/>
<label class="next" for="id5">></label>
<label class="previous" for="id3">></label>

<input type="radio" name="slide_switch" id="id5"/>
<label class="thumbnail" for="id5">
<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100"/>
</label>
<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg"/>
<label class="previous" for="id4">></label>
</div>

CSS代码:

* { margin: 0; padding: 0 }
body { background: #ccc }
.slider {
width: 640px;
position: relative;
padding-top: 320px;
margin: 20px auto;
box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);
}
.slider > img {
position: absolute;
left: 0; top: 0;
transition: all 0.5s;
display: block
}
.slider input[name='slide_switch'] {
display: none;
}
.slider label.next, .slider label.previous {
position: absolute;
left: 0; top: 0;
display: block;
font-size:3em;
opacity: 0.6;
cursor: pointer;
color: white;
}
.slider label.next { left: 560px; line-height: 320px }
.slider label.previous { left: 50px; line-height: 320px; transform: rotateY(180deg) }
.slider label.thumbnail {
margin: 18px 0 0 18px;
border: 3px solid #999;
float: left;
cursor: pointer;
transition: all 0.5s;
opacity: 0.6;
}
.slider label.thumbnail:hover { opacity: 0.8 }
.slider label.thumbnail img {
display: block;
}
.slider input[name='slide_switch']:checked + label.thumbnail {
border-color: #666;
opacity: 1;
}
.slider input[name='slide_switch'] ~ label.next,
.slider input[name='slide_switch'] ~ label.previous
{
z-index: -1
}
.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
z-index: 1
}
.slider input[name='slide_switch'] ~ img {
opacity: 0;
transform: scale(1.1);
}
.slider input[name='slide_switch']:checked + label.thumbnail + img {
opacity: 1;
transform: scale(1);
}

( Source )

最佳答案

这条规则

.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
z-index: 1
}

将针对 所有 label.next/label.previous input[name='slide_switch'],而且不仅是下一个 input[name='slide_switch'] 之前的立即数。

您需要使用它们的 for 属性定位每个 input[name='slide_switch'] 特定标签,或者使用 nth-of-type


根据评论更新

解决方案 1 - 首选

不是改变上面的 :checked 规则,而是将每组 input/image/thumb/arrow 包装在一个 div 中并改变这个规则的 .slider > img 选择器到 slider > div > img

Updated fiddle

堆栈片段

* { margin: 0; padding: 0 }
body { background: #ccc }
.slider {
width: 640px;
position: relative;
padding-top: 320px;
margin: 20px auto;
box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);
}
.slider > div > img {
position: absolute;
left: 0; top: 0;
transition: all 0.5s;
display: block
}
.slider input[name='slide_switch'] {
display: none;
}
.slider label.next, .slider label.previous {
position: absolute;
left: 0; top: 0;
display: block;
font-size:3em;
opacity: 0.6;
cursor: pointer;
color: white;
}
.slider label.next { left: 560px; line-height: 320px }
.slider label.previous { left: 50px; line-height: 320px; transform: rotateY(180deg) }
.slider label.thumbnail {
margin: 18px 0 0 18px;
border: 3px solid #999;
float: left;
cursor: pointer;
transition: all 0.5s;
opacity: 0.6;
}
.slider label.thumbnail:hover { opacity: 0.8 }
.slider label.thumbnail img {
display: block;
}
.slider input[name='slide_switch']:checked + label.thumbnail {
border-color: #666;
opacity: 1;
}
.slider input[name='slide_switch'] ~ label.next,
.slider input[name='slide_switch'] ~ label.previous
{
z-index: -1
}
.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
z-index: 1
}
.slider input[name='slide_switch'] ~ img {
opacity: 0;
transform: scale(1.1);
}
.slider input[name='slide_switch']:checked + label.thumbnail + img {
opacity: 1;
transform: scale(1);
}
<div class="slider">

<div>
<input type="radio" name="slide_switch" id="id1" />
<label class="thumbnail" for="id1">
<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100" />
</label>
<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" />
<label class="next" for="id2">></label>
</div>

<div>
<input type="radio" name="slide_switch" id="id2" checked/>
<label class="thumbnail" for="id2">
<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100" />
</label>
<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" />
<label class="previous" for="id1">></label>
<label class="next" for="id3">></label>
</div>

<div>
<input type="radio" name="slide_switch" id="id3" />
<label class="thumbnail" for="id3">
<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100" />
</label>
<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" />
<label class="previous" for="id2">></label>
<label class="next" for="id4">></label>
</div>

<div>
<input type="radio" name="slide_switch" id="id4" />
<label class="thumbnail" for="id4">
<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100" />
</label>
<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" />
<label class="next" for="id5">></label>
<label class="previous" for="id3">></label>
</div>

<div>
<input type="radio" name="slide_switch" id="id5" />
<label class="thumbnail" for="id5">
<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100" />
</label>
<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" />
<label class="previous" for="id4">></label>
</div>

</div>


解决方案2

替换这条规则

.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
z-index: 1
}

有了这个

.slider input#id1:checked ~ label[for="id2"],
.slider input#id2:checked ~ label[for="id1"],
.slider input#id2:checked ~ label[for="id3"],
.slider input#id3:checked ~ label[for="id2"],
.slider input#id3:checked ~ label[for="id4"],
.slider input#id4:checked ~ label[for="id3"],
.slider input#id4:checked ~ label[for="id5"],
.slider input#id5:checked ~ label[for="id4"]
{
z-index: 1;
}

关于html - css ~ 选择器不会将样式应用于所有目标元素(z-index 到下一个/上一个标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44678565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com