carousel cards in Javascript css html

huangapple go评论58阅读模式
英文:

carousel cards in Javascript css html

问题

这是我尝试做的参考图像:

我已经尝试制作带有卡片的轮播,但是在按下按钮时无法使指示器和卡片前进,如图像所示。您能否帮我知道如何制作指示器并在单击时前进卡片?

let targeta = document.querySelectorAll('.card');
let puntos = document.querySelectorAll('.secciones .punto');

for (i = 0; i < targeta.length; i++) {
  puntos[i].addEventListener('click', function(evento) {
    for (i = 0; i < puntos.length; i++) {
      puntos[i].classList.remove('activo');
    }
    clase_activo(evento);
  });

  targeta[i].addEventListener('click', function(evento) {
    for (i = 0; i < puntos.length; i++) {
      targeta[i].classList.remove('activo');
    }
    clase_activo(evento);
  });
}

function clase_activo(evento) {
  if (evento.target.classList.contains('activo')) {
    evento.target.classList.remove('activo');
  } else {
    evento.target.classList.add('activo');
  }
}
.product-list {
  background: #dfe6e9;
  padding: 3em;
}

.product-list .card {
  background: white;
  border-radius: 10px;
  padding: 1em;
  box-shadow: 0px 10px 5px #b2bec3;
  text-align: center;
  font-family: 'SpaceGrotesk-medium';
}

.card {
  height: 300px;
}

.product-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-column-gap: 20px;
  grid-row-gap: 40px;
}

.secciones {
  display: flex;
  align-items: center;
  justify-content: center;
}

.punto {
  background: #b4f028;
  height: 10px;
  width: 30px;
  border-radius: 5px;
  padding: 0 5px;
  margin: 0 2px;
}

.punto.activo {
  background: #f07e28;
}

.card.activo {
  background: #f07e28;
}
<section class="product-list">
  <h1>productos</h1>
  <div class="product-container">
    <div class="card 1 activo"></div>
    <div class="card 2"></div>
    <div class="card 3"></div>
    <div class="card 4"></div>
  </div>
  <div class="secciones">
    <div class="punto activo"></div>
    <div class="punto"></div>
    <div class="punto"></div>
    <div class="punto"></div>
  </div>
</section>
英文:

This is a reference image of what I'm trying to do
carousel cards in Javascript css html

