ลูปแถบเลื่อน Jquery Div
ฉันมีปัญหาเล็กน้อย Div สไลด์และถึงจุดสิ้นสุดมันแสดง div 1 อย่างรวดเร็วโดยไม่ต้องหยุดชั่วคราว Div 5 มองไม่เห็น
$(function(){ var width = '100vw'; var speed = 1500; var pause = 3000; var current = 1; var $slider = $('.slider'); var $slides = $slider.find('.slides'); var $slide = $slides.find('.slide'); setInterval(function(){ $slides.animate({'margin-left': '-='+width}, speed, function(){
current++;
if(current == $slide.length){ $slides.css('margin-left', 0);
current = 1;
}
});
}, pause);
});
แต่ถ้าเพิ่มดีเลย์
if(current == $slide.length){ $(this).delay(pause).queue(function(){
$slides.css('margin-left', 0);
});
current = 1;
}
$(function(){
var width = '100vw';
var speed = 1500;
var pause = 3000;
var current = 1;
var $slider = $('.slider');
var $slides = $slider.find('.slides');
var $slide = $slides.find('.slide');
setInterval(function(){
$slides.animate({'margin-left': '-='+width}, speed, function(){ current++; if(current == $slide.length){
$(this).delay(pause).queue(function(){ $slides.css('margin-left', 0);
});
current = 1;
}
});
}, pause);
});
.slider {
width: 100%;
height:auto;
text-align: center;
overflow: hidden;
}
.slides {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
-webkit-overflow-scrolling: touch;
/*
scroll-snap-points-x: repeat(300px);
scroll-snap-type: mandatory;
*/
}
.slides::-webkit-scrollbar {
width: 1px;
height: 10px;
}
.slides::-webkit-scrollbar-thumb {
background: #bbb;
}
.slides::-webkit-scrollbar-track {
background: transparent;
}
.slides > div {
scroll-snap-align: start;
flex-shrink: 0;
width: 100vw;
height: 100vh;
background: #eee;
transform-origin: center center;
transform: scale(1);
transition: transform 0.5s;
position: relative;
display: flex;
justify-content: center;
align-items: center;
font-size: 100px;
}
.slides > div:target {
/* transform: scale(0.8); */
}
.author-info {
background: rgba(0, 0, 0, 0.75);
color: white;
padding: 0.75rem;
text-align: center;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
margin: 0;
}
.author-info a {
color: white;
}
img {
object-fit: cover;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.slider > a {
display: inline-flex;
width: 1.5rem;
height: 1.5rem;
background: white;
text-decoration: none;
align-items: center;
justify-content: center;
margin: 0 0 0.5rem 0;
position: relative;
border:1px solid green;
}
.slider > a:active {
top: 1px;
}
.slider > a:focus {
background: #666;
}
/* Don't need button navigation */
@supports (scroll-snap-type) {
.slider > a {
display: none;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="slider">
<div class="slides">
<div id="slide-1" class="slide">Slide 1</div>
<div id="slide-2" class="slide">Slide 2</div>
<div id="slide-3" class="slide">Slide 3</div>
<div id="slide-4" class="slide">Slide 4</div>
<div id="slide-5" class="slide">Slide 5</div>
</div>
</div>
มันแสดง Div 5 อย่างที่ต้องการ แต่หลังจากแสดง div 1 แล้วมันจะไม่เลื่อนอีกต่อไป
แล้วฉันจะทำอย่างไรเพื่อให้วนซ้ำและแสดงเวลาที่กำหนดแต่ละ div
อาจมีใครบางคนสามารถแสดงวิธีทำให้ตัวเลื่อนเลื่อนไปข้างหลังหลังจากถึง div สุดท้ายและเลื่อนกลับไปที่ div หนึ่ง
คำตอบ
ขั้นแรก: วิธีแก้ปัญหาที่ไม่ดีสำหรับปัญหาของคุณ: (ไปที่วินาที)
โค้ดแอนิเมชั่นของคุณติดอยู่ในคิวหนึ่งครั้งcurrent == $slide.lengthคุณควรส่งผ่านฟังก์ชันถัดไปเป็นอาร์กิวเมนต์ในการเรียกคิวจากนั้นใช้next()เพื่อทริกเกอร์ฟังก์ชันอื่น ดังต่อไปนี้ :
if(current == $slide.length){
$(this).delay(pause).queue(function(next){ //<--- here next argument $slides.css('margin-left', 0);
next(); // <----- next call
});
current = 1;
}
แต่จะไม่ทำให้การแจ้งเตือนทำงานตามที่คาดไว้
ประการที่สอง: วิธีง่ายๆที่เหมาะสม
สิ่งที่ฉันแนะนำคือตั้งค่าซ้ายของภาพเคลื่อนไหวเป็นตัวแปรจากนั้นตรวจสอบว่าเป็นครั้งสุดท้ายหรือไม่เพื่อให้เลื่อนไปที่ 0 อื่น ๆ ที่ชาญฉลาดดำเนินการเลื่อนความกว้างมุมมองด้านซ้าย
ดูตัวอย่างการทำงาน:
$(function(){ var width = '100vw'; var speed = 1500; var pause = 3000; var current = 1; var $slider = $('.slider'); var $slides = $slider.find('.slides'); var $slide = $slides.find('.slide'); var leftAnnime = '-='+width; setInterval(function(){ $slides.animate({'margin-left': leftAnnime }, speed, function(){
current++;
if( current == $slide.length ) {
leftAnnime = 0;
current = 0;
} else {
leftAnnime = '-='+width;
}
});
}, pause);
});
.slider {
width: 100%;
height:auto;
text-align: center;
overflow: hidden;
}
.slides {
display: flex;
overflow-x: hidden;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
-webkit-overflow-scrolling: touch;
/*
scroll-snap-points-x: repeat(300px);
scroll-snap-type: mandatory;
*/
}
.slides::-webkit-scrollbar {
width: 1px;
height: 10px;
}
.slides::-webkit-scrollbar-thumb {
background: #bbb;
}
.slides::-webkit-scrollbar-track {
background: transparent;
}
.slides > div {
scroll-snap-align: start;
flex-shrink: 0;
width: 100vw;
height: 100vh;
background: #eee;
transform-origin: center center;
transform: scale(1);
transition: transform 0.5s;
position: relative;
display: flex;
justify-content: center;
align-items: center;
font-size: 100px;
}
.slides > div:target {
/* transform: scale(0.8); */
}
.author-info {
background: rgba(0, 0, 0, 0.75);
color: white;
padding: 0.75rem;
text-align: center;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
margin: 0;
}
.author-info a {
color: white;
}
img {
object-fit: cover;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.slider > a {
display: inline-flex;
width: 1.5rem;
height: 1.5rem;
background: white;
text-decoration: none;
align-items: center;
justify-content: center;
margin: 0 0 0.5rem 0;
position: relative;
border:1px solid green;
}
.slider > a:active {
top: 1px;
}
.slider > a:focus {
background: #666;
}
/* Don't need button navigation */
@supports (scroll-snap-type) {
.slider > a {
display: none;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="slider">
<div class="slides">
<div id="slide-1" class="slide">Slide 1</div>
<div id="slide-2" class="slide">Slide 2</div>
<div id="slide-3" class="slide">Slide 3</div>
<div id="slide-4" class="slide">Slide 4</div>
<div id="slide-5" class="slide">Slide 5</div>
</div>
</div>