微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Javascript:轮播活动点和左右按钮

如何解决Javascript:轮播活动点和左右按钮

我是初学者级javascript开发人员。我决定使用纯Javascript制作轮播。我成功地滑动了转盘。我决定制作活动点(也可以单击),我希望在发生滑动和左右按钮时改变点背景颜色。我已经创建了点,但是我不知道如何在我的幻灯片功能以及左右按钮中实现这些点。这是我的代码

Also please explain me the logic. Thank you

const images = document.getElementById('imgs');
const allImages = document.querySelectorAll('#imgs img');
const leftBtn = document.getElementById('left');
const rightBtn = document.getElementById('right');
let index = 0;

function run() {
  const dot = [...document.getElementsByClassName('star')];
  index++;


  if (index > allImages.length - 1) {
    index = 0
    dot.forEach(i => i.classList.add('active'))
  }
  imgs.style.transform = `translateX(${-index * 500}px)`

}
const dot = allImages.forEach(i => {
  const elem = document.createElement('div');
  elem.classList.add('star');
  document.body.appendChild(elem)
})

let x = setInterval(run,2000);

images.onmouSEOver = () => {
  clearInterval(x)
}
images.onmouSEOut = () => {
  x = setInterval(run,2000);
}
*{
  Box-sizing: border-Box;
}

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.carousel {
  overflow: hidden;
  width: 500px;
  height: 500px;
  Box-shadow: 2px 2px 5px rgba(0,.3);
  border-radius: 5px;
}

.image-container {
  display: flex;
 transition: transform 300ms linear;
 transform: translateX(0);
}

img {
  width:500px;
  height: 500px;
  object-fit: cover;
}

.star{
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 10px;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
  background-color: #eeeeee;

}
.star.active{
  background-color: red;
}
button{
  cursor: pointer;
  position: relative;
  font-size: 18px;
  transition: 0.6s ease;
  user-select: none;
  height: 50px;
  width: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  align-content: center;
  top: calc(50% - 25px);
}
button:hover {
  background-color: rgba(0,0.8);
};

button.left {
  border-radius: 3px 0 0 3px;
  right: 0;
}
button.left {
  border-radius: 3px 0 0 3px;
  left: 0;
}
<button id="left">&#10094;</button>
  <button id="right">&#10095;</button>

  <div class="carousel">
    <div class="image-container" id="imgs" >
      <img src="https://images.unsplash.com/photo-1599736375341-51b0a848f3c7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="">
      <img src="https://images.unsplash.com/photo-1516026672322-bc52d61a55d5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="">
      <img src="https://images.unsplash.com/photo-1573081586928-127ecc7948b0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="">
      <img src="https://images.unsplash.com/flagged/photo-1572850005109-f4ac7529bf9f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="">
    </div>
  </div>

解决方法

您接近结果。在run函数中,您只需更改当前活动dot的类并将新的活动{}设置为有效。为了理解,我将您的dot变量重命名为dots

function run() {
  const dots = [...document.getElementsByClassName('star')];
  index++;


  if (index > allImages.length - 1) {
    index = 0
  }
  
  dots.forEach((dot,i) => {
    if (dot.classList.contains('active')) dot.classList.remove('active');
    if (i === index) dot.classList.add('active'));
  }
  imgs.style.transform = `translateX(${-index * 500}px)`

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。