检查时如何更改单选按钮背景颜色

发布于 2025-02-02 15:13:47 字数 2056 浏览 0 评论 0原文

我有一个简单的幻灯片,带有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>

I have simple slideshow with 3 images, I need to make background color of the checked button(obviously, each button refers to one img). I tried literally all possible solutions in the google, I would be really thankful if anyone could help me. Thanks in advance!

Here is my slideshow code:

.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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2025-02-09 15:13:47

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

.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>  

Try to set radio button before label

.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>

Here's a solution with a bit of 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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文