gpt4 book ai didi

javascript - 向我的图像 slider 添加过渡效果

转载 作者:太空宇宙 更新时间:2023-11-04 07:40:53 25 4
gpt4 key购买 nike

我正在使用图像 slider ,我正在尝试从零开始做一个,这样我就可以在这个过程中学习。我能做的很简单,我想添加一个过渡效果,比如淡出,或类似的东西。如果有人知道如何将它添加到我的代码中,那将对我有很大帮助。这是我到目前为止所做的:

var currentIndex = 0,
items = $('.container div'),
itemAmt = items.length;

function cycleItems() {
var item = $('.container div').eq(currentIndex);
items.hide();
item.css('display','inline-block');
}

var autoSlide = setInterval(function() {
currentIndex += 1;
if (currentIndex > itemAmt - 1) {
currentIndex = 0;
}
cycleItems();
}, 3000);

$('.next').click(function() {
clearInterval(autoSlide);
currentIndex += 1;
if (currentIndex > itemAmt - 1) {
currentIndex = 0;
}
cycleItems();
});

$('.prev').click(function() {
clearInterval(autoSlide);
currentIndex -= 1;
if (currentIndex < 0) {
currentIndex = itemAmt - 1;
}
cycleItems();
});
.container {
max-width: 200px;
background-color: black;
margin: 0 auto;
text-align: center;
position: relative;
}
.container div {
background-color: white;
width: 100%;
display: inline-block;
display: none;
}
.container img {
width: 100%;
height: auto;
}

button {
position: absolute;
}

.next {
right: 5px;
}

.prev {
left: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="demo">
<button class="next">Next</button>
<button class="prev">Previous</button>
<div class="container">
<div style="display: inline-block;">
<img src="https://placeimg.com/400/200/people"/>
</div>
<div>
<img src="https://placeimg.com/400/200/any"/>
</div>
<div>
<img src="https://placeimg.com/400/200/nature"/>
</div>
<div>
<img src="https://placeimg.com/400/200/architecture"/>
</div>
<div>
<img src="https://placeimg.com/400/200/animals"/>
</div>
<div>
<img src="https://placeimg.com/400/200/people"/>
</div>
<div>
<img src="https://placeimg.com/400/200/tech"/>
</div>
</div>
</section>

最佳答案

您可以使用 .fadeIn()来自 jQuery。

item.fadeIn();

运行这段代码:

var currentIndex = 0,
items = $('.container div'),
itemAmt = items.length;

function cycleItems() {
var item = $('.container div').eq(currentIndex);
items.hide();
item.fadeIn();//css('display','inline-block');
}

var autoSlide = setInterval(function() {
currentIndex += 1;
if (currentIndex > itemAmt - 1) {
currentIndex = 0;
}
cycleItems();
}, 3000);

$('.next').click(function() {
clearInterval(autoSlide);
currentIndex += 1;
if (currentIndex > itemAmt - 1) {
currentIndex = 0;
}
cycleItems();
});

$('.prev').click(function() {
clearInterval(autoSlide);
currentIndex -= 1;
if (currentIndex < 0) {
currentIndex = itemAmt - 1;
}
cycleItems();
});
.container {
max-width: 200px;
background-color: black;
margin: 0 auto;
text-align: center;
position: relative;
}
.container div {
background-color: white;
width: 100%;
display: inline-block;
display: none;
}
.container img {
width: 100%;
height: auto;
}

button {
position: absolute;
}

.next {
right: 5px;
}

.prev {
left: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="demo">
<button class="next">Next</button>
<button class="prev">Previous</button>
<div class="container">
<div style="display: inline-block;">
<img src="https://placeimg.com/400/200/people"/>
</div>
<div>
<img src="https://placeimg.com/400/200/any"/>
</div>
<div>
<img src="https://placeimg.com/400/200/nature"/>
</div>
<div>
<img src="https://placeimg.com/400/200/architecture"/>
</div>
<div>
<img src="https://placeimg.com/400/200/animals"/>
</div>
<div>
<img src="https://placeimg.com/400/200/people"/>
</div>
<div>
<img src="https://placeimg.com/400/200/tech"/>
</div>
</div>
</section>

关于javascript - 向我的图像 slider 添加过渡效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529875/

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