admin管理员组

文章数量:1317123

Basically I am just trying to do random jQuery stuff for educational purpose, and here is my very simple slider. I want it to work automatically and also with controls (little arrows to scroll to next/previous slider). The only problem that I have right now is that when you press the arrow, the function that automatically switches slides every 5 seconds is still counting these 5000 ms, so the next slide appears faster then desired. What I want is to make those arrows reset the timer, so you press the arrow -> next slide appears -> only after 5 seconds later the slide switches again. Sorry for sloppy explanation, hope I made it clear enough.

Here's the jsfiddle: /

and here is the code

HTML

<body>
<div id="wrapper">
    <header>
         <h1>Simplest Sliding Image Slider</h1>

    </header>
    <div id="content">
        <div id="slider_container">
            <div id="slider">
                <div class="slides" id="slide1">
                    <img src=".jpg" alt="" />
                </div>
                <div class="slides" id="slide2">
                    <img src=".jpg" alt="" />
                </div>
                <div class="slides" id="slide3">
                    <img src=".jpg" alt="" />
                </div>
            </div>
        </div>
    </div>
    <footer></footer>
    </div>
</body>       

JS

jQuery(document).ready(function($) {

// start slider function
startSlider();

// set width and step variables and add active class to first slider
var slideWidth = $('.slides').width();
$('#slide1').addClass('slides active');

// actual function
function startSlider() {

looper = setInterval(function() {
  // remove and add class active
  $('.active').removeClass('active').next().addClass('active');

  // animation expression
  $('.active').animate({'left': '-=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '-=' + (slideWidth) + 'px'}, 500);

  // return to first slide after the last one
  if($('.active').length == 0) {
    $('#slide1').addClass('active');
    $('.slides').animate({'left': 0}, 500);

  }
}, 5000); // interval


// adding controls
$('.slides').append("<div class='controls'><a class='control_left' href='#'></a><a class='control_right' href='#'></a></div>");

// remove unnecessary controlls on first and last slides
$('.slides:nth-child(1) a.control_left').remove();
$(".slides:nth-child(" + $('.slides').length + ") a.control_right").remove();

// add functionality to controls
$('.control_left').on('click', function() {
  $('.active').removeClass('active').prev().addClass('active');
  $('.active').animate({'left': '+=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '+=' + (slideWidth) + 'px'}, 500);
});

$('.control_right').on('click', function() {
  $('.active').removeClass('active').next().addClass('active');
  $('.active').animate({'left': '-=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '-=' + (slideWidth) + 'px'}, 500);
});



}

});

Thx a lot in advance

Basically I am just trying to do random jQuery stuff for educational purpose, and here is my very simple slider. I want it to work automatically and also with controls (little arrows to scroll to next/previous slider). The only problem that I have right now is that when you press the arrow, the function that automatically switches slides every 5 seconds is still counting these 5000 ms, so the next slide appears faster then desired. What I want is to make those arrows reset the timer, so you press the arrow -> next slide appears -> only after 5 seconds later the slide switches again. Sorry for sloppy explanation, hope I made it clear enough.

Here's the jsfiddle: http://jsfiddle/cA9aW/

and here is the code

HTML

<body>
<div id="wrapper">
    <header>
         <h1>Simplest Sliding Image Slider</h1>

    </header>
    <div id="content">
        <div id="slider_container">
            <div id="slider">
                <div class="slides" id="slide1">
                    <img src="http://s2.postimg/5uxqi0mgl/cats1.jpg" alt="" />
                </div>
                <div class="slides" id="slide2">
                    <img src="http://s2.postimg/66f6us2wl/cats2.jpg" alt="" />
                </div>
                <div class="slides" id="slide3">
                    <img src="http://s2.postimg/ai3sjs9th/cats3.jpg" alt="" />
                </div>
            </div>
        </div>
    </div>
    <footer></footer>
    </div>
</body>       

JS

jQuery(document).ready(function($) {

// start slider function
startSlider();

// set width and step variables and add active class to first slider
var slideWidth = $('.slides').width();
$('#slide1').addClass('slides active');

// actual function
function startSlider() {

looper = setInterval(function() {
  // remove and add class active
  $('.active').removeClass('active').next().addClass('active');

  // animation expression
  $('.active').animate({'left': '-=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '-=' + (slideWidth) + 'px'}, 500);

  // return to first slide after the last one
  if($('.active').length == 0) {
    $('#slide1').addClass('active');
    $('.slides').animate({'left': 0}, 500);

  }
}, 5000); // interval


// adding controls
$('.slides').append("<div class='controls'><a class='control_left' href='#'></a><a class='control_right' href='#'></a></div>");

// remove unnecessary controlls on first and last slides
$('.slides:nth-child(1) a.control_left').remove();
$(".slides:nth-child(" + $('.slides').length + ") a.control_right").remove();

// add functionality to controls
$('.control_left').on('click', function() {
  $('.active').removeClass('active').prev().addClass('active');
  $('.active').animate({'left': '+=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '+=' + (slideWidth) + 'px'}, 500);
});

$('.control_right').on('click', function() {
  $('.active').removeClass('active').next().addClass('active');
  $('.active').animate({'left': '-=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '-=' + (slideWidth) + 'px'}, 500);
});



}

});

Thx a lot in advance

Share Improve this question asked Sep 28, 2013 at 15:41 Serge EremeevSerge Eremeev 1201 gold badge2 silver badges10 bronze badges 3
  • You want to reset it on every button click and startover again right? – PSL Commented Sep 28, 2013 at 15:53
  • That's not the right (nor simplest) way to build sliders, just use 2 buttons instead of putting buttons all over the place – Roko C. Buljan Commented Sep 28, 2013 at 15:59
  • I realise that this is very far from being perfect, but just wanted to make a simple slider first and solve all the problems I encounter. Now I will try to polish it, make it better, and then try something a little bit more advanced. – Serge Eremeev Commented Sep 28, 2013 at 16:06
Add a ment  | 

2 Answers 2

Reset to default 3

Slideshow with prev/next buttons, autoslide, pause on hover

Instead of jQuery's .animate() and animating the left CSS property, use the GPU accelerated CSS transform: translateX for the animation on a mon slides wrapper element

$(".SlideShow").each((i, EL) => {

  const
    $parent = $(EL),
    $slides = $(".SlideShow-slides", EL),
    $item = $(".SlideShow-item", EL),
    $prevNext = $(".SlideShow-btn", EL),
    tot = $item.length,
    mod = (n, m) => ((n % m) + m) % m;

  let
    c = 0,
    itv;

  const prev = () => {c = mod(--c, tot); anim();};
  const next = () => {c = mod(++c, tot); anim();};
  const anim = () => $slides.css({transform: `translateX(-${c * 100}%)`});
  const stop = () => clearInterval(itv);
  const play = () => itv = setInterval(next, 4000);
  
  $prevNext.on("click", (ev) => $(ev.currentTarget).is(".next") ? next() : prev());
  $parent.hover(stop, play);
  play(); // start

});
.SlideShow {
  position: relative;
  overflow: hidden;
  width: 100%;
  height: 180px;
}

.SlideShow-slides {
  display: flex;
  flex-flow: row-nowrap;
  height: 100%;
  width: 100%;
  transition: transform 0.7s; /* Animation duration here */
}

.SlideShow-item {
  min-width: 100%;
}

.SlideShow-item>img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.SlideShow-btn {
  position: absolute;
  top: 0;
  z-index: 1;
  width: 50px;
  height: 100%;
  background: rgba(255, 255, 255, 0.5);
  opacity: 0.5;
  border: 0;
  cursor: pointer;
}

.SlideShow-btn:hover {
  opacity: 1;
}

.SlideShow-btn.next {
  right: 0px;
}
<div class="SlideShow">
  <div class="SlideShow-slides">
    <div class="SlideShow-item"><img src="http://placehold.it/600x400/0bf?text=A" alt=""></div>
    <div class="SlideShow-item"><img src="http://placehold.it/600x400/fb0?text=B" alt=""></div>
    <div class="SlideShow-item"><img src="http://placehold.it/600x400/0fb?text=C" alt=""></div>
  </div>
  <button type="button" class="SlideShow-btn prev"></button>
  <button type="button" class="SlideShow-btn next"></button>
</div>

<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.5.1/jquery.min.js"></script>

What you need to do it to clear the interval in the button clicks and start interval again.

function resetInterval(){ //add this method which wil reset the timer
        window.clearInterval(looper); //clear current interval
        looper = setInterval(autoSlide, 5000); //start auto slide again.
}
function autoSlide(){ //move this to a function from being anonymous
        // remove and add class active
        $('.active').removeClass('active').next().addClass('active');
        // animation expression
        $('.active').animate({
            'left': '-=' + (slideWidth) + 'px'
        }, 500);
        $('.active').siblings().animate({
            'left': '-=' + (slideWidth) + 'px'
        }, 500);

        // return to first slide after the last one
        if ($('.active').length === 0) {
            $('#slide1').addClass('active');
            $('.slides').animate({
                'left': 0
            }, 500);
        }
}

and

 $('.control_left').on('click', function () {
        resetInterval(); //reset it
 ....

$('.control_right').on('click', function () {
        resetInterval(); //reset it
....

Demo

本文标签: javascriptjquery slider with controls and automatic scrollingStack Overflow