CSS/JS 滑块循环问题
CSS/JS Slider issue with loop
我对 CSS/JS 滑块有疑问。
我使用简单的方法使其工作 - 较大的元素 (container
) 位于较小的 (area
) 内,隐藏溢出。
我在滑块 btn
和 btn2
上方有按钮来控制更大元素的移动 (containter
)
问题是只有到"first slide"(蓝色)才有效,然后就不能滑到第二个(红色)了。
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”的具体值,你要做的是每次增加“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.Every 单击按钮时,它将检查当前幻灯片是否是最后一张幻灯片
if (slideCurrent < slideTotal - 1) {
2.if 这不是最后一张幻灯片,它将获取“容器”的当前“左”值,然后删除“px”并将其从字符串转换为
整数(使用 parseInt()
函数),但如果“左”属性 尚未定义,它将被替换为值“0”
var left = container.style.left;
var leftInInteger = parseInt(left.replace("px", "")) || 0;
3.Now 它具有整数的当前“left”值 它可以将值减去“区域”的宽度(顶部较小的元素)所以它会向右滑动并且将 slideCurrent
变量增加一个
container.style.left = (leftInInteger - slideWidth) + "px"
slideCurrent += 1;
4.Lastly,它会将“容器”的颜色设置为指定的颜色
//STYLING
container.style.backgroundColor = slideColors[slideCurrent];
button.style.backgroundColor = slideColors[slideCurrent];
button2.style.backgroundColor = slideColors[slideCurrent];
然后你可以反方向滑动到另一个方向
我对 CSS/JS 滑块有疑问。
我使用简单的方法使其工作 - 较大的元素 (container
) 位于较小的 (area
) 内,隐藏溢出。
我在滑块 btn
和 btn2
上方有按钮来控制更大元素的移动 (containter
)
问题是只有到"first slide"(蓝色)才有效,然后就不能滑到第二个(红色)了。
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”的具体值,你要做的是每次增加“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.Every 单击按钮时,它将检查当前幻灯片是否是最后一张幻灯片
if (slideCurrent < slideTotal - 1) {
2.if 这不是最后一张幻灯片,它将获取“容器”的当前“左”值,然后删除“px”并将其从字符串转换为
整数(使用 parseInt()
函数),但如果“左”属性 尚未定义,它将被替换为值“0”
var left = container.style.left;
var leftInInteger = parseInt(left.replace("px", "")) || 0;
3.Now 它具有整数的当前“left”值 它可以将值减去“区域”的宽度(顶部较小的元素)所以它会向右滑动并且将 slideCurrent
变量增加一个
container.style.left = (leftInInteger - slideWidth) + "px"
slideCurrent += 1;
4.Lastly,它会将“容器”的颜色设置为指定的颜色
//STYLING
container.style.backgroundColor = slideColors[slideCurrent];
button.style.backgroundColor = slideColors[slideCurrent];
button2.style.backgroundColor = slideColors[slideCurrent];
然后你可以反方向滑动到另一个方向