html - CSS - 每 5 秒滑动一次

标签 html css

我的网站上有这个 slider ( fiddle ):

/*
The only thing stopping this from being used
in older browsers is the "~" selector in the
last CSS rule,but since replacing it results
in messy code I left it as it is.
*/
@import url(http://fonts.googleapis.com/css?family=Varela+Round);

html, body {
  background: #333 url("http://codepen.io/images/classy_fabric.png");
}

.slides {
  padding: 0;
  width: 609px;
  height: 420px;
  display: block;
  margin: 0 auto;
  position: relative;
}

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

.slides input { display: none; }

.slide-container { display: block; }

.slide {
  top: 0;
  opacity: 0;
  width: 609px;
  height: 420px;
  display: block;
  position: absolute;

  transform: scale(0);

  transition: all .7s ease-in-out;
}

.slide img {
  width: 100%;
  height: 100%;
}

.nav label {
  width: 200px;
  height: 100%;
  display: none;
  position: absolute;

  opacity: 0;
  z-index: 9;
  cursor: pointer;

  transition: opacity .2s;

  color: #FFF;
  font-size: 156pt;
  text-align: center;
  line-height: 380px;
  font-family: "Varela Round", sans-serif;
  background-color: rgba(255, 255, 255, .3);
  text-shadow: 0px 0px 15px rgb(119, 119, 119);
}

.slide:hover + .nav label { opacity: 0.5; }

.nav label:hover { opacity: 1; }

.nav .next { right: 0; }

input:checked + .slide-container  .slide {
  opacity: 1;

  transform: scale(1);

  transition: opacity 1s ease-in-out;
}

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

.nav-dots {
  width: 100%;
  bottom: 9px;
  height: 11px;
  display: block;
  position: absolute;
  text-align: center;
}

.nav-dots .nav-dot {
  top: -5px;
  width: 11px;
  height: 11px;
  margin: 0 4px;
  position: relative;
  border-radius: 100%;
  display: inline-block;
  background-color: rgba(0, 0, 0, 0.6);
}

.nav-dots .nav-dot:hover {
  cursor: pointer;
  background-color: rgba(0, 0, 0, 0.8);
}

input#img-1:checked ~ .nav-dots label#img-dot-1,
input#img-2:checked ~ .nav-dots label#img-dot-2,
input#img-3:checked ~ .nav-dots label#img-dot-3,
input#img-4:checked ~ .nav-dots label#img-dot-4,
input#img-5:checked ~ .nav-dots label#img-dot-5,
input#img-6:checked ~ .nav-dots label#img-dot-6 {
  background: rgba(0, 0, 0, 0.8);
}
<ul class="slides">
  <input type="radio" name="radio-btn" id="img-1" checked />
  <li class="slide-container">
    <div class="slide">
      <img src="http://farm9.staticflickr.com/8072/8346734966_f9cd7d0941_z.jpg" />
    </div>
    <div class="nav">
      <label for="img-6" class="prev">&#x2039;</label>
      <label for="img-2" class="next">&#x203a;</label>
    </div>
  </li>

  <input type="radio" name="radio-btn" id="img-2" />
  <li class="slide-container">
    <div class="slide">
      <img src="http://farm9.staticflickr.com/8504/8365873811_d32571df3d_z.jpg" />
    </div>
    <div class="nav">
      <label for="img-1" class="prev">&#x2039;</label>
      <label for="img-3" class="next">&#x203a;</label>
    </div>
  </li>

  <input type="radio" name="radio-btn" id="img-3" />
  <li class="slide-container">
    <div class="slide">
      <img src="http://farm9.staticflickr.com/8068/8250438572_d1a5917072_z.jpg" />
    </div>
    <div class="nav">
      <label for="img-2" class="prev">&#x2039;</label>
      <label for="img-4" class="next">&#x203a;</label>
    </div>
  </li>

  <input type="radio" name="radio-btn" id="img-4" />
  <li class="slide-container">
    <div class="slide">
      <img src="http://farm9.staticflickr.com/8061/8237246833_54d8fa37f0_z.jpg" />
    </div>
    <div class="nav">
      <label for="img-3" class="prev">&#x2039;</label>
      <label for="img-5" class="next">&#x203a;</label>
    </div>
  </li>

  <input type="radio" name="radio-btn" id="img-5" />
  <li class="slide-container">
    <div class="slide">
      <img src="http://farm9.staticflickr.com/8055/8098750623_66292a35c0_z.jpg" />
    </div>
    <div class="nav">
      <label for="img-4" class="prev">&#x2039;</label>
      <label for="img-6" class="next">&#x203a;</label>
    </div>
  </li>

  <input type="radio" name="radio-btn" id="img-6" />
  <li class="slide-container">
    <div class="slide">
      <img src="http://farm9.staticflickr.com/8195/8098750703_797e102da2_z.jpg" />
    </div>
    <div class="nav">
      <label for="img-5" class="prev">&#x2039;</label>
      <label for="img-1" class="next">&#x203a;</label>
    </div>
  </li>

  <li class="nav-dots">
    <label for="img-1" class="nav-dot" id="img-dot-1"></label>
    <label for="img-2" class="nav-dot" id="img-dot-2"></label>
    <label for="img-3" class="nav-dot" id="img-dot-3"></label>
    <label for="img-4" class="nav-dot" id="img-dot-4"></label>
    <label for="img-5" class="nav-dot" id="img-dot-5"></label>
    <label for="img-6" class="nav-dot" id="img-dot-6"></label>
  </li>
</ul>

使用这段代码手动滑动:

.slide {
    top: 0;
    opacity: 0;
    width: 609px;
    height: 420px;
    display: block;
    position: absolute;
    transform: scale(0);
    transition: all .7s ease-in-out;
}

如您所见,它是纯 HTML 和 CSS,我希望保持这种状态。

有没有办法让 slider 每 5 秒循环一次图像?

最佳答案

它根本不会因为该代码而滑动。由于单选按钮和 :checked 伪类,它可以滑动:

<input type="radio" name="radio-btn" id="img-1" checked />
input:checked + .slide-container .slide {
  opacity: 1;
  transform: scale(1);
  transition: opacity 1s ease-in-out;
}

因此,正如目前所写的那样,不使用 JS 就无法实现任何自动化,因为使用 CSS 就无法更改单选框的状态。

如果想要自动化,可以试试animation属性(property)而不是 radio 。但是这样你就不能手动滑动了。

关于html - CSS - 每 5 秒滑动一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27735741/

相关文章:

php - 登录时我尝试验证 3 个表中的值,但不起作用?

javascript - Ajax:多次提交后防止刷新页面

html - 如何将图像 float 到 div 内的某个位置?

javascript - 拖放后改变高度

html - CSS 选择器 : finding the last sibling of a type

css - 具有可变宽度的父列表项下的中心子菜单

html - 垂直对齐基础 5 中的一列

html - 我可以使用 :focus 显示和隐藏元素吗

css - 如何为 webkit 中的 HTML5 搜索取消按钮创建回退?

php - 将 Bootstrap 类添加到日期时间属性 Symfony