gpt4 book ai didi

JavaScript slider 无法正常工作

转载 作者:行者123 更新时间:2023-11-28 00:56:10 26 4
gpt4 key购买 nike

我正在尝试制作一个 slider ,但我遇到了一个问题:slideWidth 变量(即幻灯片的宽度)总是相同的,所以当它到达第二个时slide 不想再往前走,和之前的 slide 一样,因为固定的变量值,它会滑到空白处。 + 它不想动画。如何解决这个问题?

请查看代码:

// Slider
const slider_wrapp = document.querySelector('.tract-slider');
const slider = document.querySelector('.tract-slider-wrapp');
var slide = document.getElementsByClassName('tract-slide');

const leftBtn = document.querySelector('.slide-left');
const rightBtn = document.querySelector('.slide-right');

let swWidth = slider_wrapp.clientWidth;
let sliderWidth = swWidth * slide.length;
let slideWidth = swWidth;

slider.style.width = sliderWidth + "px";

for (var i = 0; i < slide.length; i++) {
slide.item(i).style.width = " " + swWidth + "px";
}

function moveRight() {
slider.style.transform = "translateX(" + (-slideWidth) + "px)";
}

function moveLeft() {
slider.style.transform = "translateX(" + (slideWidth) + "px)";
}

rightBtn.addEventListener("click", function() {
moveRight()
});

leftBtn.addEventListener("click", function() {
moveLeft();
});
.tract-slider {
width: 100%;
height: 75vh;
position: relative;
overflow: hidden;
}

.tract-slider-wrapp {
height: 100%;
position: relative;
-webkit-transition: translate 350ms cubic-bezier(.08, .13, 0, .81);
-o-transition: translate 350ms cubic-bezier(.08, .13, 0, .81);
transition: translate 350ms cubic-bezier(.08, .13, 0, .81);
}

.tract-slide {
height: 100%;
float: left;
position: relative;
display: block;
background-position: center;
-webkit-background-size: cover;
background-size: cover;
}

.tract-slide:nth-child(1) {
background-image: url("https://static.pexels.com/photos/126282/pexels-photo-126282.jpeg");
}

.tract-slide:nth-child(2) {
background-image: url("https://static.pexels.com/photos/29017/pexels-photo-29017.jpg");
}

.tract-slide:nth-child(3) {
background-image: url("https://static.pexels.com/photos/70760/dandelion-dandelion-seeds-taraxacum-fluffy-70760.jpeg");
}

.tract-slider-control {
position: absolute;
left: 0;
bottom: 0;
background: #ffffff;
padding: 1em;
}

.tract-slider-btn {
display: inline-block;
cursor: pointer;
margin-left: 1em;
}

.tract-slider-btn:nth-child(1) {
margin-left: 0;
}
<div class="tract-slider">
<div class="tract-slider-wrapp">
<div class="tract-slide"></div>
<div class="tract-slide"></div>
<div class="tract-slide"></div>
</div>
<div class="tract-slider-control">
<div class="tract-slider-btn slide-left">Prev</div>
<div class="tract-slider-btn slide-right">Next</div>
</div>
</div>

P.S. If you know the solution please write it in JavaScript not JQuery

最佳答案

您的问题是您错过了计算 slideWrapper 的 translateX 位置:(slideWidth var)

要修复它,您必须检查并分配一个新的计算值,而不是始终将其设置为 slider 宽度。

请注意,我在 moveRight()moveLeft() 中都添加了计算

请参阅下面的工作片段:(仅使用 js)

// Slider
const slider_wrapp = document.querySelector('.tract-slider');
const slider = document.querySelector('.tract-slider-wrapp');
var slide = document.getElementsByClassName('tract-slide');

const leftBtn = document.querySelector('.slide-left');
const rightBtn = document.querySelector('.slide-right');

let swWidth = slider_wrapp.clientWidth;
let sliderWidth = swWidth * slide.length;
let slideWidth = 0;

slider.style.width = sliderWidth + "px";

for (var i = 0; i < slide.length; i++) {
slide.item(i).style.width = " " + swWidth + "px";
}

function moveRight() {
slideWidth === sliderWidth - swWidth ? slideWidth = 0 : slideWidth += swWidth;
slider.style.transform = "translateX(" + (-slideWidth) + "px)";
}

function moveLeft() {
slideWidth === 0 ? slideWidth = sliderWidth-swWidth : slideWidth -= swWidth;
slider.style.transform = "translateX(" + (-slideWidth) + "px)";
}

rightBtn.addEventListener("click", function() {
moveRight()
});

leftBtn.addEventListener("click", function() {
moveLeft();
});
.tract-slider {
width: 100%;
height: 75vh;
position: relative;
overflow: hidden;
}

.tract-slider-wrapp {
height: 100%;
position: relative;
-webkit-transition: all 350ms cubic-bezier(.08, .13, 0, .81);
-o-transition: all 350ms cubic-bezier(.08, .13, 0, .81);
transition: all 350ms cubic-bezier(.08, .13, 0, .81);
}

.tract-slide {
height: 100%;
float: left;
position: relative;
display: block;
background-position: center;
-webkit-background-size: cover;
background-size: cover;
}

.tract-slide:nth-child(1) {
background-image: url("https://static.pexels.com/photos/126282/pexels-photo-126282.jpeg");
}

.tract-slide:nth-child(2) {
background-image: url("https://static.pexels.com/photos/29017/pexels-photo-29017.jpg");
}

.tract-slide:nth-child(3) {
background-image: url("https://static.pexels.com/photos/70760/dandelion-dandelion-seeds-taraxacum-fluffy-70760.jpeg");
}

.tract-slider-control {
position: absolute;
left: 0;
bottom: 0;
background: #ffffff;
padding: 1em;
}

.tract-slider-btn {
display: inline-block;
cursor: pointer;
margin-left: 1em;
}

.tract-slider-btn:nth-child(1) {
margin-left: 0;
}
<div class="tract-slider">
<div class="tract-slider-wrapp">
<div class="tract-slide"></div>
<div class="tract-slide"></div>
<div class="tract-slide"></div>
</div>
<div class="tract-slider-control">
<div class="tract-slider-btn slide-left">Prev</div>
<div class="tract-slider-btn slide-right">Next</div>
</div>
</div>

关于JavaScript slider 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44809728/

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