jQuery动画滑块

时间:2017-04-19 07:36:16

标签: javascript html

我正在尝试创建一个jQuery滑块。 每张幻灯片都有不同的元素,当幻灯片处于活动状态时必须淡入,并且当它转到下一张幻灯片时必须淡出,等等......

到目前为止,我必须管理幻灯片中的每个元素淡入,但问题是我无法让它循环滑块。

jQuery代码:

function slider() {
    $(".animate-circle-1").delay(1000).fadeIn(1000).delay(6000).fadeOut(1000).delay(12000).fadeIn(1000);
    $(".animate-quote-0").delay(2000).fadeIn(1000).delay(2000).fadeOut(1000).delay(15000).fadeIn(1000);
    $(".animate-person-1").delay(5000).fadeIn(1000).delay(2000).fadeOut(1000);
    $(".animate-quote-1").delay(5000).fadeIn(1000).delay(2000).fadeOut(1000);
    $(".animate-person-2").delay(9000).fadeIn(1000).delay(4000).fadeOut(1000);
    $(".animate-circle-2").delay(10000).fadeIn(1000).delay(3000).fadeOut(1000);
    $(".animate-quote-2").delay(11000).fadeIn(1000).delay(2000).fadeOut(1000);  
    $(".animate-person-3").delay(15000).fadeIn(1000).delay(4000).fadeOut(1000);
    $(".animate-circle-3").delay(16000).fadeIn(1000).delay(3000).fadeOut(1000);
    $(".animate-quote-3").delay(17000).fadeIn(1000).delay(2000).fadeOut(1000);
    $(".slide-1").delay(8000).fadeOut(2000);
    $(".slide-2").delay(8000).fadeIn(2000);
    $(".slide-2").delay(6000).fadeOut(2000);
    $(".slide-3").delay(6000).fadeIn(2000);
}

HTML:

<div class="header-wrapper">

  <div class="slide slide-1">
  </div>

  <div class="slide slide-2">
  </div>

  <div class="slide slide-3">
  </div>

  <div class="header-wrapper-elements">
    <div class="slide-container">
      <div class="animate-quote animate-quote-0" style="display: none;">“It’s<br>
        good<br>
        to<br>
        connect”</div>
      <div class="animate-person animate-person-1" style="display: none;"></div>
      <div class="animate-circle animate-circle-1" style="display: none;"></div>
      <div class="animate-quote animate-quote-1" style="display: none;">Creëer<br>
        betrokken-<br>
        heid in de<br>
        student<br>
        levenscyclus</div>
      <div class="animate-person animate-person-2" style="display: none;"></div>
      <div class="animate-circle animate-circle-2" style="display: none;"></div>
      <div class="animate-quote animate-quote-2" style="display: none;">bouw<br>
        relaties om<br>
        business te<br>
        creëren</div>
      <div class="animate-person animate-person-3" style="display: none;"></div>
      <div class="animate-circle animate-circle-3" style="display: none;"></div>
      <div class="animate-quote animate-quote-3" style="display: none;">Bied een<br>
        unieke<br>
        klant-<br>
        ervaring</div>
    </div>
  </div>

有人知道一个好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

你的HMTL和JS的架构师并不是那么好。您可以查看https://www.sitepoint.com/web-foundations/making-simple-image-slider-html-css-jquery/以了解如何更好地完成此操作。 注意:为了更改图像滑动鼠标

`

<!-- Some junk -->
<div class="content">
  <p>This is a proof-of-concept for a slideshow that doesn't use any Javascript. It does not have pages or left/right buttons etc.
  <ul>
    <li>Pick a static width and height for the slides.</li>
    <li>Place all your slides side-by-side in a <strong>single</strong> image.</li>
    <li>Set the image as the background of the div. Use the background-position property to set the initial position of 0px. Be sure to calculate where the position needs to be for each slide, they will be negative numbers.</li>
    <li>In the animation CSS, set the length of time for the entire slideshow to run. For my three images I used 13 seconds. If you have more images you will need to increase this as needed.</li>
    <li>For the keyframes, you need to do some math. It's (pics * 2) / 100. Use this as the multipler for each slide, with "100%" being the last keyframe. Each image needs to have multiple keyframes in order for the slideshow to "pause" on that image. I used two stops for each slide, hence "pics * 2". Notice how the last keyframe at 100% is also the position of the first image of the slideshow. So technically, the first image gets 3 keyframes. I hope this makes sense.
    <li>This project is using Codepen's built-in -prefix-free setting, otherwise you need all those prefixes too!</li>
    <li>The Javascript is optional and is only there to enable clicking on each banner. If you want, you can remove all that and use a click event just on the div itself (one link for the entire slideshow). You will have to calculate the range of positions you want to be enabled for each banner click event. For example on the first banner, it is only clickable until it scrolls about half way to the 2nd banner, then the click fires on the second banner.</li>
  </ul>
  </p>

<p>Images taken from random Lorempixel.com images.</p>
</div>` 



  /* USUAL STUFF */
body { margin:10px auto; text-align:center; }
.content { max-width:800px;text-align:left; margin:auto; }

/* THE DIV */
.simple-ss {
  width:800px;
  height:250px;
  background-color:red;
  margin:auto;
  background-image:url("http://imgur.com/download/OtK7XDW");
  background-position:0;
  background-repeat:no-repeat;
  background-size:cover;

/* ANIMATING STUFF */
  animation-name: slide;
  animation-duration: 13s;
  animation-direction: normal;
  animation-timing-function: ease;
  animation-iteration-count: infinite;
}

/* NOTE CODEPEN AUTOGENERATING -PREFIXES */
@keyframes slide {
  0% {background-position:0 0;}
  16.66% {background-position:0 0;}
  33.32% {background-position:-800px 0;}
  49.98% {background-position:-800px 0;}
  66.64% {background-position:-1600px 0;}
  83.30% {background-position:-1600px 0;}
  100% {background-position:0 0;}
}
el = document.getElementById("simple-ss");

el.onclick = links;

function links(){   left = parseInt(getComputedStyle(el).getPropertyValue(&#34; background-position&#34;)。split(&#34;&#34;,1));

/ *确定点击活动的位置* /   if(left&gt; = -400){

// First until about half way scrolled over
alert("first");
//window.open("http://www.google.com");

}否则if(left&gt; = -1200){

// Second when half way scrolled either side
alert("second");
//window.open("http://www.google.com");

}否则if(left&gt; = -1600){

// Third when over half way into banner
alert("third");
//window.open("http://www.google.com");

}

}