如何让我的CSS滑块自动播放?

时间:2015-10-11 18:09:47

标签: javascript jquery html css

有谁知道我如何制作我的CSS幻灯片自动播放?我使用下面的代码来制作它,但我找不到让它自动播放的方法。

这是我学习如何做到的地方......

http://thecodeplayer.com/walkthrough/css3-image-slider-with-stylized-thumbnails

任何人都可以帮助我吗?新手在这里......

// CSS CODES

    *{
margin: 0;
padding: 0;}

    .slider{
width: 640px;
position: relative;
padding-top: 250px;
margin: 100px auto;
box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);}

    .slider>img{
position: absolute;
left: 0;
bottom: 0;
transition: all 0.5s;}

    .slider input[name='slide_switch']{
display: none;}

    .slider label {
margin: 18px 0 0 18px;
border: 3px solid #999;
float: left;
cursor: pointer;
transition: all 2.5s;
opacity: 0.6;;}

    .slider label img{
display: block;}

    .slider input[name='slide_switch']:checked+label {
border-color: #666;
opacity: 1;}

    .slider input[name='slide_switch'] ~ img {
opacity: 0;
transform: scale(1.1);}

    .slider input[name='slide_switch']:checked+label+img {
opacity: 1;
transform: scale(1);}

/ HTML CODE(对不起重复图片我只是用它来尝试。) /

    <div class="slider">

        <input type="radio" name="slide_switch" id="id1" checked = "checked"/>
<label for="id1">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>


<input type="radio" name="slide_switch" id="id2"/>
<label for="id2">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>


<input type="radio" name="slide_switch" id="id3"/>
<label for="id3">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>


<input type="radio" name="slide_switch" id="id4"/>
<label for="id4">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>


<input type="radio" name="slide_switch" id="id5"/>
<label for="id5">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>





<input type="radio" name="slide_switch" id="id6"/>
<label for="id6">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>


<input type="radio" name="slide_switch" id="id7"/>
<label for="id7">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>


<input type="radio" name="slide_switch" id="id8"/>
<label for="id8">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>


<input type="radio" name="slide_switch" id="id9"/>
<label for="id9">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>


<input type="radio" name="slide_switch" id="id10"/>
<label for="id10">
    <img src="Img/Jaguar.jpg" width="100"/>
</label>
<img src="Img/Jaguar.jpg"/>
</div>  

1 个答案:

答案 0 :(得分:1)

您可以使用以下JS来旋转它:

$(function () {
  var time = 2000; // Time in milliseconds.
  allInputs = $('input[name="slide_switch"]');
  i = 0;
  setInterval(function () {
    $('input[name="slide_switch"]').prop("checked", false);
    $('#id' + ((i % allInputs.length) + 1)).prop("checked", true);
    i++;
  }, time);
});

工作代码段

&#13;
&#13;
$(function () {
  var time = 2000; // Time in milliseconds.
  allInputs = $('input[name="slide_switch"]');
  i = 0;
  setInterval(function () {
    $('input[name="slide_switch"]').prop("checked", false);
    $('#id' + ((i % allInputs.length) + 1)).prop("checked", true);
    i++;
  }, time);
});
&#13;
/*Time for the CSS*/
* {margin: 0; padding: 0;}
body {background: #ccc;}

.slider{
  width: 640px; /*Same as width of the large image*/
  position: relative;
  /*Instead of height we will use padding*/
  padding-top: 320px; /*That helps bring the labels down*/

  margin: 100px auto;

  /*Lets add a shadow*/
  box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);
}


/*Last thing remaining is to add transitions*/
.slider>img{
  position: absolute;
  left: 0; top: 0;
  transition: all 0.5s;
}

.slider input[name='slide_switch'] {
  display: none;
}

.slider label {
  /*Lets add some spacing for the thumbnails*/
  margin: 18px 0 0 18px;
  border: 3px solid #999;

  float: left;
  cursor: pointer;
  transition: all 0.5s;

  /*Default style = low opacity*/
  opacity: 0.6;
}

.slider label img{
  display: block;
}

/*Time to add the click effects*/
.slider input[name='slide_switch']:checked+label {
  border-color: #666;
  opacity: 1;
}
/*Clicking any thumbnail now should change its opacity(style)*/
/*Time to work on the main images*/
.slider input[name='slide_switch'] ~ img {
  opacity: 0;
  transform: scale(1.1);
}
/*That hides all main images at a 110% size
On click the images will be displayed at normal size to complete the effect
*/
.slider input[name='slide_switch']:checked+label+img {
  opacity: 1;
  transform: scale(1);
}
/*Clicking on any thumbnail now should activate the image related to it*/

/*We are done :)*/
&#13;
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<!--
We will make a slider with stylized thumbnails using CSS3
The markup is very simple:
Radio Inputs
Labels with thumbnails to detect click event
Main Image
-->
<div class="slider">
  <input type="radio" name="slide_switch" id="id1"/>
  <label for="id1">
    <img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100"/>
  </label>
  <img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg"/>

  <!--Lets show the second image by default on page load-->
  <input type="radio" name="slide_switch" id="id2" checked="checked"/>
  <label for="id2">
    <img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100"/>
  </label>
  <img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg"/>

  <input type="radio" name="slide_switch" id="id3"/>
  <label for="id3">
    <img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100"/>
  </label>
  <img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg"/>

  <input type="radio" name="slide_switch" id="id4"/>
  <label for="id4">
    <img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100"/>
  </label>
  <img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg"/>

  <input type="radio" name="slide_switch" id="id5"/>
  <label for="id5">
    <img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100"/>
  </label>
  <img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg"/>
</div>

<!-- We will use PrefixFree - a script that takes care of CSS3 vendor prefixes
You can download it from http://leaverou.github.com/prefixfree/ -->
<script src="http://thecodeplayer.com/uploads/js/prefixfree.js" type="text/javascript"></script>
&#13;
&#13;
&#13;