如何在选中时更改单选按钮背景颜色

How to change radio button background color while checked

我有一个包含 3 张图片的简单幻灯片,我需要为选中的按钮设置背景颜色(显然,每个按钮对应一个 img)。我在 google 中尝试了所有可能的解决方案,如果有人能帮助我,我将非常感激。提前致谢!

这是我的幻灯片代码:

.slider {
            transform: translateX(20%);
            display: inline-block;
            position: relative;
            width: 61%;
            overflow: hidden;
            box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px 0px; 
        }
        
        .images {
            display: flex;
            width: 100%;
        }
        
        .images img {
            width: 100%;
            transition: all 0.15s ease;  
        }
        
        .images input {
            display: none; 
        }
        
        .dots {
            display: flex;
            justify-content: center;
            margin: 5px; 
        }
        
        .dots label {
            height: 15px;
            width: 15px;
            border-radius: 50%;
            border: solid #13447E 3px;
            cursor: pointer;
            transition: all 0.15s ease;
            margin: 5px;
        } 
       
        #img1:checked ~ .m1 {
            margin-left: 0;   
        }
         
        #img2:checked ~ .m2 {
            margin-left: -100%;
        }
        
        #img3:checked ~ .m3 {
            margin-left: -200%;
        }
<div class="slider">
  <div class="images">
      <input type="radio" name="slide" id="img1" checked>
      <input type="radio" name="slide" id="img2">
      <input type="radio" name="slide" id="img3">


      <img src="img/img1.jpeg" class="m1" alt="img1">
      <img src="img/img2.jpeg" class="m2" alt="img2">
      <img src="img/img3.jpeg" class="m3" alt="img3">

  </div>
  <div class="dots">
      <label for="img1"></label>
      <label for="img2"></label>
      <label for="img3"></label>

  </div>
</div>

尝试在标签前设置单选按钮

.slider {
        transform: translateX(20%);
        display: inline-block;
        position: relative;
        width: 61%;
        overflow: hidden;
        box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px 0px; 
    }
    
    .images {
        display: flex;
        width: 100%;
    }
    
    .images img {
        width: 100%;
        transition: all 0.15s ease;  
    }
    
    .images input {
        display: none; 
    }
    
    .dots {
        display: flex;
        justify-content: center;
        margin: 5px; 
    }
    
    .dots label {
        height: 15px;
        width: 15px;
        border-radius: 50%;
        border: solid #13447E 3px;
        cursor: pointer;
        transition: all 0.15s ease;
        margin: 5px;
    } 
   
    #img1:checked ~ .m1 {
        margin-left: 0;   
    }
     
    #img2:checked ~ .m2 {
        margin-left: -100%;
    }
    
    #img3:checked ~ .m3 {
        margin-left: -200%;
    }
    [type="radio"] {
    display: none;
}
    input[type="radio"]:checked+label{border-color:red;}
<div class="slider">
        <div class="images">
            <img src="img/img1.jpeg" class="m1" alt="img1">
            <img src="img/img2.jpeg" class="m2" alt="img2">
            <img src="img/img3.jpeg" class="m3" alt="img3">
             
        </div>
        <div class="dots">
        <input type="radio" name="slide" id="img1" checked>
            <label for="img1"></label>
             <input type="radio" name="slide" id="img2">
            <label for="img2"> </label>
             <input type="radio" name="slide" id="img3">
            <label for="img3"></label>
             
        </div>
      </div>  

这是一个有点javascript的解决方案:)

.slider {
    transform: translateX(20%);
    display: inline-block;
    position: relative;
    width: 61%;
    overflow: hidden;
    box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px 0px; 
}

.images {
    display: flex;
    width: 100%;
}

.images img {
    width: 100%;
    transition: all 0.15s ease;  
}

.images input {
    display: none; 
}

.dots {
    display: flex;
    justify-content: center;
    margin: 5px; 
}

.dots label {
    height: 15px;
    width: 15px;
    border-radius: 50%;
    border: solid #13447E 3px;
    cursor: pointer;
    transition: all 0.15s ease;
    margin: 5px;
} 

#img1:checked ~ .m1 {
    margin-left: 0;   
}
 
#img2:checked ~ .m2 {
    margin-left: -100%;
}

#img3:checked ~ .m3 {
    margin-left: -200%;
}


.active {
    background-color: rgb(153, 153, 255);
}
<div class="slider">
        <div class="images">
            <input type="radio" name="slide" id="img1" checked>
            <input class="" type="radio" name="slide" id="img2">
            <input class="" type="radio" name="slide" id="img3">
           

            <img src="img/img1.jpeg" class="m1" alt="img1">
            <img src="img/img2.jpeg" class="m2" alt="img2">
            <img src="img/img3.jpeg" class="m3" alt="img3">
             
        </div>
        <div class="dots">
            <label id="dot1" class="active" onclick="activate('dot1')" for="img1"></label>
            <label id="dot2" class="" onclick="activate('dot2')" for="img2"></label>
            <label id="dot3" class="" onclick="activate('dot3')" for="img3"></label>
             
        </div>
      </div>  

      <script>
          var lastActiveDot = document.getElementById("dot1");
            function activate(id){
                var newActiveDot = document.getElementById(id);
                newActiveDot.classList.add("active");
                lastActiveDot.classList.remove("active")

                lastActiveDot = newActiveDot;
            }
      </script>