简体   繁体   English

我的 JS/CSS 滑块在第一张幻灯片后停止工作。 我该如何解决这个问题?

[英]My JS/CSS Slider stops working after the first slide. How can i solve this?

I just made a slider in CSS and JS and I have encountered a problem which is: after the first slide the slider stops and i can't click the button.我刚刚在 CSS 和 JS 中制作了一个滑块,但遇到了一个问题:第一张幻灯片后滑块停止,我无法单击按钮。 I tried so many things, I changed the slide width, z-index of every element and even some parts of the scripts but nothing seems to work.我尝试了很多东西,我更改了每个元素的幻灯片宽度、z-index 甚至脚本的某些部分,但似乎没有任何效果。 It seems to work only in Firefox, but not in chrome.它似乎只适用于 Firefox,但不适用于 chrome。

document.addEventListener('DOMContentLoaded', () => {

const SLIDETIME = 500; //ms
// --------------------------

const backButton = document.querySelector('.wbn-slider-back-btn');
const forwardButton = document.querySelector('.wbn-slider-next-btn');
// Select all slides and convert node to array for easy handling
const allSlides = Array.from(document.querySelectorAll('.wbn-slide'));
 //const allSlides = [...document.querySelectorAll('.wbn-slide')];
let clickable = true;
let active = null;
let newActive = null;

function initSlider() {
  // Set the CSS transition on the slides to the value we specified in SLIDETIME above
  allSlides.forEach(slide =>
    slide.setAttribute(
      'style',
      `transition: transform ${SLIDETIME}ms ease;
                   animation-duration: ${SLIDETIME}ms`,
    ),
  );
}

function changeSlide(forward) {
  if (clickable) {
    clickable = false;
    active = document.querySelector('.active');
    const activeSlideIndex = allSlides.indexOf(active);

    if (forward) {
      console.log('activeSlideIndex: ', activeSlideIndex);
      console.log('allSlides.length: ', allSlides.length);
      console.log('new slide: ', (activeSlideIndex + 1) % allSlides.length);

      newActive = allSlides[(activeSlideIndex + 1) % allSlides.length];
      active.classList.add('slideOutLeft');
      newActive.classList.add('slideInRight', 'active');
    } else {
      console.log('activeSlideIndex: ', activeSlideIndex);
      console.log('allSlides.length: ', allSlides.length);
      console.log('new slide: ', (activeSlideIndex - 1 + allSlides.length) % allSlides.length);

      newActive =
        allSlides[
          (activeSlideIndex - 1 + allSlides.length) % allSlides.length
        ];
      active.classList.add('slideOutRight');
      newActive.classList.add('slideInLeft', 'active');
    }
  }
}

allSlides.forEach(slide => {
  slide.addEventListener('transitionend', e => {
    // Check for the old active transition and if clickable is false
    // to not trigger it more than once
    if (slide === active && !clickable) {
      clickable = true;
      // Remove all CSS animation classes on old active
      active.className = 'wbn-slide';
    }
  });
});

//Event listeners
forwardButton.addEventListener('click', () => {
  changeSlide(true);
});
backButton.addEventListener('click', () => {
  changeSlide(false);
});

// Init the slider
initSlider();

}); }); this is the JS code i'm using, and the following is the CSS:这是我正在使用的 JS 代码,以下是 CSS:

.slider-team{
  flex-shrink: 0;
    overflow: hidden;
    width: 39.41667vw ;
    height: 39.58333vw;
}
.wbn-slide {
  position: absolute;
  z-index: 500;
}
.wbn-slide img {
  object-fit: cover;
  pointer-events: none;
}

.slideInLeft {
  animation-name: animateInLeft;
  z-index: 800;
}

.slideInRight {
  animation-name: animateInRight;
  z-index: 800;
}

.slideOutLeft {
  transform: translateX(-0%);
}

.slideOutRight {
  transform: translateX(0%);
}
@keyframes animateInLeft {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(0%);
  }
}

@keyframes animateInRight {
  0% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(0%);
  }
}
.active{
  z-index: 1000;
}
.wbn-overlay-text {
  text-align: center;
  position: absolute;
  display: flex;
  display: -ms-flexbox;
  display: -webkit-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  color: white;
  top: 0;
  left: 0;
  z-index: 10000;
  opacity: 1;
}

