简体   繁体   English

带有多个幻灯片间隔的jQuery幻灯片不起作用

[英]Jquery slideshow with multiple slideshows Interval not working

I've set up a jquery slideshow borrowing some code from someone else. 我已经建立了一个jquery幻灯片演示,借用了其他人的一些代码。 I wanted to use the same logic to animate three slideshow panes at once (They should all move and pause at the same time) 我想使用相同的逻辑一次对三个幻灯片窗格进行动画处理(它们应该同时移动和暂停)

However, when using the following code, only the third pane works as expected and I'm not sure why. 但是,在使用以下代码时,只有第三个窗格可以按预期工作,我不确定为什么。 Can anyone help explain why and look at fixing it? 谁能帮助解释原因并寻求解决方案?

It may be that I have to try a different method to achieve this, which is fine, but I'd like to at least understand what the problem is. 可能我必须尝试一种不同的方法来实现此目的,这很好,但我至少想了解问题所在。 I've got the working Jsfiddle here as well. 我也有正在工作的Jsfiddle What should happen is that each window should, after 10 seconds, move to the next slide, then after ten seconds it should go back to the original slide. 应该发生的是,每个窗口在10秒钟后应移至下一张幻灯片,然后在十秒钟后应返回到原始幻灯片。 Only the third window is doing this properly, the others are skipping the interval 只有第三个窗口可以正确执行此操作,其他窗口则跳过该间隔

 <div class='windows'>

    <div id='sliderWindow1' class='sliderWindow'>

            <ul class='slidesWindow'>
            <!-- Trend Graph -->
                <li class='slideWindow'>
                    <div id='trendGraph'>Pane 1</div> <!--jscript to get chart -->
                </li>
                <!-- total number of votes -->
                <li class='slideWindow'>
                    <div id='totalVotes'>Pane 2</div> <!--jscript to get chart -->
                </li>
                <li class='slideWindow'>
                    <div id='trendGraph2'>Pane 1</div> <!--jscript to get chart -->
                </li>
            </ul>

    </div>



    <div id='sliderWindow2' class='sliderWindow'>
        <ul class='slidesWindow'>

            <!-- Related Polls -->
                <li class='slideWindow'>
                    <div id='related'>Pane 1b</div> <!--jscript to get chart -->
                </li>
                <!-- Likes -->
                <li class='slideWindow'>
                    <div id='likesAndLiker'>Pane 2b</div> <!--jscript to get chart -->
                </li>
                <li class='slideWindow'>
                    <div id='relatedPolls2'>Pane 1b</div> <!--jscript to get chart -->
                </li>

            </ul>

    </div>

    <div id='sliderWindow3' class='sliderWindow'>
        <ul class='slidesWindow'>
            <!-- Your Vote -->
                <li class='slideWindow'>
                    <div id='yourLike'>Pane 1c</div> <!--jscript to get chart -->
                </li>
                <!-- total number of votes -->
                <li class='slideWindow'>
                    <div id='OtherThing'>Pane 2c</div> <!--jscript to get chart -->
                </li>
                <li class='slideWindow'>
                    <div id='yourLike2'>Pane 1c</div> <!--jscript to get chart -->
                </li>
            </ul>

    </div>



</div>

Jscript Jscript脚本

 $(document).ready(function(){
//configuration
    var width = 100;
    var animationSpeed = 1000;
    var pause = 12000;
    var currentID = 0;
    var currentSlide = 1;
    var height = 100;
    var StopCounting = true;

    //cache DOM     
    var $Slider = $('.sliderWindow');
    var $SlideContainer = $Slider.find(".slidesWindow");
    var $Slides = $SlideContainer.find(".slideWindow");
    var $SlidesLength = 3;
    var interval;


    $Slider.on('mouseenter', pauseSlider).on('mouseleave', startSlider);

//begin the slide show
startSlider();              

function startSlider(){

            //set interval
            interval = setInterval(function(){
                console.log(currentSlide + " of " + $SlidesLength);
                //margin left -720
                $SlideContainer.animate({'margin-left': '-='+width}, animationSpeed, function(){
                //callback
                currentSlide++;
                currentID++;

                //if this is the last slide go to position one (0px)
                if(currentSlide === $SlidesLength){

                currentSlide = 1;
                $SlideContainer.css('margin-left', 0);
                StopCounting = true;
                }


                });

            }, pause);
    }

    //pause the slider by clearing the interval
    function pauseSlider(){
    clearInterval(interval);
    }

 });//end of document ready

CSS CSS

 .windows {
  height: 200px;
 width: 100%;
 display: inline-block;
 margin-top: 20px;
 background-color: #C2F0FF;
 }

 .sliderWindow {
 margin-left: 10%;
 height: 100px;
 /*width: 20%;*/
 width: 100px;
 background-color: #2EB8E6;
 display: inline;
 float: left;
 overflow: hidden;
 }

 .slidesWindow{
 display: block;
 width: 300px;
 height: 100px;
 margin: 0;
 padding: 0;
 }

 .slideWindow {
 float: left;
 list-style-type: none;
 /*width: 100%;*/
 width: 100px;
 height: 100px;
 }

From the JSfiddle, I could not figure out the exact response you are looking for. 从JSfiddle中,我无法弄清您正在寻找的确切响应。 But I think I ran into a similar problem when I used multiple accordion elements in bootstrap/jquery the other day. 但是我想我前几天在bootstrap / jquery中使用多个手风琴元素时遇到了类似的问题。 The solution to the issue was to rename the div id and class id's the second and third time I was using the accordion. 解决该问题的方法是在我第二次和第三次使用手风琴时重命名div id和class id。 The analogy for you would be to check you div id and class id names and see if you can change their names in the second and third copies of the element. 类推是检查div id和class id名称,看看是否可以在元素的第二和第三副本中更改它们的名称。

I located the answer, the problem is where the "current slide" was adding up. 我找到了答案,问题在于“当前幻灯片”的总和。 This adds up for each slide that animates, and therefore "current slide" should start at 0 and Slide length should be equal to the number of slides times the number of windows. 这将为动画的每个幻灯片加起来,因此“当前幻灯片”应从0开始,并且幻灯片长度应等于幻灯片数乘以窗口数。

Therefore the answer is: 因此,答案是:

$(document).ready(function(){
//configuration
    var width = 100;
    var animationSpeed = 1000;
    var pause = 12000;
    var currentID = 0;
    var currentSlide = 0;
    var height = 100;
    var StopCounting = true;

    //cache DOM     
    var $Slider = $('.sliderWindow');
    var $SlideContainer = $Slider.find(".slidesWindow");
    var $Slides = $SlideContainer.find(".slideWindow");
    var $SlidesLength = 6;
    var interval;


    $Slider.on('mouseenter', pauseSlider).on('mouseleave', startSlider);

//begin the slide show
startSlider();              

function startSlider(){

            //set interval
            interval = setInterval(function(){

                //margin left -720
                $SlideContainer.animate({'margin-left': '-='+width}, animationSpeed, function(){
                //callback
                currentSlide++;
                currentID++;
                    alert(currentSlide);

                //if this is the last slide go to position one (0px)
                if(currentSlide === $SlidesLength){

                currentSlide = 0;
                $SlideContainer.css('margin-left', 0);
                StopCounting = true;
                }


                });

            }, pause);
    }

    //pause the slider by clearing the interval
    function pauseSlider(){
    clearInterval(interval);
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM