gpt4 book ai didi

jquery - 如何让幻灯片上的三个点可点击以显示相应的幻灯片?

转载 作者:行者123 更新时间:2023-11-28 14:03:53 24 4
gpt4 key购买 nike

如何让幻灯片上的三个点可点击以显示相应的幻灯片?

我希望旋转木马底部的三个点可以点击,以便显示相应的幻灯片。我如何使用 jQuery 实现此目的?

有人建议获取当前幻灯片的索引,然后使用索引将类应用于正确的点...但我不确定如何去做。

http://slideshow-test-22.superhi.com/

var currentSlide = 0
var totalSlides = $('.holder div.slide').length

var moveSlide = function(slide) {
var leftPosition = -(slide * 100) + 'vw'
$('.holder').css('left', leftPosition)
}

// Next Slide Function
var nextSlide = function() {
currentSlide = currentSlide + 1
if (currentSlide >= totalSlides) {
currentSlide = 0
}
moveSlide(currentSlide)
}

// Previous Slide Function
var previousSlide = function() {
currentSlide = currentSlide - 1
if (currentSlide < 0) {
currentSlide = totalSlides - 1
}
moveSlide(currentSlide)
}

// Auto Slide Function
var autoSlide = setInterval(function() {
nextSlide()
}, 5000)

// Arrows Function
$('.next').on('click', function() {
clearInterval(autoSlide)
nextSlide()
})

$('.prev').on('click', function() {
clearInterval(autoSlide)
previousSlide()
})
/* Base Carousel Styling */

.slideshow {
position: relative;
overflow: hidden;
width: 100vw;
height: 550px;
}

.holder {
position: relative;
top: 0;
left: 0;
width: 10000vw;
height: 550px;
transition: left 2s;
}

.holder div.slide {
float: left;
width: 100vw;
height: 550px;
}

.slide {
background-color: #f1f1f2;
position: relative;
}

a.prev,
a.next {
color: #eee;
text-decoration: none;
font-size: 30px;
position: absolute;
top: 50%;
transform: translateY(-50%);
padding: 20px;
}

.prev {
left: 10px;
}

.next {
right: 10px;
}


/* End Of Base Carousel Styling */


/*Slide Styling*/

.slide {
background-repeat: no-repeat;
background-size: cover;
}


/*Slide Copy + CTA Styling*/

h1 {
font-family: Oswald;
font-size: 60px;
line-height: 1.2;
text-transform: uppercase;
color: white;
}


/* p{
font-family: Open Sans;
font-size:18px;
line-height: 1.2;
text-transform: capitalize;
}

a.cta{
font-family: Open Sans;
font-size:12px;
line-height: 1.2;
padding:16px 8px;
text-transform: uppercase;
} */

.copy-cta-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}


/*Slide One Styling*/

.slide-1 {
background-color: #101820;
}

.slide-2 {
background-color: #283c50;
}

.slide-3 {
background-color: #426385;
}


/* The dots/bullets/indicators */

.carousel-dots {
position: absolute;
bottom: 10px;
width: 100%;
text-align: center;
}

.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #fff;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}

.active,
.dot:hover {
background-color: #717171;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="slideshow">
<div class="holder">
<div class="slide-1 slide">
<div class="copy-cta-container">
<h1>Slide 1</h1>
</div>
</div>
<div class="slide-2 slide">
<div class="copy-cta-container">
<h1>Slide 2</h1>
</div>
</div>
<div class="slide-3 slide">
<div class="copy-cta-container">
<h1>Slide 3</h1>
</div>
</div>
</div>
<a href="#" class="prev">&lt;</a>
<a href="#" class="next">&gt;</a>
<section class="carousel-dots">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</section>
</div>

最佳答案

您可以使用现有的 moveSlide() 函数来更改事件幻灯片。您需要做的就是将单击的 .dot 元素的索引关联到幻灯片,这可以通过获取其 index() 来完成。试试这个:

$('.dot').click(function() {
currentSlide = $(this).index();
moveSlide(currentSlide);
});

完整示例:

var currentSlide = 0
var totalSlides = $('.holder div.slide').length

$('.dot').click(function() {
currentSlide = $(this).index();
moveSlide(currentSlide);
});

var moveSlide = function(slide) {
var leftPosition = -(slide * 100) + 'vw'
$('.holder').css('left', leftPosition)
}

// Next Slide Function
var nextSlide = function() {
currentSlide = currentSlide + 1
if (currentSlide >= totalSlides) {
currentSlide = 0
}
moveSlide(currentSlide)
}

// Previous Slide Function
var previousSlide = function() {
currentSlide = currentSlide - 1
if (currentSlide < 0) {
currentSlide = totalSlides - 1
}
moveSlide(currentSlide)
}

// Auto Slide Function
var autoSlide = setInterval(function() {
nextSlide()
}, 5000)

// Arrows Function
$('.next').on('click', function() {
clearInterval(autoSlide)
nextSlide()
})

$('.prev').on('click', function() {
clearInterval(autoSlide)
previousSlide()
})
/* Base Carousel Styling */

.slideshow {
position: relative;
overflow: hidden;
width: 100vw;
height: 550px;
}

.holder {
position: relative;
top: 0;
left: 0;
width: 10000vw;
height: 550px;
transition: left 2s;
}

.holder div.slide {
float: left;
width: 100vw;
height: 550px;
}

.slide {
background-color: #f1f1f2;
position: relative;
}

a.prev,
a.next {
color: #eee;
text-decoration: none;
font-size: 30px;
position: absolute;
top: 50%;
transform: translateY(-50%);
padding: 20px;
}

.prev {
left: 10px;
}

.next {
right: 10px;
}


/* End Of Base Carousel Styling */


/*Slide Styling*/

.slide {
background-repeat: no-repeat;
background-size: cover;
}


/*Slide Copy + CTA Styling*/

h1 {
font-family: Oswald;
font-size: 60px;
line-height: 1.2;
text-transform: uppercase;
color: white;
}


/* p{
font-family: Open Sans;
font-size:18px;
line-height: 1.2;
text-transform: capitalize;
}

a.cta{
font-family: Open Sans;
font-size:12px;
line-height: 1.2;
padding:16px 8px;
text-transform: uppercase;
} */

.copy-cta-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}


/*Slide One Styling*/

.slide-1 {
background-color: #101820;
}

.slide-2 {
background-color: #283c50;
}

.slide-3 {
background-color: #426385;
}


/* The dots/bullets/indicators */

.carousel-dots {
position: absolute;
bottom: 10px;
width: 100%;
text-align: center;
}

.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #fff;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}

.active,
.dot:hover {
background-color: #717171;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="slideshow">
<div class="holder">
<div class="slide-1 slide">
<div class="copy-cta-container">
<h1>Slide 1</h1>
</div>
</div>
<div class="slide-2 slide">
<div class="copy-cta-container">
<h1>Slide 2</h1>
</div>
</div>
<div class="slide-3 slide">
<div class="copy-cta-container">
<h1>Slide 3</h1>
</div>
</div>
</div>
<a href="#" class="prev">&lt;</a>
<a href="#" class="next">&gt;</a>
<section class="carousel-dots">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</section>
</div>

关于jquery - 如何让幻灯片上的三个点可点击以显示相应的幻灯片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57415795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com