.wbn-overlay-text .wbn-header {
  font-family: 'Italiana', serif;
  font-size: 80px;
  font-weight: 400;
}

.wbn-overlay-text .wbn-text {
  text-align: center;
  font-size: 22px;
  font-weight: 600;
  line-height: 30px;
  color: white;
  max-width: 30%;
}
.wbn-buttons {
  position: absolute;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-end;
  width: 48%;
  height: 100%;
  z-index: 20000 !important;
}

.wbn-slider-back-btn,
.wbn-slider-next-btn {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #fff;
  font-size: 20px;
  opacity: 0.7;
  border-radius: 50px;
  width: 50px;
  height: 50px;
  transition: all 0.2s;
  margin: 20px;
}

.wbn-slider-back-btn:hover,
.wbn-slider-next-btn:hover {
  transform: scale(1.1);
  opacity: 0.8;
}

And this is the HTML code:这是 HTML 代码:

<div class="slider-team">
                <div class="wbn-slide">
                    <img src=<?php echo get_template_directory_uri(). '/img/slide1.jpg'?> alt="">
                    <div class="wbn-overlay-text">
                        <span class="wbn-header">BEATRICE <br>BORELLINI</span>
                        <span class="wbn-text">ART DIRECTOR</span>
                    </div>
                </div>
                <div class="wbn-slide">
                    <img src=<?php echo get_template_directory_uri(). '/img/slide2.jpg'?> alt="">
                    <div class="wbn-overlay-text">
                        <span class="wbn-header">NICCOLO' <br> BIANCHI</span>
                        <span class="wbn-text">CEO & WEB DESIGNER</span>
                    </div>
                </div>
                <div class="wbn-slide active"><img src=<?php echo get_template_directory_uri(). '/img/slide3.jpg'?> alt="">
                  <div class="wbn-overlay-text">
                        <span class="wbn-header">PAOLA <br> MAZZONI</span>
                        <span class="wbn-text">CEO</span>
                    </div>
                </div>

                  <div class="wbn-buttons">
        <div class="wbn-slider-back-btn">
          <span><img src=<?php echo get_template_directory_uri(). '/img/Freccia.png'?> alt=""> </span>
        </div>

        <div class="wbn-slider-next-btn">
          <span><img src=<?php echo get_template_directory_uri(). '/img/Freccia.png'?> alt=""> </span>
        </div>
            </div>

在代码执行的每一步之后使用 console.log/alert,你会发现它卡住的地方,在小提琴它不起作用,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在到达最后一张幻灯片后如何使滑块回到第一张幻灯片,反之亦然 - How can I make the slider go back to the first slide after reaching the last slide and vice versa 我漂亮的 slider 旋转木马有一个额外的白色幻灯片。 有什么办法可以摆脱它? - My slick slider carousel has an extra white slide. Is there any way to get rid of it? 如何在我的第一张幻灯片上添加一个按钮来加载我的下一张幻灯片? - How can I add a button to my first slide that will load my next slide? 水平滑块错误 - 多个水平滑块与本机 JavaScript 具有相同的类,所有滑块一起工作我如何解决它 - horizontal slider bug - more than one horizontal slider with same classes with native JavaScript , All slider working together how I can solve it 幻灯片,使用 HTML、CSS 和 JS 不起作用? 卡在第一张幻灯片上不动 - Slideshow, using HTML,CSS and JS is not working? Stuck on the first slide and not moving CSS:我们如何在 slider 中滑动 animation? - CSS: how we can do slide animation in slider? 在我使用 html、css 和 js 添加自定义光标后,该光标在链接或按钮上不起作用。我应该如何解决? - After I added made a custom cursor using html,css and js, that cursor is not working on links or button .How should I solve it? 我更改.htaccess后,我的Css和Js无法正常工作 - After I Change my .htaccess my Css An Js not working 全屏菜单仅在滑块的第一张幻灯片上起作用,而在其余部分上不起作用-CSS,HTML - Full screen menu working just on first slide of slider not on the rest - CSS, HTML 如何使我的js滑块向相反方向移动? - How can I make my js slider move the opposite direction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM