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

时间:2018-03-25 14:50:40

标签: javascript jquery html css fadein

在这里,我已经完成了幻灯片代码。当我点击左右按钮。下一个按钮时,我点击。

我希望当我点击下一个按钮时,箭头幻灯片显示在右侧移动并产生一些效果,当我点击上一个按钮箭头时,应该向左移动一些效果。

我使用了动画fadeInLeft和动画fadeInRight。但我不知道我是如何添加这个课程的。

我希望当我点击左键时,我的班级(动画fadeInLeft)添加,当我点击上一个按钮时,我的班级(动画fadeInRight)应该添加。

单击左右键单击时,如何添加这些类。 在这里,我的代码请查看它。

   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**

帮助将受到重视。

1 个答案:

答案 0 :(得分:1)

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

但是,通过仅更改slideIndex的初始i和循环起始值,使其全部为零,有一个有趣的结果,这可能是您希望实现的。

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**