Multiple slideshows on one page makes the first one not work anymore

前端 未结 3 942
难免孤独
难免孤独 2020-12-04 00:48

First of all I know this question\'s been asked before the answer wasn\'t solving my problem so I\'d like to ask it again : I used a Slideshow code from \"W3school\" wich pr

3条回答
  •  情深已故
    2020-12-04 01:40

    I've created a solution here:

    var sliderObjects = [];
    createSliderObjects();
    
    function plusDivs(obj, n) {
      var parentDiv = $(obj).parent();
      var matchedDiv;
      $.each(sliderObjects, function(i, item) {
        if ($(parentDiv[0]).attr('id') == $(item).attr('id')) {
          matchedDiv = item;
          return false;
        }
      });
      matchedDiv.slideIndex=matchedDiv.slideIndex+n;
      showDivs(matchedDiv, matchedDiv.slideIndex);
    }
    
    function createSliderObjects() {
      var sliderDivs = $('.slider');
      $.each(sliderDivs, function(i, item) {
        var obj = {};
        obj.id = $(item).attr('id');
        obj.divContent = item;
        obj.slideIndex = 1;
        obj.slideContents = $(item).find('.mySlides');
        showDivs(obj, 1);
        sliderObjects.push(obj);
      });
    }
    
    function showDivs(divObject, n) {
      debugger;
      var i;
      if (n > divObject.slideContents.length) {
        divObject.slideIndex = 1
      }
      if (n < 1) {
        divObject.slideIndex = divObject.slideContents.length
      }
      for (i = 0; i < divObject.slideContents.length; i++) {
        divObject.slideContents[i].style.display = "none";
      }
      divObject.slideContents[divObject.slideIndex - 1].style.display = "block";
    }
    
    
    

    Manual Slideshow

    you can now add as many divs for sliders with 'slider' class and a unique id.

提交回复
热议问题