Cycle Divs and Thumbnails

北城以北 提交于 2019-12-04 12:24:47

Fade rotator - Demo on jsBin

<div id="rotator">

  <div id="slides">
    <div>Sample text 1</div>
    <div>Sample text 2</div>
    <div>Sample text 3</div>
  </div>

  <div id="thumbs">
    <div>1</div>
    <div>2</div>
    <div>3</div>   
  </div>

</div>
  • Auto fade
  • Pause on hover
  • Hover on thumbs triggers the main slide
  • Restart where stopped

And the jQ code:

var $el = $('#fader'),

//  SETUP  ////////
    F = 600 ,    // Fade Time
    P = 2000 ,   // Pause Time
    C = 0 ,      // Counter / Start Slide# (0 based)
///////////////////

    $sl = $('#slides > div'),
    $th = $('#thumbs > div'),
    N = $sl.length,
    T = null;

$sl.hide().eq(C).show();
$th.eq(C).addClass('on');

// ANIMATION
function anim() { 
  $sl.eq(C%N).stop(1).fadeTo(F,1).siblings().fadeTo(F,0);
  $th.removeClass('on').eq(C%N).addClass('on');
}

// AUTO ANIMATE     
function autoAnim() {   
   T = setTimeout(function() {
      C++;
      anim();     // Animate
      autoAnim(); // Prepare another iteration
   }, P+F);
}
autoAnim();      // Start loop

// HOVER PAUSE
$el.hover(function(e) {
   return e.type==='mouseenter'? clearTimeout( T ) : autoAnim();
});

// HOVER THUMBNAILS
$th.on('mouseenter', function() {
   C = $th.index( this );
   anim();
});

The simplest way to implement your desired functionality is to use Malsup's Cycle Plugin. Go to http://jquery.malsup.com/cycle/ and download the script, then look at the well-documented demos he's provided. Using Cycle gives you the opportunity to take advantage of a ton of different options which would take you forever to hand code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!