简体   繁体   English

在到达最后一张幻灯片后如何使滑块回到第一张幻灯片,反之亦然

[英]How can I make the slider go back to the first slide after reaching the last slide and vice versa

I'm currently trying to teach to myself javascript and right now I'm in the middle of building a slider. 我目前正在尝试自学JavaScript,现在正在构建滑块。 So far I've to managed to figure out how I can make slider slide left and right. 到目前为止,我已经设法弄清楚了如何使滑块左右滑动。 My problems so far at the monent is how can I make slider go back to the first slide after reaching the last slide and vice versa. 到目前为止,我在问题上遇到的问题是,在到达最后一张幻灯片后如何使滑块回到第一张幻灯片,反之亦然。

Here is a code I have been working on. 这是我一直在努力的代码。

 $(document).ready(function() { var totalSlides = $('.slides li').length, slidesWidth = $('.slides li').width(), slideContainer = $('.slides'); slideCount = 0; slideItems = $('.slides li'); slideContainer.width(slidesWidth * totalSlides); $('.buttons .next').on('click', function() { nextSlide(); }); $('.buttons .prev').on('click', function() { prevSlide(); }); function nextSlide() { slideCount++; if (slideCount <= totalSlides) { slideCount++; console.log(slideCount); slideItems.animate({ left: '-=' + slidesWidth }, 200); } else if (slideCount === totalSlides) { slideCount = 0; slideItems.animate({ left: '+=' + slideContainer }, 200); } } function prevSlide() { if (totalSlides >= slideCount) { slideCount--; console.log(slideCount); slideItems.animate({ left: '+=' + slidesWidth }, 200); } } // function resetSlides() { // if ( slideCount === totalSlides ) { // slideCount = 0; // slideItems.animate({ // left: '+=' + slideContainer // }, 200); // } // } }) 
 .wrapper { max-width: 1440px; margin: 0 auto; } .timeline-container { max-width: 1440px; margin: 0 auto; position: relative; overflow: hidden; height: 300px; .slides { position: absolute; background-color: #ccc; width: 1440px; max-height: 300px; top: 0; left: 0; li { float: left; max-width: 1440px; width: 100%; height: 300px; position: relative; } } .buttons { position: absolute; top: 50%; left: 0; z-index: 10; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="timeline-container"> <ul class="slides"> <li> <div class="image"> </div> <div class="content"> 1965 </div> </li> <li> <div class="image"> </div> <div class="content"> 1968 </div> </li> <li> <div class="image"> </div> <div class="content"> 1969 </div> </li> </ul> <ul class="buttons"> <li class="prev">&lt;</li> <li class="next">&gt;</li> </ul> </div> </div> 

Try this code 试试这个代码

function gotoSlide(position){
if(position === 'first'){

    console.log(slideCount);
    slideItems.animate({
        left: '+=' + 0
    }, 200);
    slideCount=0;
}
else{
    console.log(slideCount);
    slideItems.animate({
        left: '+=' + slidesWidth*(totalSlides-1)
    }, 200);
    slideCount=totalSlides;
}
}

and call gotoSlide(yourPosition) on the button click. 并在单击按钮时调用gotoSlide(yourPosition)。

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

相关问题 如何使英雄滑块转到特定幻灯片 - How to make hero slider go to a specific slide 我的 JS/CSS 滑块在第一张幻灯片后停止工作。 我该如何解决这个问题? - My JS/CSS Slider stops working after the first slide. How can i solve this? 分别到达最后一张和第一张幻灯片后,如何使“下一个”按钮和“上一个”按钮不起作用? - How to make Next button and Previous button non-working after I reach last and first slide respectively? 如何使此视差滑块自动滑动 - How can i make this parallax slider to slide automatically 当用户到达转盘上的最后一张幻灯片时,循环返回到滑块中的第一张幻灯片 - loop back to first slide in a slider when user reaches last slide on the carousel 如何停止最后一张幻灯片上的滑块 - How to stop slider on the last slide 显示最后一张幻灯片时,如何使此javascript停止 - How can I make this javascript stop when the last slide is shown 如何检测最后一张幻灯片和第一张幻灯片? - How to detect the last slide and first slide in slick? 如何从一个div转到滑块中的特定幻灯片? - How do I go from one div to a specific slide in a slider? Iphone-Web,jQuery:当用户在滑块上滑动手指时,如何使网页上的滑块滑动? - Iphone-web, jQuery: How can i make slider on web page to slide as the user slides finger on slider?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM