gpt4 book ai didi

javascript - Vanilla javascript轮播不滑动

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

我有一个带有主要 div 的简单旋转木马。在主 div 中有一个宽度为 6000px 的 ul,每当我单击向右或向左箭头时,它就会滑动。

我将事件监听器附加到两个箭头,所以当我单击左箭头时,我将 300px 添加到该 ul(slider) 的当前 marginLeft 和右箭头的 marginRight 300px。

问题 1:当我单击向左箭头时,ul( slider )移动但只有 1 次当我单击第二次时它不会移动,即使我将 300px 添加到 marginLeft 而不是仅添加 300px 这是我看到的一个常见错误.

问题 2:当我单击右箭头时,它根本不会滑动 ul(slider)。

代码笔:https://codepen.io/anon/pen/bXBaYW?editors=0110

const carousel = document.getElementById('carousel');
const leftArrow = carousel.querySelector('.carousel-left-arrow');
const rightArrow = carousel.querySelector('.carousel-right-arrow');
const slides = carousel.querySelector('.slides');

function scrollLeft() {
slides.style.marginLeft += '300px';
console.log(slides.style.marginLeft);
}

function scrollRight() {
slides.style.marginLeft -= '300px';
console.log(slides.style.marginLeft);
}

leftArrow.addEventListener('click' , scrollLeft);
rightArrow.addEventListener('click' , scrollRight);

最佳答案

首先你应该这样增加:

 let marginLeft = 0; /* first define this as global */
marginLeft += 300;
slides.style.marginLeft = marginLeft + "px";

因为,"300px"+ "300px"= "300px300px" 你这样做的方式是错误的。

而且您无法获得像 element.style.marginLeft 这样的样式。

应该使用getComputedStyle(element).property

这是可行的。

const carousel = document.getElementById('carousel');
const leftArrow = carousel.querySelector('.carousel-left-arrow');
const rightArrow = carousel.querySelector('.carousel-right-arrow');
const slides = carousel.querySelector('.slides');

let marginLeft = 0;

function scrollLeft() {
marginLeft += 300;
slides.style.marginLeft = marginLeft + "px";
console.log(getComputedStyle(slides).marginLeft);
}

function scrollRight() {
marginLeft -= 300;
slides.style.marginLeft = marginLeft + "px";
console.log(getComputedStyle(slides).marginLeft);
}

leftArrow.addEventListener('click', scrollLeft);
rightArrow.addEventListener('click', scrollRight);
#carousel {
width: 90%;
margin: 0 auto;
margin-top: 20px;
overflow: hidden;
position: relative;
}

#carousel .arrow {
position: absolute;
top: 32%;
background-color: rgba(255, 255, 255, 0.7);
border-radius: 0%;
cursor: pointer;
width: 20px;
}

#carousel .arrow img {
margin-top: 10px;
max-width: 100%;
}

#carousel .carousel-left-arrow {
width: 25px;
left: 0;
margin-left: 5px;
}

#carousel .carousel-right-arrow {
right: 0;
width: 25px;
}

#carousel .slides {
width: 6000px;
overflow: hidden;
list-style: none;
padding: 0;
transition: 0.2s;
margin-left: 0;
margin-right: 0
}

#carousel .slide {
float: left;
margin: 0 5px;
text-decoration: none;
transform: 0.2s;
color: whitesmoke;
}

#carousel .slides img {
width: 300px
}
<div id="carousel">
<span class="carousel-left-arrow arrow"><</span>
<ul class="slides">
<a href='#' class="slide">
<img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
</a>
<a href='#' class="slide">
<img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
</a>
<a href='#' class="slide">
<img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
</a>
<a href='#' class="slide">
<img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
</a>
<a href='#' class="slide">
<img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
</a>
<a href='#' class="slide">
<img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
</a>
</ul>
<span class="carousel-right-arrow arrow">></span>
</div>

关于javascript - Vanilla javascript轮播不滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233095/

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