gpt4 book ai didi

javascript - 垂直 Javascript slider ,比它应该滑动的更多

转载 作者:行者123 更新时间:2023-11-30 19:52:49 25 4
gpt4 key购买 nike

我正在尝试制作一个垂直图像 slider ,它可以精确地滑动 slider 的宽度。

我现在已经尝试了很多个小时来让它工作,我认为这应该是相当简单的事情,但我根本无法弄清楚我做错了什么,我的大脑快要崩溃了还认为我现在把它弄得太复杂了。

slider 的宽度为 548px,高度为 137px。

slider 内的图像宽度为 548 像素除以 2。

Fiddle of the slider

正如您在下面看到的, slider 移动的幅度比应有的略大 enter image description here

JavaScript 模块(是的,向左移动和向右移动是不同的,这是因为我一直在测试很多不同的方法。

如果我为此更改正确的功能:

  var horizontalMoveRight = function(wrapper) {
wrapper.scrollLeft += elemScroll;

if (elemMoved == elemMove || elemMoved > elemMove) {
clearInterval(slideInterval);
elemMoved = 0;
} else {
elemMoved++;
}
};

它几乎可以工作,但在点击几下后仍然会稍微移动一些。

var horizontalSlider = (function() {

var slideParent;
var slideLeftButton;
var slideRightButton;
var slideInterval;
var slideTimer;

var elemScroll = 0;
var elemLength = 0;
var elemMove = 0;
var elemMoved = 0;
var lastMove = false;


var horizontalConstruct = function(parenElement, lButn, rButn, slidTimer, eleScroll) {
slideParent = parenElement;
slideLeftButton = document.querySelectorAll(lButn);
slideRightButton = document.querySelectorAll(rButn);
slideTimer = slidTimer;
elemScroll = eleScroll;

Array.from(slideLeftButton).forEach(elem => elem.addEventListener("click", horizontalClickLeft, false));
Array.from(slideRightButton).forEach(elem => elem.addEventListener("click", horizontalClickRight, false));
};

var horizontalClickLeft = function() {
clearInterval(slideInterval);
elemMoved = 0;
var wrapper = this.parentNode.querySelector(slideParent);

elemLength = wrapper.clientWidth;
elemMove = (elemLength - elemScroll) / elemScroll;

slideInterval = setInterval(function() {
horizontalMoveLeft(wrapper);
}, slideTimer);
};
var horizontalClickRight = function() {
clearInterval(slideInterval);
elemMoved = 0;
var wrapper = this.parentNode.querySelector(slideParent);

elemLength = wrapper.clientWidth;
elemMove = (elemLength - elemScroll) / elemScroll;

slideInterval = setInterval(function() {
horizontalMoveRight(wrapper);
}, slideTimer);
};

var horizontalMoveRight = function(wrapper) {
if (elemMoved == elemMove || elemMoved > elemMove) {
clearInterval(slideInterval);
elemMoved = 0;
} else {
if (elemMoved + 1 > elemMove) {
lastMove = Number(((elemMove - elemMoved) * 10));
}

if (lastMove === false) {
wrapper.scrollLeft += elemScroll;
} else {
wrapper.scrollLeft += lastMove;
lastMove = false;
}

elemMoved++;
}
};

var horizontalMoveLeft = function(wrapper) {
wrapper.scrollLeft += -elemScroll;

if (elemMoved == elemMove || elemMoved > elemMove) {
clearInterval(slideInterval);
elemMoved = 0;
} else {
elemMoved++;
}
};


return {
horizontalConstruct: horizontalConstruct,
horizontalClickLeft: horizontalClickLeft,
horizontalClickRight: horizontalClickRight

};
})();

horizontalSlider.horizontalConstruct(".slideInner", ".slideLeftHorizontal", ".slideRightHorizontal", 7, 10);
.slideInner {
position: relative;
width: 548px;
height: 137px;
overflow: hidden;
display: flex;
}

.slideInner a,
.slideInner a img {
width: calc(548px / 2);
}

.slideRightHorizontal,
.slideLeftHorizontal {
position: absolute;
min-width: 45px;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
z-index: 2;
cursor: pointer;
background-color: rgba(0, 0, 0, 0.5);
}

.slideLeftHorizontal {
left: 0;
color: #fff;
}

.slideRightHorizontal {
right: 0;
color: #fff;
}

.overflowh {
overflow: hidden;
}

.slideT1M {
margin: 20px 0px;
border: 1px solid #000;
display: flex;
width: 548px;
position: relative;
}
<div class="slideT1M">
<div class="slideLeftHorizontal">
L
</div>
<div class="slideInner">
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
</div>
<div class="slideRightHorizontal">
R
</div>
</div>

最佳答案

你的错误在这一行:

elemMove = (elemLength - elemScroll)/elemScroll;

只是将其更改为:

elemMove = elemLength/elemScroll;

var horizontalSlider = (function() {

var slideParent;
var slideLeftButton;
var slideRightButton;
var slideInterval;
var slideTimer;

var elemScroll = 0;
var elemLength = 0;
var elemMove = 0;
var elemMoved = 0;
var lastMove = false;


var horizontalConstruct = function(parenElement, lButn, rButn, slidTimer, eleScroll) {
slideParent = parenElement;
slideLeftButton = document.querySelectorAll(lButn);
slideRightButton = document.querySelectorAll(rButn);
slideTimer = slidTimer;
elemScroll = eleScroll;

Array.from(slideLeftButton).forEach(elem => elem.addEventListener("click", horizontalClickLeft, false));
Array.from(slideRightButton).forEach(elem => elem.addEventListener("click", horizontalClickRight, false));
};

var horizontalClickLeft = function() {
clearInterval(slideInterval);
elemMoved = 0;
var wrapper = this.parentNode.querySelector(slideParent);

elemLength = wrapper.clientWidth;
elemMove = (elemLength - elemScroll) / elemScroll;

slideInterval = setInterval(function() {
horizontalMoveLeft(wrapper);
}, slideTimer);
};
var horizontalClickRight = function() {
clearInterval(slideInterval);
elemMoved = 0;
var wrapper = this.parentNode.querySelector(slideParent);

elemLength = wrapper.clientWidth;
elemMove = elemLength / elemScroll;

slideInterval = setInterval(function() {
horizontalMoveRight(wrapper);
}, slideTimer);
};

var horizontalMoveRight = function(wrapper) {
if (elemMoved == elemMove || elemMoved > elemMove) {
clearInterval(slideInterval);
elemMoved = 0;
} else {
if (elemMoved + 1 > elemMove) {
lastMove = Number(((elemMove - elemMoved) * 10));
}

if (lastMove === false) {
wrapper.scrollLeft += elemScroll;
} else {
wrapper.scrollLeft += lastMove;
lastMove = false;
}

elemMoved++;
}
};

var horizontalMoveLeft = function(wrapper) {
wrapper.scrollLeft += -elemScroll;

if (elemMoved == elemMove || elemMoved > elemMove) {
clearInterval(slideInterval);
elemMoved = 0;
} else {
elemMoved++;
}
};


return {
horizontalConstruct: horizontalConstruct,
horizontalClickLeft: horizontalClickLeft,
horizontalClickRight: horizontalClickRight

};
})();

horizontalSlider.horizontalConstruct(".slideInner", ".slideLeftHorizontal", ".slideRightHorizontal", 7, 10);
.slideInner {
position: relative;
width: 548px;
height: 137px;
overflow: hidden;
display: flex;
}

.slideInner a,
.slideInner a img {
width: calc(548px / 2);
}

.slideRightHorizontal,
.slideLeftHorizontal {
position: absolute;
min-width: 45px;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
z-index: 2;
cursor: pointer;
background-color: rgba(0, 0, 0, 0.5);
}

.slideLeftHorizontal {
left: 0;
color: #fff;
}

.slideRightHorizontal {
right: 0;
color: #fff;
}

.overflowh {
overflow: hidden;
}

.slideT1M {
margin: 20px 0px;
border: 1px solid #000;
display: flex;
width: 548px;
position: relative;
}
<div class="slideT1M">
<div class="slideLeftHorizontal">
L
</div>
<div class="slideInner">
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/5775/calculator-scientific.jpg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
<a href="#">
<img src="https://images.pexels.com/photos/212286/pexels-photo-212286.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
</a>
</div>
<div class="slideRightHorizontal">
R
</div>
</div>

关于javascript - 垂直 Javascript slider ,比它应该滑动的更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54315351/

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