我的问题是,我如何仅使用CSS 来完成此操作?
(function(lastimg) {
document.querySelector("#img-select").addEventListener('click', function(e){
if (e.target.tagName.toLowerCase() == 'input') {
if (lastimg == e.target) {
e.target.checked = false;
lastimg = null;
} else {
lastimg = e.target;
}
}
});
}());
.container {
display: flex;
flex-wrap: wrap;
max-width: 660px;
}
.container > label {
flex: 1;
flex-basis: 33.333%;
}
.container > div {
flex: 1;
flex-basis: 100%;
}
.container label img {
display: block;
margin: 0 auto;
}
.container input, .container input ~ div {
display: none;
padding: 10px;
}
.container #img1:checked ~ #img1txt,
.container #img2:checked ~ #img2txt,
.container #img3:checked ~ #img3txt,
.container #img4:checked ~ #img4txt {
display: block;
}
<div id="img-select" class="container">
<input id="img1" type="radio" name="img-descr">
<input id="img2" type="radio" name="img-descr">
<input id="img3" type="radio" name="img-descr">
<label for="img1">
<img src="http://lorempixel.com/200/200/food/1/" alt="">
</label>
<label for="img2">
<img src="http://lorempixel.com/200/200/food/6/" alt="">
</label>
<label for="img3">
<img src="http://lorempixel.com/200/200/food/8/" alt="">
</label>
<div id="img1txt">
<div>Recipe nr 1</div>
</div>
<div id="img2txt">
<div>Recipe nr 2</div>
</div>
<div id="img3txt">
<div>Recipe nr 3</div>
</div>
</div>
编辑
我需要一个跨浏览器的解决方案,在主要浏览器上工作,没有脚本的,只有CSS。
为了澄清,我希望它像一个普通的无线输入一样工作,但是如果点击两次,或者重复点击同一个,它应该像一个复选框输入一样切换自己。
此外,标记更改也是允许的,只要布局结构保持不变,我也更喜欢它是否可以断线,因为一个页面可以有3个以上的食谱。
编辑2
这个问题的主要焦点是如何使无线电输入可切换,尽管有几个答案显示了使用纯CSS切换状态的其他方法,因此任何此类技巧都是受欢迎的。
发布于 2016-03-23 03:24:42
不能使用CSS更改单选按钮的功能。CSS仅为视觉更改而设计。
也就是说,你可以用一个聪明的技巧来模拟这种行为。对于您的示例,我建议使用CSS以可视方式将当前所选单选按钮的标签替换为附加到表示“空白”或“空”选择的另一个单选按钮的虚拟标签。这样,单击虚拟标签将选择“空白”选项,从而有效地清除您先前的选择:
.container {
display: flex;
flex-wrap: wrap;
max-width: 660px;
}
.container > label {
flex: 1;
flex-basis: 33.333%;
}
.container > div {
flex: 1;
flex-basis: 100%;
}
.container label img {
display: block;
margin: 0 auto;
}
.container input, .container input ~ div {
display: none;
padding: 10px;
}
.container #img1:checked ~ #img1txt,
.container #img2:checked ~ #img2txt,
.container #img3:checked ~ #img3txt {
display: block;
}
.container label[for=noimg] {
display: none;
}
.container #img1:checked ~ label[for=img1],
.container #img2:checked ~ label[for=img2],
.container #img3:checked ~ label[for=img3] {
display: none;
}
.container #img1:checked ~ label[for=img1] + label[for=noimg],
.container #img2:checked ~ label[for=img2] + label[for=noimg],
.container #img3:checked ~ label[for=img3] + label[for=noimg] {
display: block;
}
<div id="img-select" class="container">
<input id="noimg" type="radio" name="img-descr">
<input id="img1" type="radio" name="img-descr">
<input id="img2" type="radio" name="img-descr">
<input id="img3" type="radio" name="img-descr">
<label for="img1">
<img src="http://lorempixel.com/200/200/food/1/" alt="">
</label>
<label for="noimg">
<img src="http://lorempixel.com/200/200/food/1/" alt="">
</label>
<label for="img2">
<img src="http://lorempixel.com/200/200/food/6/" alt="">
</label>
<label for="noimg">
<img src="http://lorempixel.com/200/200/food/6/" alt="">
</label>
<label for="img3">
<img src="http://lorempixel.com/200/200/food/8/" alt="">
</label>
<label for="noimg">
<img src="http://lorempixel.com/200/200/food/8/" alt="">
</label>
<div id="img1txt">
<div>Recipe nr 1</div>
</div>
<div id="img2txt">
<div>Recipe nr 2</div>
</div>
<div id="img3txt">
<div>Recipe nr 3</div>
</div>
</div>
发布于 2016-03-25 06:01:48
如果效果不需要持久,您可以使用:focus
实现类似的效果,而不是使用单选按钮。
要使元素成为focusable,请将tabindex
属性设置为整数。如果您不希望通过顺序焦点导航(按下"tab“键)到达元素,请使用负数。
.container {
display: flex;
flex-wrap: wrap;
max-width: 660px;
}
.container > .img {
flex: 1;
position: relative;
}
.container > .img > .unselect {
display: none;
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
}
.container > .txt {
display: none;
order: 1;
flex-basis: 100%;
}
.container > .img:focus > .unselect,
.container > .img:focus + .txt {
display: block;
}
<div id="img-select" class="container">
<div class="img" tabindex="0">
<img src="http://lorempixel.com/200/200/food/1/" alt="">
<span class="unselect" tabindex="-1"></span>
</div>
<div class="txt">Recipe nr 1</div>
<div class="img" tabindex="0">
<img src="http://lorempixel.com/200/200/food/6/" alt="">
<span class="unselect" tabindex="-1"></span>
</div>
<div class="txt">Recipe nr 2</div>
<div class="img" tabindex="0">
<img src="http://lorempixel.com/200/200/food/8/" alt="">
<span class="unselect" tabindex="-1"></span>
</div>
<div class="txt">Recipe nr 3</div>
</div>
发布于 2016-03-28 04:12:30
接受赏金挑战(无需额外的noimg
)
.container {
display: flex;
flex-wrap: wrap;
max-width: 660px;
overflow: hidden;
position: relative;
}
.container img {
user-select: none;
pointer-events: none;
}
.container > label {
flex: 1;
flex-basis: 33.333%;
z-index: 1;
}
.container > div {
flex: 1;
flex-basis: 100%;
}
.container label img { margin: 0 auto }
.container input,
.container input ~ div {
display: none;
padding: 10px;
}
.container label[for=none] {
position: absolute;
bottom: 0;
top: 0;
left: 0;
right: 0;
z-index: 0;
}
.container #img1:checked ~ label[for=img1],
.container #img2:checked ~ label[for=img2],
.container #img3:checked ~ label[for=img3] {
pointer-events: none;
z-index: -1;
}
.container #img1:checked ~ #img1txt,
.container #img2:checked ~ #img2txt,
.container #img3:checked ~ #img3txt { display: block }
<div id="img-select" class="container">
<input id="img1" type="radio" name="img-descr">
<input id="img2" type="radio" name="img-descr">
<input id="img3" type="radio" name="img-descr">
<!-- Experimental -->
<input id="none" type="radio" name="img-descr" checked>
<label for="none"></label>
<label for="img1">
<img src="http://dummyimage.com/200/333" alt="">
</label>
<label for="img2">
<img src="http://dummyimage.com/200/666" alt="">
</label>
<label for="img3">
<img src="http://dummyimage.com/200/999" alt="">
</label>
<div id="img1txt">
<div>Recipe nr 1</div>
</div>
<div id="img2txt">
<div>Recipe nr 2</div>
</div>
<div id="img3txt">
<div>Recipe nr 3</div>
</div>
</div>
编辑:根据定义,单选按钮不应该是可切换的(我忘记了这是这个任务中的一个额外要求=打破规则)。@Ajedi32答案可能是最好的,但它可以优化(重复图像)?赏金还在游戏中..。
EDIT 2:现在是全功能的解决方案。(做这个小把戏https://stackoverflow.com/a/7392038/2601031)
EDIT 3:多层布局+修复的选择。
https://stackoverflow.com/questions/36162805
复制相似问题