开发者

Need help with updating links in a carousel when next/previous buttons are clicked

开发者 https://www.devze.com 2023-03-01 16:41 出处:网络
I am building a carousel from scratch and am wondering how to go about adding a \"current\" class (to change the color) to a list of links t开发者_JAVA百科hat also make the carousel change (outside th

I am building a carousel from scratch and am wondering how to go about adding a "current" class (to change the color) to a list of links t开发者_JAVA百科hat also make the carousel change (outside the main carousel div).

I'm confused as to how to know which number LI to update based on when the next or previous button is clicked.

For example, say the current image is #4 out of 10 images. How can I make it so it will update the 3rd link if the previous button is clicked or the 5th if the next button is clicked?


You should keep an internal counter. All carousel scripts do that. The counter is a number 0-9 (for 10 LI's). Next will increment it with 1 and Prev the opposite. Something like this:

(function() { // you'll have some kind of closure
  var active = 0, size = 10;

  function next() {
    var prevActive = active;
    active++;
    if ( size <= active ) {
      active = 0;
    }
  }

  function prev() {
    // same general idea here
  }
})();

Why are you making your own carousel script? There are literally hundreds.

0

精彩评论

暂无评论...
验证码 换一张
取 消