javascript - 使 slider 上的点处于事件状态

标签 javascript html css slider

我有这个使用纯 JS 创建的 Slider 示例。

slider 效果很好。剩下要做的唯一一件事就是激活三个点,这样当第一张幻灯片打开时,第一个点激活,显示与其他点不同的颜色,依此类推。此外,您应该能够在单击点时打开正确的幻灯片,因此第一个点打开第一张幻灯片,第二个点打开第二张幻灯片,依此类推。

你能帮我实现这个目标吗?您可以在下面找到源代码。

const nextBtn = document.querySelector('.nextBtn');
const prevBtn = document.querySelector('.prevBtn');
const container = document.querySelector('.images');
const offers = document.getElementById('offers');
const link = document.getElementById('links');

let colors = ['#7f86ff', '#2932d1', '#00067f'];
let currentSlide = 0;

let texts = ['Change1', 'Change2', 'Change3'];
let currentText = 0;

let links = ['<a href="https:www.google.com">Link1</a>', '<a href="https:www.is.fi">Link2</a>', '<a href="https:www.betsson.com">Link3</a>'];
let currentLink = 0;

function updateSlide(direction) {
  currentSlide =
    (colors.length + currentSlide + direction)
  % colors.length;
  container.style.backgroundColor = colors[currentSlide];
  container.animate([{opacity:'0.1'}, {opacity:'1.0'}],
  {duration: 200, fill:'forwards'})
}

function updateText(direction) {
  currentText =
    (texts.length + currentText + direction)
  % texts.length;
  offers.innerHTML = texts[currentText];
  offers.animate([{transform:'translateY(-50px)', opacity:'0.0'}, {transform:'translateY(0)', opacity:'1.0'}],
  {duration: 200, fill:'forwards'})
}

function updateLink(direction) {
  currentLink =
    (links.length + currentLink + direction)
  % links.length;
  link.innerHTML = links[currentLink];
  link.animate([{transform:'scale(0,0)'}, {transform:'scale(1.1)'}],
  {duration: 200, fill:'forwards'})
}

updateSlide(0);
updateText(0);
updateLink(0);

nextBtn.addEventListener('click', nextSlide);
prevBtn.addEventListener('click', prevSlide);

function nextSlide() {
  updateSlide(+1);
  updateText(+1);
  updateLink(+1);
  clearInterval(myInterval);
}

function prevSlide() {
  updateSlide(-1);
  updateText(-1);
  updateLink(-1);
  clearInterval();
  clearInterval(myInterval);
}

var myInterval = window.setInterval(function(){
  updateSlide(+1),updateText(+1),updateLink(+1);  }, 
  8000);
body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: lightblue;
}

.images {
  background-color: #4047c9;
  flex: 0 0 80%;
  min-height: 70vh;
  border-radius: 10px;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  color: white;
}

#links {
  text-decoration: none;
  color: white;
  border: solid 2px white;
  border-radius: 3px;
  padding: 5px 10px;
}

#links:hover {
  background-color: #000238;
}

a {
  color: white;
  text-decoration: none;
}

.dots {
  display: flex;
  margin-top: 120px;
  margin-bottom: 50px;
}

#dot1, #dot2, #dot3 {
  width: 20px;
  height: 20px;
  background-color: rgb(147, 151, 249);
  border-radius: 50%;
  margin: 0px 5px;
  cursor: pointer;
}

#dot1:active, #dot2:active, #dot3:active {
  background-color: #fff;
}

.btn {
  display: inline-block;
  background: white;
  color: black;
  padding: 10px;
  border: none;
  cursor: pointer;
}

.prevBtn {
  position: absolute;
  top: 50%;
  left: 0;
  transform: translate(-50%, -50%);
}

.nextBtn {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translate(50%, -50%);
}

.btn:active {
  background-color: grey;
  color: white;
}

.btn:hover {
  background-color: grey;
  color: white;
}
<body>

  <div class="images">
    <button type="button" class="btn prevBtn">Prev Btn</button>
    <button type="button" class="btn nextBtn">Next Btn</button>
    <h1 id="offers">Changing text</h1>
    <a href="#" id="links">Links</a>
    <div class="dots">
    <span id="dot1"></span>
    <span id="dot2"></span>
    <span id="dot3"></span>
    </div>
  </div>
      
</body>

最佳答案

首先,根据

https://developer.mozilla.org/en-US/docs/Web/CSS/:active

The :active CSS pseudo-class represents an element (such as a button) that is being activated by the user.

所以如果你想让你的点处于事件状态,你必须编写一种不同的方式来让它们处于事件状态,因为它们当前是 <span>标签,我建议给他们一类 .active ,并添加 Javascript 代码以将该类添加到它们,或在 Javascript 函数中以编程方式添加该样式。

根据您的其他要求,您很可能还必须将这些点设为 <a>标记或类似的东西,这样你就可以在它们上面添加功能,让点击这些点将你带到任何幻灯片。大概是这样的:

function dot1Click() {
  updateSlide(1);
  updateText(1);
  updateLink(1);
  dot1.style.backgroundColor = #fff;
}

然后你应该有一些你想要的东西。当我有更多时间整理代码片段时,我会回到这个问题,但我想给你一些东西来帮助你开始!

关于javascript - 使 slider 上的点处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55047613/

相关文章:

javascript - 无法从按钮中删除禁用属性

javascript - HTML 选择标签自动完成

html - 如何对齐图像旁边的标题和副标题?

javascript - offset().top 在 Safari 中不起作用

javascript - package.json 脚本中的 Node 支持哪些类似 shell 的语法?

javascript - 检查 IF 语句中的复选框是否已选中

html - 如何摆脱页面上的滚动条?

css - 使用 css 更改 svg 图像的颜色

html - 问题关闭响应式菜单

css - 如何在高对比度模式下显示 CSS 按钮