香草javascript轮播不滑动 [英] vanilla javascript carousel not sliding

查看:42
本文介绍了香草javascript轮播不滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带主要div的简单轮播.在主div内有一个宽度为6000像素的ul,每当我单击向右或向左箭头时,该ul就会滑动.

i have a simple carousel with main div. inside the main div there is a ul that has width of 6000px that slides whenever i click either of right or left arrows.

我在两个箭头上都附加了事件侦听器,因此当我单击向左箭头时,我向该ul(slider)的当前marginLeft和向右箭头的marginRight 300px添加了300px.

i attached event listeners to both the arrows so when i click left arrow I add 300px to the current marginLeft of that ul(slider) and marginRight 300px for the right arrow.

问题1:当我单击向左箭头时ul(滑块)移动,但仅单击1次,即使我向marginLeft添加300px而不是仅添加300px,它也不会移动,这是我常见的错误

Problem 1: when i click left arrow the ul(slider) moves but only 1 time when i click second time it doesn't move even though i add 300px to marginLeft instead of just adding 300px which is a common mistake i seen.

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

Problem 2: the right arrow when i click it doesn't slide the ul(slider) at all.

Codepen: 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);

推荐答案

首先,您应该像这样增加:

Firstly you should increase like this:

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

因为"300px" +"300px" ="300px300px" 这样做是错误的.

您将无法获得 element.style.marginLeft 之类的样式.

And you can't get style like 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轮播不滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