我有一堆列组 (col-md-4),每个列组在一个 div 中包含两个彼此重叠的绝对定位图像。我需要根据选中的单选按钮将其不透明度从 0 更改为 1 来切换组内的可见图像。有没有办法用 jquery 做到这一点?
<div class="row">
<div class="col-md-4">
<div class="slide-swapper">
<img src="images/gray.jpg" class="img-fluid gray" height="360" width="360" alt="gray">
<img src="images/silver.jpg" class="img-fluid silver" height="360" width="360" alt="silver">
</div>
<div class="color-group">
<input id="input-1" type="radio" checked="checked" name="change-image">
<label for="input-1" title="gray">Gray</label>
<input id="input-2" type="radio" name="change-image">
<label for="input-2" title="silver">Silver</label>
</div>
</div>
</div>
<style>
.slide-swapper {
position: relative;
height: 360px;
margin-bottom: 30px;
}
img {
position: absolute;
top: 0;
left: 0;
}
</style>
最佳答案
我的建议:
img {
position: absolute;
top: 0;
left: 0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
.color-group {
padding-top: 160px;
}
#input-1:checked ~ .blue {
opacity: 0;
}
#input-2:checked ~ .pink {
opacity: 0;
}
<div class="color-group">
<input id="input-1" type="radio" checked="checked" name="change-image">
<label for="input-1" title="gray">Pink</label>
<input id="input-2" type="radio" name="change-image">
<label for="input-2" title="silver">Blue</label>
<img class="pink" src="http://placehold.it/360x150/ffbbdd" height="150" width="360" alt="Pink">
<img class="blue" src="http://placehold.it/360x150/9acef9" height="150" width="360" alt="Blue">
</div>
jQuery 解决方案(根据要求):
$(document).ready(function() {
$(".color-group input").change(function() {
$(this).parent().prev().children("img:nth-of-type(2)").fadeToggle("slow");
});
});
.slide-swapper {
position: relative;
height: 150px;
margin-bottom: 30px;
}
img {
position: absolute;
top: 0;
left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-4">
<div class="slide-swapper">
<img src="https://placehold.it/360x150/ffbbdd" class="img-fluid gray" height="150" width="360" alt="gray">
<img src="https://placehold.it/360x150/9acef9" class="img-fluid silver" height="150" width="360" alt="silver">
</div>
<div class="color-group">
<input id="input-1" type="radio" checked="checked" name="change-image">
<label for="input-1" title="gray">Gray</label>
<input id="input-2" type="radio" name="change-image">
<label for="input-2" title="silver">Silver</label>
</div>
</div>
</div>
关于jquery - 如何根据选择的单选按钮交换显示的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42194750/