简体   繁体   English

JavaScript自动幻灯片显示,尝试获取工作按钮

[英]JavaScript automatic slideshow, trying to get working buttons

my very first post on here! 我的第一个帖子就在这里!

I'm working on a website, which has a full screen slideshow landing page. 我正在一个网站上,该网站具有全屏幻灯片放映页面。 It changes image every few seconds, and has small dots at the side of the page, to indicate which image is currently active. 它每隔几秒钟更改一次图像,并在页面的侧面带有小点,以指示当前处于活动状态的图像。 It's all working great so far, but I'd like to make the small dots clickable, and change to the respective image. 到目前为止,一切都很好,但是我想使小点可点击,然后更改为相应的图像。

As I am still very new at coding, I haven't been able to find a solution that works. 由于我对编码仍然很陌生,所以我一直无法找到有效的解决方案。 Every image slider I can find online is either manual ones, with clickable buttons, or automatic ones, with no buttons at all. 我在网上可以找到的每个图像滑块要么是带有可单击按钮的手动滑块,要么是完全没有按钮的自动滑块。

Here's my code: 这是我的代码:

HTML HTML

<div id="slideshow">
        <div id="slide1" class="mySlides fade">Image 1</div>
        <div id="slide2" class="mySlides fade">Image 2</div>
        <div id="slide3" class="mySlides fade">Image 3</div>
        <div id="dotContainer">
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
        </div>          
</div>

CSS CSS

.mySlides{
    display: none;  
}

#slideshow{
    width: 100%;
    height: 100vh;
    position: relative;
    background-color: #717171;
}

#dotContainer{
    position: absolute;
    top: 50%;
    left: 98%;
    transform: translate(-50%, -50%);
}

.dot{
    width: 12px;
    height: 12px;
    margin: 6px 15px 6px 0;
    background-color: rgba(255, 255, 255, 0.7);
    border-radius: 50%;
    display: block;
    transition: 0.6s;
}

.activeDot{
    background-color: rgba(34,34,34, 0.7);
}

.fade{
    animation-name: fade;
    animation-duration: 1.5s;
}

@keyframes fade{
    from{
        opacity: 0.4;
    }
    to{
        opacity: 1;
    }
}

JavaScript JavaScript的

var slideIndex = 0;
    showSlides();

    function showSlides(){
        var slides = document.getElementsByClassName("mySlides");
        var dots = document.getElementsByClassName("dot");

        for (var i = 0; i < slides.length; i++){
            slides[i].style.display = "none";
        }
        slideIndex++;
        if (slideIndex > slides.length){
            slideIndex = 1;
        }
        for (var i = 0; i < dots.length; i++){
        dots[i].className = dots[i].className.replace(" activeDot", "");
        }
        slides[slideIndex-1].style.display = "block";
        dots[slideIndex-1].className += " activeDot";
        setTimeout(showSlides, 3000);       
    }

You can see it in action here: JSFiddle Demo 您可以在这里查看其运行情况: JSFiddle演示

All help is very much appreciated, thanks 非常感谢所有帮助,谢谢

https://jsfiddle.net/0wLgdoru/37/ https://jsfiddle.net/0wLgdoru/37/

 var slideIndex = 0; showSlides(); function showSlides() { var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); for (var i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } slideIndex++; if (slideIndex > slides.length) { slideIndex = 1; } for (var i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" activeDot", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " activeDot"; } function dotClickHandler(e) { clearInterval(intervalId); var activeDotIndex = this.getAttribute("slideDot"); slideIndex = activeDotIndex - 1; showSlides(); intervalId = setInterval(showSlides, 3000); } var dotNode = document.getElementsByClassName("dot"); for (var i=0;i<dotNode.length;i++) { dotNode[i].addEventListener("click", dotClickHandler); } var intervalId = setInterval(showSlides, 3000); 
 .mySlides{ display: none; } #slideshow{ width: 100%; height: 100vh; position: relative; background-color: #717171; } #dotContainer{ position: absolute; top: 50%; left: 98%; transform: translate(-50%, -50%); } .dot{ width: 12px; height: 12px; margin: 6px 15px 6px 0; background-color: rgba(255, 255, 255, 0.7); border-radius: 50%; display: block; transition: 0.6s; } .activeDot{ background-color: rgba(34,34,34, 0.7); } .fade{ animation-name: fade; animation-duration: 1.5s; } @keyframes fade{ from{ opacity: 0.4; } to{ opacity: 1; } } 
 <div id="slideshow"> <div id="slide1" class="mySlides fade">Image 1</div> <div id="slide2" class="mySlides fade">Image 2</div> <div id="slide3" class="mySlides fade">Image 3</div> <div id="dotContainer"> <span class="dot" slideDot="1"></span> <span class="dot" slideDot="2"></span> <span class="dot" slideDot="3"></span> </div> </div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM