Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 第一轮后幻灯片放映的索引设置为1_Javascript_Jquery_Slideshow - Fatal编程技术网

Javascript 第一轮后幻灯片放映的索引设置为1

Javascript 第一轮后幻灯片放映的索引设置为1,javascript,jquery,slideshow,Javascript,Jquery,Slideshow,我不太清楚到底发生了什么。在初始启动时,代码可以完美地运行。但是,我注意到toIndex在初次运行后从0变为1。一般来说,它会取消导航高亮显示,只会制造麻烦 这是一个 代码呢 滑块 // Helper function to return the first slide for the given slider function getFirstSlide(sliderElem) { return sliderElem.find(".slide").eq(0); } // Helper f

我不太清楚到底发生了什么。在初始启动时,代码可以完美地运行。但是,我注意到toIndex在初次运行后从0变为1。一般来说,它会取消导航高亮显示,只会制造麻烦

这是一个 代码呢

滑块

// Helper function to return the first slide for the given slider
function getFirstSlide(sliderElem) {
  return sliderElem.find(".slide").eq(0);
}

// Helper function to get current slide for this slider
function getCurrentSlide(sliderElem) {
  return sliderElem.find(".currentSlide");
}

// Animate the given slider to the given slide
function animToSlide(sliderElem, slideNum) {

  // Get the current slide
  var currentSlide = getCurrentSlide(sliderElem);

  // Get the target slide
  var targetSlide;
  if(slideNum < sliderElem.data().slideCount) {
    // If the number if within the number of slides
    targetSlide = sliderElem.find(".slide").eq(slideNum);
  } else {
    // If the number is not within the number of slides, wrap to the first
    targetSlide = getFirstSlide(sliderElem);
  }

  // Position the target slide (currently invisible) above the current slide
  targetSlide.css({"z-index": 500});

  // Position the current slide under the target slide
  currentSlide.css({"z-index": 100});

  // Add currentSlide class to the target slide. Triggers CSS3 transition to start fading it in
  targetSlide.addClass("currentSlide");

  if(sliderElem.data().onSlideChange !== undefined) {
      sliderElem.data().onSlideChange(currentSlide.index(), slideNum);
  }

  // Wait for the animation duration before removing the currentSlide class from the current slide
  // Ensures that target slide in fully visible before the current slide fades away
  setTimeout(function() {
    currentSlide.removeClass("currentSlide");

  }, sliderElem.data().animDuration);
}

// Start a timer for the slider with the given duration, and save a reference to it
function startSliderTimer(sliderElem, slideDuration) {
  if(sliderElem.data().autoSlide){

  // Save the timer to the slider's data in case we need to cancel it later
  sliderElem.data({
    slideTimer: setTimeout(function() {

      sliderTickEvent(sliderElem);

    }, slideDuration)
  });
  }
}

// Used to manually set the current slide of the given slider to the given slide
function setSlide(sliderElem, slideNum) {

  // Cancel if an attempt is made to switch to the current slide
  if(slideNum == getCurrentSlide(sliderElem).index()) {
    return;
  }

  // Get the current timer for this slider
  var slideTimer = sliderElem.data().slideTimer;

  // Stop it if it exists
  if(slideTimer !== undefined) {
    clearTimeout(slideTimer);
  }

  // Animate to the given slide
  animToSlide(sliderElem, slideNum);

  // Set a new timer
  startSliderTimer(sliderElem, sliderElem.data().slideDuration);
}

// This is called on a timer.
// Calls animToSlide to the next slide
function sliderTickEvent(sliderElem) {
  // Get current slide number by looking at DOM
  var currentSlide = getCurrentSlide(sliderElem).index();

  // Animate to the next slide
  animToSlide(sliderElem, currentSlide + 1);

  // Start a timer.
  startSliderTimer(sliderElem, sliderElem.data().slideDuration);
}

// Given an object of options, initialize a new slider
function initSlider(sliderOptions) {

  // Get the slider element
  var sliderElem = sliderOptions.sliderElem;

  // Take the slider container and add our generic class
  sliderElem.addClass("slider");

  // Take the slides and add our generic class
 sliderElem.find(sliderOptions.slideSelector).addClass("slide");

  // Apply slider sizing 
  sliderElem.css({
    width: sliderOptions.sliderWidth,
    height: sliderOptions.sliderHeight
  });

  // Apply transition effect to slide
  sliderElem.find(".slide").css({
    transition: "opacity " + sliderOptions.animDuration + "s " + sliderOptions.animTimingFunc
  });

  // Count the number of slides and save it in the slider options
  sliderOptions.slideCount = sliderElem.find(".slide").length;

  // Store the slider options on the slider element itself
  sliderElem.data(sliderOptions);

  //Start a timer for this slider
  startSliderTimer(sliderElem, sliderOptions.slideDuration);

  //Add dot nav
  if(sliderElem.data().dotNav){
    innerDiv = '';
    sliderElem.find('.slide').each(function() {
      innerDiv += '<div class="slideNavDot">&sdot;</div>';
    });
    slideNav = '<div class="slideNav">' + innerDiv + '</div>';
    sliderElem.append(slideNav);
    sliderElem.find('.slideNavDot').eq(0).addClass('dotSelected');
  } 
}



$(document).ready(function() {

    var defaultSliderOptions = {
        //jQuery object for the slider container
        sliderElem: null,

        //jQuery selector for a slide
        slideSelector: null,

        //Slide duration in milliseconds
        slideDuration: 7000,

        // Animation duration in seconds
        animDuration: .7,

        // ease, ease-in, ease-out, ease-in-out, etc
        animTimingFunc: 'ease',

        sliderWidth: '100%',
        sliderHeight: '100%',
        autoSlide: true,

        onSlideChange: undefined
    };
    initSlider($.extend({}, defaultSliderOptions, {
        sliderElem: $("#campus-slider"),
        slideSelector: ".campusSlide",
        dotNav: true,
        onSlideChange: function(fromIndex, toIndex) {
            $('#campus-slider').find(".slideNavDot").addClass('campusDot');
            $('.campusDot').removeClass("dotSelected").eq(toIndex).addClass("dotSelected");
            $('.slideCover').css('background-image', 'none');
            console.log(toIndex);
        }
    }));

    $('#campus-slider').find('.slideNavDot').click(function() {
        setSlide($("#campus-slider"), $(this).index());
        $('#campus-slider').find('.slideNavDot').removeClass('dotSelected');
        $(this).addClass('dotSelected');
    });

});
//Helper函数返回给定滑块的第一张幻灯片
函数getFirstSlide(SlideReem){
返回sliderElem.find(“.slide”).eq(0);
}
//用于获取此滑块的当前幻灯片的辅助函数
函数getCurrentSlide(SlideReem){
返回sliderElem.find(“.currentSlide”);
}
//将给定滑块设置为给定幻灯片的动画
函数animToSlide(sliderElem,slideNum){
//获取当前幻灯片
var currentSlide=getCurrentSlide(SlideReem);
//获取目标幻灯片
var目标幻灯片;
if(slideNum
看起来唯一的问题是您没有重设条件来突出显示第一个点。JQuery方法
.eq()
onSlideChange: function(fromIndex, toIndex) {
    $('#campus-slider').find(".slideNavDot").addClass('campusDot');

    // if we exceed the lengths of the dots
    var $dots = $('.campusDot');
    if($dots.length === toIndex) {
        // reset to zero index
        toIndex = 0;
    }

    $('.campusDot').removeClass("dotSelected").eq(toIndex).addClass("dotSelected");
    $('.slideCover').css('background-image', 'none');
    console.log(toIndex);
}