如何使用 javascript 在我的按钮点击上添加此 class?

How to add this class on my button click using javascript?

到这里,我完成了幻灯片代码。当我点击左右 button.next 和上一个按钮时。

我希望当我单击下一个按钮时箭头幻灯片放映会向右侧移动并产生一些效果,而当我单击上一个按钮时箭头应该向左侧移动并产生一些效果。

我使用动画 fadeInLeft 和动画 fadeInRight。但我不知道如何添加这个 class ..

我想当我点击左键时我的 class 这个 class (动画淡入左)添加,当我点击上一个按钮时我的 class(动画淡入右)应该添加。

当我单击左右按钮时,我应该如何添加这些 class。 这是我的代码,请查看。

   var divMain = document.getElementById("slide");
    console.log(divMain)

    var align = divMain.getAttribute("type");
    console.log(align)

    if(align == "slideshow") {
        var timeline = document.getElementById('slide');

        timeline.insertAdjacentHTML("afterbegin",'<a class="left animated fadeInLeft color_arrow carousel-control" href="#myCarousel" onclick = "plusDivs(-1)" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span><span class="sr-only">Previous</span></a>');

        timeline.insertAdjacentHTML('afterbegin',' <a class="right color_arrow  animated fadeInRight carousel-control" href="#myCarousel"  onclick = "plusDivs(1)" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span><span class="sr-only">Next</span></a>');

        var slideIndex = 2;
        showDivs(slideIndex);

        function plusDivs(sliderItem) {
            showDivs(slideIndex += sliderItem);
        }

        function showDivs(sliderItem) {
            let i;
            let sliderData = document.getElementsByTagName("section");
            
            if (sliderItem > sliderData.length) {slideIndex = 1}    
            if (sliderItem < 1) {slideIndex = sliderData.length}

            for (i = 1; i < sliderData.length; i++) {
                sliderData[i].classList.add('hide')
                sliderData[i].classList.remove('active')  
            }
            sliderData[slideIndex-1].classList.remove('hide')
            sliderData[slideIndex-1].classList.add('active') 
        }
    }
    
  div[type="slideshow"] > section:not(.hide) {
        margin: auto;
        width: 900px;
        z-index: 100;
        border-left: 4px solid #00BCD4;
        min-height:250px;
        background-color: #b3e5fc2b;
        border-radius: 4px;
        margin-bottom: 55px;
        position: relative;
        top: 50px;
        box-shadow: rgb(136, 136, 136) 3px 3px 1px;
    }

  
    div[type="slideshow"] > section:not(.hide) > header {
        font-weight: 600;
        color: cadetblue;
        transform: translate(93px, 32px);
        font-size: 34px;
        font-family: arial;
        position: relative;
    }
    div[type="slideshow"] > section:not(.hide) > article {
        transform: translate(87px,39px);
        color: black;
        font-size: 22px;
        font-family: arial;
        position: relative;
        padding: 10px;
        word-wrap: break-word;
    }
     .active{
        -webkit-animation: fadein 2s
        -moz-animation: fadein 2s; 
        -ms-animation: fadein 2s;
        -o-animation: fadein 2s; 
        animation: fadein 2s;
        opacity: 1 !important;
    }
    .hide{opacity: 0; min-height: 0 !important; margin: 0 !important;}
    .hide header, .hide article{display: none;}
    @keyframes fadein {
        0% { opacity: 0 }
        50% { opacity: 0.5 }
        100% {opacity: 1}
    }
<div type='slideshow' id='slide'>
 <section class='sectionDiv'>
  <header>Title One</header>
  <article>Content</article>
 </section>
 <section class='sectionDiv'>
  <header>Title Two</header>
  <article>Content</article>
 </section>
 <section class='sectionDiv'>
  <header>Title Three</header>
  <article>Content</article>
 </section>
 <section class='sectionDiv'>
  <header>Title Four</header>
  <article>Content</article>
 </section>
</div>

These class i want add **animated fadeInLeft and animated fadeInRight**

我们会感谢您的帮助。

我不确定你的问题...因为那里没有 fadeInLeft class。

但是,只需更改 slideIndex 的初始值和 i 的循环起始值,使其全部变为 zero-based,就会有一个有趣的结果,这可能正是您希望的实现。

var slideIndex = 1;  // Intead of 2

并且:

for (i = 0; ...  // Instead of 1

看下面的区别::

var divMain = document.getElementById("slide");
    console.log(divMain)

    var align = divMain.getAttribute("type");
    console.log(align)

    if(align == "slideshow") {
        var timeline = document.getElementById('slide');

        timeline.insertAdjacentHTML("afterbegin",'<a class="left animated fadeInLeft color_arrow carousel-control" href="#myCarousel" onclick = "plusDivs(-1)" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span><span class="sr-only">Previous</span></a>');

        timeline.insertAdjacentHTML('afterbegin',' <a class="right color_arrow  animated fadeInRight carousel-control" href="#myCarousel"  onclick = "plusDivs(1)" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span><span class="sr-only">Next</span></a>');

        var slideIndex = 1;
        showDivs(slideIndex);

        function plusDivs(sliderItem) {
            showDivs(slideIndex += sliderItem);
        }

        function showDivs(sliderItem) {
            let i;
            let sliderData = document.getElementsByTagName("section");
            
            if (sliderItem > sliderData.length) {slideIndex = 1}    
            if (sliderItem < 1) {slideIndex = sliderData.length}

            for (i = 0; i < sliderData.length; i++) {
                sliderData[i].classList.add('hide')
                sliderData[i].classList.remove('active')  
            }
            sliderData[slideIndex-1].classList.remove('hide')
            sliderData[slideIndex-1].classList.add('active') 
        }
    }
div[type="slideshow"] > section:not(.hide) {
        margin: auto;
        width: 900px;
        z-index: 100;
        border-left: 4px solid #00BCD4;
        min-height:250px;
        background-color: #b3e5fc2b;
        border-radius: 4px;
        margin-bottom: 55px;
        position: relative;
        top: 50px;
        box-shadow: rgb(136, 136, 136) 3px 3px 1px;
    }

  
    div[type="slideshow"] > section:not(.hide) > header {
        font-weight: 600;
        color: cadetblue;
        transform: translate(93px, 32px);
        font-size: 34px;
        font-family: arial;
        position: relative;
    }
    div[type="slideshow"] > section:not(.hide) > article {
        transform: translate(87px,39px);
        color: black;
        font-size: 22px;
        font-family: arial;
        position: relative;
        padding: 10px;
        word-wrap: break-word;
    }
     .active{
        -webkit-animation: fadein 2s
        -moz-animation: fadein 2s; 
        -ms-animation: fadein 2s;
        -o-animation: fadein 2s; 
        animation: fadein 2s;
        opacity: 1 !important;
    }
    .hide{opacity: 0; min-height: 0 !important; margin: 0 !important;}
    .hide header, .hide article{display: none;}
    @keyframes fadein {
        0% { opacity: 0 }
        50% { opacity: 0.5 }
        100% {opacity: 1}
    }
<div type='slideshow' id='slide'>
 <section class='sectionDiv'>
  <header>Title One</header>
  <article>Content</article>
 </section>
 <section class='sectionDiv'>
  <header>Title Two</header>
  <article>Content</article>
 </section>
 <section class='sectionDiv'>
  <header>Title Three</header>
  <article>Content</article>
 </section>
 <section class='sectionDiv'>
  <header>Title Four</header>
  <article>Content</article>
 </section>
</div>

These class i want add **animated fadeInLeft and animated fadeInRight**