Slider Html/Css dont move

xa9qqrwz  于 2023-04-01  发布在  其他
关注(0)|答案(1)|浏览(128)

嗨,我试图用HTML/CSS做一个滑块,但我不能让代码工作:这是CSS代码:

.content { height: 520px;width: 935px; overflow: hidden; 
 position: absolute;top: 50%; left:50%; transform: 
 translate(-50%,-25%);}

.navigation { position: absolute; bottom: 20px; left : 50%; 
          transform: translate(-50%); display: flex;}

.bar { width: 70px; height: 17px; border: 2px solid  white;
  margin: 6px; border-radius: 5px; cursor: pointer; 
  transition: 0.4s;}

.bar:hover { background-color: white;}

.slides{ display: flex; /*width: 100%;  height: 100%;*/ } 
.slide { width: 20%; transition: .6s; }   
.slide img { width: 100%; height: 100%;}
#slide1:checked ~.s1 { margin-left:0;  }
#slide2:checked ~.s1 { margin-left: -20%;}

我想我在幻灯片类的注解部分犯了同样的错误,这是HTML代码:

<div class="content">
    <div class="slides">
        <input type="radio" name="slide" id="slide1" 
 checked>
        <input type="radio" name="slide" id="slide2">
        <input type="radio" name="slide" id="slide3">
        <input type="radio" name="slide" id="slide4">
        
    </div>
   
    <div class="slide" s1>
        <img src="img/XU/Xu1a.jpg" alt="Robaleiro Xu">
    </div>
    <div class="slide" >
        <img src="img/XU/Xu2a.jpg" alt="Robaleiro Xu">    
    </div>
    <div class="slide" >
        <img src="img/XU/Xu3a.jpg" alt="Robaleiro Xu">  
    </div>
    <div class="slide" >
        <img src="img/XU/Xu5a.jpg" alt="Robaleiro Xu">  
    </div>

  <div class="navigation"> 
      <label  class="bar"  for="slide1"></label>
      <label  class="bar"  for="slide2"></label>
      <label  class="bar"  for="slide3"></label>
      <label  class="bar"  for="slide4"></label>
  </div>

我已经修改了这些代码很多次,在youtube示例中,此代码工作正常,但不适合我,感谢任何方向亚历杭德罗

rqqzpn5f

rqqzpn5f1#

首先,你需要了解“~”CSS选择器是如何工作的。
article|example
它选择了同一个父元素中的一个相邻元素。所以“input标签”和“.slide标签”应该有相同的父元素。
以下是工作示例:

.carousel {
    margin-left: 15%;
    margin-right: 15%;
}

ul.slides {
    display: block;
    position: relative;
    height: 600px;
    margin: 0;
    padding: 0;
    overflow: hidden;
    list-style: none;
}

.slides * {
    user-select: none;
    -ms-user-select: none;
    -moz-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

ul.slides input {
    display: none; 
}

.slide-container { 
    display: block; 
}

.slide-image {
    display: block;
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    opacity: 0;
    transition: all .7s ease-in-out;
}   

.slide-image img {
    width: auto;
    min-width: 100%;
    height: 100%;
}

.carousel-controls {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 999;
    font-size: 100px;
    line-height: 600px;
    color: #fff;
}

.carousel-controls label {
    display: none;
    position: absolute;
    padding: 0 20px;
    opacity: 0;
    transition: opacity .2s;
    cursor: pointer;
}

.slide-image:hover + .carousel-controls label{
    opacity: 0.5;
}

.carousel-controls label:hover {
    opacity: 1;
}

.carousel-controls .prev-slide {
    width: 49%;
    text-align: left;
    left: 0;
}

.carousel-controls .next-slide {
    width: 49%;
    text-align: right;
    right: 0;
}

.carousel-dots {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 20px;
    z-index: 999;
    text-align: center;
}

.carousel-dots .carousel-dot {
    display: inline-block;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-color: #fff;
    opacity: 0.5;
    margin: 10px;
}

input:checked + .slide-container .slide-image {
    opacity: 1;
    transform: scale(1);
    transition: opacity 1s ease-in-out;
}

input:checked + .slide-container .carousel-controls label {
     display: block; 
}

input#img-1:checked ~ .carousel-dots label#img-dot-1,
input#img-2:checked ~ .carousel-dots label#img-dot-2,
input#img-3:checked ~ .carousel-dots label#img-dot-3,
input#img-4:checked ~ .carousel-dots label#img-dot-4,
input#img-5:checked ~ .carousel-dots label#img-dot-5,
input#img-6:checked ~ .carousel-dots label#img-dot-6 {
    opacity: 1;
}

input:checked + .slide-container .nav label { display: block; }
<div>
  <div class="carousel">
    <ul class="slides">
      <input type="radio" name="radio-buttons" id="img-1" checked />
      <li class="slide-container">
        <div class="slide-image">
          <img src="https://upload.wikimedia.org/wikipedia/commons/9/9e/Timisoara_-_Regional_Business_Centre.jpg">
        </div>
        <div class="carousel-controls">
          <label for="img-3" class="prev-slide">
            <span>&lsaquo;</span>
          </label>
          <label for="img-2" class="next-slide">
            <span>&rsaquo;</span>
          </label>
        </div>
      </li>
      <input type="radio" name="radio-buttons" id="img-2" />
      <li class="slide-container">
        <div class="slide-image">
          <img src="https://content.r9cdn.net/rimg/dimg/db/02/06b291e8-city-14912-171317ad83a.jpg?width=1750&height=1000&xhint=3040&yhint=2553&crop=true">
        </div>
        <div class="carousel-controls">
          <label for="img-1" class="prev-slide">
            <span>&lsaquo;</span>
          </label>
          <label for="img-3" class="next-slide">
            <span>&rsaquo;</span>
          </label>
        </div>
      </li>
      <input type="radio" name="radio-buttons" id="img-3" />
      <li class="slide-container">
        <div class="slide-image">
          <img src="https://speakzeasy.files.wordpress.com/2015/05/twa_blogpic_timisoara-4415.jpg">
        </div>
        <div class="carousel-controls">
          <label for="img-2" class="prev-slide">
            <span>&lsaquo;</span>
          </label>
          <label for="img-1" class="next-slide">
            <span>&rsaquo;</span>
          </label>
        </div>
      </li>
      <div class="carousel-dots">
        <label for="img-1" class="carousel-dot" id="img-dot-1"></label>
        <label for="img-2" class="carousel-dot" id="img-dot-2"></label>
        <label for="img-3" class="carousel-dot" id="img-dot-3"></label>
      </div>
    </ul>
  </div>
</div>
<center>Code by Anca Spatariu. Username @ancaspatariu on Codepen</center>
  • 示例来自Codepen网站的@ancaspatariu用户。*

相关问题