I have tried to make a carousel with cards, but I have not been able to make the indicator of the buttons and the card advance when pressing the buttons, as shown in the image. Could you give me a hand to know how to make the indicator and the card advance as it is clicked?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let targeta = document.querySelectorAll(&#39;.card&#39;);
let puntos = document.querySelectorAll(&#39;.secciones .punto&#39;);

for (i = 0; i &lt; targeta.length; i++) {
  puntos[i].addEventListener(&#39;click&#39;, function(evento) {
    for (i = 0; i &lt; puntos.length; i++) {
      puntos[i].classList.remove(&#39;activo&#39;);
    }
    clase_activo(evento);
  });

  targeta[i].addEventListener(&#39;click&#39;, function(evento) {
    for (i = 0; i &lt; puntos.length; i++) {
      targeta[i].classList.remove(&#39;activo&#39;);
    }
    clase_activo(evento);
  });
}

function clase_activo(evento) {
  if (evento.target.classList.contains(&#39;activo&#39;)) {
    evento.target.classList.remove(&#39;activo&#39;);
  } else {
    evento.target.classList.add(&#39;activo&#39;);
  }
}

<!-- language: lang-css -->

.product-list {
  background: #dfe6e9;
  padding: 3em;
}

.product-list .card {
  background: white;
  border-radius: 10px;
  padding: 1em;
  box-shadow: 0px 10px 5px #b2bec3;
  text-align: center;
  font-family: &#39;SpaceGrotesk-medium&#39;;
}

.card {
  height: 300px;
}

.product-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-column-gap: 20px;
  grid-row-gap: 40px;
}

.secciones {
  display: flex;
  align-items: center;
  /*      background: #fb6969;*/
  justify-content: center;
}

.punto {
  background: #b4f028;
  /* #fb9769; */
  height: 10px;
  width: 30px;
  border-radius: 5px;
  padding: 0 5px;
  margin: 0 2px;
}

.punto.activo {
  background: #f07e28;
  /* #fb9769; */
}

.card.activo {
  background: #f07e28;
}

<!-- language: lang-html -->

&lt;section class=&quot;product-list&quot;&gt;
  &lt;h1&gt;productos&lt;/h1&gt;
  &lt;div class=&quot;product-container&quot;&gt;

    &lt;div class=&quot;card 1 activo&quot;&gt;
    &lt;/div&gt;

    &lt;div class=&quot;card 2 &quot;&gt;
    &lt;/div&gt;

    &lt;div class=&quot;card 3 &quot;&gt;
    &lt;/div&gt;

    &lt;div class=&quot;card 4 &quot;&gt;
    &lt;/div&gt;

  &lt;/div&gt;
  &lt;div class=&quot;secciones&quot;&gt;
    &lt;div class=&quot;punto  activo&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;punto &quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;punto &quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;punto &quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

答案1

得分: 0

// 用循环将点击事件附加到卡片和点
我使用了一个循环来为卡片和点附加点击事件。当点击卡片或点时,将从所有卡片和点中移除活动类,并添加到被点击的项目及其相应的项目。

// select all cards and dots
选择所有卡片和点
let cards = document.querySelectorAll('.card');
let dots = document.querySelectorAll('.secciones .punto');

// loop through each card and dot
遍历每个卡片和点
for (let i = 0; i < cards.length; i++) {
    // attach click event to each card
    // 为每个卡片附加点击事件
    cards[i].addEventListener('click', function() {
        for (let j = 0; j < cards.length; j++) {
            // remove the active class from all cards
            // 从所有卡片中移除活动类
            cards[j].classList.remove('activo');
            dots[j].classList.remove('activo');
        }
        // add the active class to the clicked card
        // 将活动类添加到被点击的卡片
        this.classList.add('activo');
        dots[i].classList.add('activo');
    });

    // attach click event to each dot
    // 为每个点附加点击事件
    dots[i].addEventListener('click', function() {
        for (let j = 0; j < cards.length; j++) {
            // remove the active class from all cards
            // 从所有卡片中移除活动类
            cards[j].classList.remove('activo');
            dots[j].classList.remove('activo');
        }
        // add the active class to the corresponding card
        // 将活动类添加到相应的卡片
        cards[i].classList.add('activo');
        this.classList.add('activo');
    });
}
英文:

I used a loop to attach click events to both the cards and the dots. When either a card or a dot is clicked, the active class is removed from all cards and dots, and then added to the clicked item and its corresponding item.

// select all cards and dots
let cards = document.querySelectorAll(&#39;.card&#39;);
let dots = document.querySelectorAll(&#39;.secciones .punto&#39;);

// loop through each card and dot
for (let i = 0; i &lt; cards.length; i++) {
    // attach click event to each card
    cards[i].addEventListener(&#39;click&#39;, function() {
        for (let j = 0; j &lt; cards.length; j++) {
            // remove the active class from all cards
            cards[j].classList.remove(&#39;activo&#39;);
            dots[j].classList.remove(&#39;activo&#39;);
        }
        // add the active class to the clicked card
        this.classList.add(&#39;activo&#39;);
        dots[i].classList.add(&#39;activo&#39;);
    });

    // attach click event to each dot
    dots[i].addEventListener(&#39;click&#39;, function() {
        for (let j = 0; j &lt; cards.length; j++) {
            // remove the active class from all cards
            cards[j].classList.remove(&#39;activo&#39;);
            dots[j].classList.remove(&#39;activo&#39;);
        }
        // add the active class to the corresponding card
        cards[i].classList.add(&#39;activo&#39;);
        this.classList.add(&#39;activo&#39;);
    });
}

huangapple
  • 本文由 发表于 2023年2月16日 03:00:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定