首页 > 解决方案 > JS加后缀整数并从1旋转到4

问题描述

我必须将类的后缀更改为.background-1.background-2。在 4 之后它又回到 1。就像一个旋转木马。

我的触发器是点击,它可以在下面看到:

$('.arrow-next').click(function () {
        // Here should be the code
});

标签: javascriptjquerycss

解决方案


这会做...

const $carousel = $('.carousel');
$('.arrow-next').click(function() {
  if ($carousel.classList.contains('background-1')) {
    $el.classList.remove('background-1');
    $el.classList.add('background-2');
  } else if ($carousel.classList.contains('background-2')) {
    $el.classList.remove('background-2');
    $el.classList.add('background-3');
  } else if ($carousel.classList.contains('background-3')) {
    $el.classList.remove('background-3');
    $el.classList.add('background-4');
  } else if ($carousel.classList.contains('background-4')) {
    $el.classList.remove('background-4');
    $el.classList.add('background-1');
  }
});

推荐阅读