CSS/JS 滑块与循环的问题
CSS/JS Slider issue with loop
我对 CSS/JS 滑块有问题。我使用了简单的方法来使其工作 - 较大的元素(container
)在较小的(area
)中隐藏溢出。我在滑块上方有按钮 btn
和 btn2
来控制更大元素的移动 ( containter
)
问题是它只能在到达"第一张幻灯片"(蓝色)之前起作用,然后无法滑动到第二张幻灯片(红色)。
小提琴: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,该技术也与下面解释的技术非常相似)
小提琴: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
来设置"左"的特定值,而是每次要向左滑动时递增"左"的值,每次要向右滑动时递减值。(看下面的代码)
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.如果不是最后一张幻灯片,它将获得"容器"的当前"左"值,然后它将删除"px"并将其从字符串转换为整数(使用 parseInt()
函数),但如果尚未定义"left"属性,它将替换为"0"的值
var left = container.style.left;
var leftInInteger = parseInt(left.replace("px", "")) || 0;
3.现在它的当前值是整数"left",它可以将值减减"area"(顶部较小的元素)的宽度,因此它将向右滑动并将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];
然后你可以做相反的事情滑到另一个方向。
相关文章:
- Jquery图像循环问题
- JavaScript的循环问题,将值插入数组将不起作用
- 数学和循环问题
- Java脚本循环问题
- NetSuite行项目删除内部循环问题
- 附加仅适用于第一个子循环问题
- 简单的 do/while 循环无限循环问题
- Javascript 和 HTML5 循环问题
- 仅在jQuery函数内部循环问题
- 嵌套循环问题
- 谷歌地图标记的循环问题
- 函数在 For 循环问题中
- PHP while 循环问题在 iOS 5 Safari 中
- JavaScript 加载重置时间戳循环问题
- Jquery 循环问题
- 与jquery结合使用时的Javascript循环问题
- Javascript中的ID和for循环问题
- 无限滚动(mysql,php&js)循环问题
- Facebook查询循环问题
- jQueryrotate if else循环问题