gpt4 book ai didi

javascript - 循环的 CSS/JS slider 问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:31 26 4
gpt4 key购买 nike

我对 CSS/JS slider 有疑问。我使用简单的方法使其工作 - 较大的元素 (container) 在较小的 (area) 中, overflow hidden 。我在 slider btnbtn2 上方有按钮来控制更大元素 (container)

的移动

问题 是它只在到达“第一张幻灯片”(蓝色)之前有效,然后就不可能滑动到第二张幻灯片(红色)。

fiddle :https://jsfiddle.net/26r32hb6

var button = document.querySelector('.btn');
var button2 = document.querySelector('.btn2');
var container = document.querySelector('.container');

button.style.background = 'blue';
button2.style.background = 'blue';
container.style.backgroundColor = 'blue';

button.onclick = function() {
if (container.style.left == 0) {
container.style.left = '-100.5%';
container.style.backgroundColor = 'red';
button.style.backgroundColor = 'red';
button2.style.backgroundColor = 'red';}
else if (container.style.left == '-100.5%') {
container.style.left = '-200.5%';
container.style.backgroundColor = 'purple';
button.style.backgroundColor = 'purple';
button2.style.backgroundColor = 'purple';
}
};
button2.onclick = function() {
if (container.style.left == '-100.5%') {
container.style.left = 0;
container.style.backgroundColor = 'blue';
button.style.backgroundColor = 'blue';
button2.style.backgroundColor = 'blue';}
else if (container.style.left == '-200.5%') {
container.style.left = '-100.5%';
container.style.backgroundColor = 'red';
button.style.backgroundColor = 'red';
button2.style.backgroundColor = 'red';
}
};
.wrapbtns {
width: 365px;
height: 50px;
background: yellow;
}
.btn {
width: 50px;
height: 50px;
float: right;
}
.btn2 {
width: 50px;
height: 50px;
float: left;
margin-right: 5px;
}
.btn, .btn2 {
transition-duration: 1s;
display: flex;
justify-content: center;
align-items: center;
color: white;
cursor: pointer;

}
.area {
width: 365px;
height: 250px;
overflow: hidden; /* This */
position: relative;
}
.container {
background: blue;
width: 1100px;
height: 250px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: left;
align-items: center;
left: 0;
position: absolute;
transition-duration: 1s;
}
.box {
background: yellow;
min-width: 100px;
height: 100px;
margin: 1%;
display: flex;
justify-content: center;
align-items: center;
color: black;
font-size: 48px;
transition-duration: 500ms;
}
<div class="wrapbtns">
<div class="btn2">-X</div>
<div class="btn">X</div></div>

<div class="area">
<div class="container">

<div class="box">a</div>
<div class="box">b</div>
<div class="box">c</div>
<div class="box">d</div>
<div class="box">e</div>
<div class="box">f</div>
<div class="box">g</div>
<div class="box">h</div>
<div class="box">i</div>
<div class="box">j</div>
<div class="box">k</div>
<div class="box">l</div>
<div class="box">m</div>
<div class="box">n</div>
<div class="box">o</div>
<div class="box">p</div>
<div class="box">q</div>
<div class="box">r</div>
</div>
</div>

最佳答案

首先,如果您使用类似 jQuery 的东西,所有这一切都会变得容易得多.但假设您只想使用 javascript 执行此操作,这是我的解决方案:

(注意:即使您使用 jQuery,该技术也与下面解释的技术非常相似)

fiddle :https://jsfiddle.net/Bintang/kvowcj6w/

var button = document.querySelector('.btn');
var button2 = document.querySelector('.btn2');
var container = document.querySelector('.container');
var area = document.querySelector('.area');

button.style.background = 'blue';
button2.style.background = 'blue';
container.style.backgroundColor = 'blue';

var slideCurrent = 0;
var slideTotal = Math.floor(container.offsetWidth / area.offsetWidth);
var slideWidth = area.offsetWidth;
var slideColors = ['blue', 'red', 'purple'];

button.onclick = function() {

if (slideCurrent < slideTotal - 1) {
var left = container.style.left;
var leftInInteger = parseInt(left.replace("px", "")) || 0;
container.style.left = (leftInInteger - slideWidth) + "px"
slideCurrent += 1;

//STYLING
container.style.backgroundColor = slideColors[slideCurrent];
button.style.backgroundColor = slideColors[slideCurrent];
button2.style.backgroundColor = slideColors[slideCurrent];
}

};
button2.onclick = function() {

if (slideCurrent > 0) {
var left = container.style.left;
var leftInInteger = parseInt(left.replace("px", "")) || 0;
container.style.left = (leftInInteger + slideWidth) + "px";
slideCurrent -= 1;

//STYLING
container.style.backgroundColor = slideColors[slideCurrent];
button.style.backgroundColor = slideColors[slideCurrent];
button2.style.backgroundColor = slideColors[slideCurrent];
}

};

解释:

不是每次要滑动时都使用 if & else 来设置“left”的具体值,而是增加每次你想向左滑动时“左”,每次你想向右滑动时递减该值。 (看下面的代码)

var slideCurrent = 0;
var slideTotal = Math.floor(container.offsetWidth / area.offsetWidth);
var slideWidth = area.offsetWidth;
var slideColors = ['blue', 'red', 'purple'];

button.onclick = function() {

if (slideCurrent < slideTotal - 1) {
var left = container.style.left;
var leftInInteger = parseInt(left.replace("px", "")) || 0;
container.style.left = (leftInInteger - slideWidth) + "px"
slideCurrent += 1;

//STYLING
container.style.backgroundColor = slideColors[slideCurrent];
button.style.backgroundColor = slideColors[slideCurrent];
button2.style.backgroundColor = slideColors[slideCurrent];
}

};

上面的代码做了什么:

1.每次点击按钮都会检查当前幻灯片是否是最后一张幻灯片

if (slideCurrent < slideTotal - 1) {

2.如果它不是最后一张幻灯片,它将获取“container”的当前“left”值,然后它将删除“px”并将其从字符串转换为整数(使用 parseInt() 函数),但如果未定义“left”属性,它将被替换为“0”的值

var left = container.style.left;
var leftInInteger = parseInt(left.replace("px", "")) || 0;

3. 现在它具有整数“left”的当前值,它可以将值减去“区域”(顶部较小的元素)的宽度,因此它将向右滑动并增加 slideCurrent 逐一变化

container.style.left = (leftInInteger - slideWidth) + "px"
slideCurrent += 1;

4.最后,它会将“容器”的颜色设置为指定的颜色

//STYLING
container.style.backgroundColor = slideColors[slideCurrent];
button.style.backgroundColor = slideColors[slideCurrent];
button2.style.backgroundColor = slideColors[slideCurrent];

然后你可以反其道而行之,向另一个方向滑动。

关于javascript - 循环的 CSS/JS slider 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677282/

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