简体   繁体   English

如何根据选择的单选按钮交换显示的图像?

[英]How to swap the displayed image based on which radio button is selected?

I have a bunch of column groups (col-md-4), each containing two absolutely positioned images on top of each other within a div. 我有一堆列组(col-md-4),每个列组在div内彼此顶部都包含两个绝对定位的图像。 I need to switch the visible image within a group by changing its opacity from 0 to 1 based on which radio button is checked. 我需要根据选中的单选按钮,通过将其不透明度从0更改为1来在组内切换可见图像。 Is there a way to do this with jquery? 有没有办法用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>

My suggestion: 我的建议:

 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 Solution (as requested): 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> 

just add value attribute to each one of your input radios, and use this script :D hope this help 只需将value属性添加到每个输入单选按钮,然后使用此脚本:D希望此帮助

HTML 的HTML

add value="gray" and value="silver" 添加value="gray"value="silver"

    <div class="color-group">
      <input id="input-1" type="radio" checked="checked" name="change-image" value="gray" checked>
      <label for="input-1" title="gray">Gray</label>
      <input id="input-2" type="radio" name="change-image" value="silver">
      <label for="input-2" title="silver">Silver</label>
    </div>

You may add css opacity:0 to img silver for initialization 您可以将CSS opacity:0添加到img silver以进行初始化

<img src="images/silver.jpg" class="img-fluid silver" height="360" width="360" alt="silver" style="opacity:0">

and input radio gray to checked 并输入单选灰色以选中

<input id="input-1" type="radio" checked="checked" name="change-image" value="gray" checked>

JQuery jQuery查询

<script>
$(window).on('load',function() {
    $('input[name=change-image]').on('change',function() {
        $('img[alt='+$(this).val()+']').fadeTo(400,1.0).siblings().fadeTo(400,0.0);
    });
});
</script>

Good luck. 祝好运。

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

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