Cara buat slideshow sederhana dengan javascript tanpa jquery


Assalamu'alaikum wr wb,
kali ini saya akan share bagaimana Cara buat slideshow sederhana dengan javascript tanpa jquery.
Berikut ini syntaxnya.
---------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#slides{
position: relative;
height: 150px;
padding: 0px;
margin: 0px;
list-style-type: none;
}

.slide{
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
opacity: 0;
z-index: 1;

-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;
-o-transition: opacity 1s;
transition: opacity 1s;
}

.showing{
opacity: 1;
z-index: 2;
}

.slide{
font-size: 40px;
padding: 40px;
box-sizing: border-box;
background: #333;
color: #fff;
}

.slide:nth-of-type(1){
background: red;
}
.slide:nth-of-type(2){
background: orange;
}
.slide:nth-of-type(3){
background: green;
}
.slide:nth-of-type(4){
background: blue;
}
.slide:nth-of-type(5){
background: purple;
}
</style>
</head>
<body>
<ul id="slides">
<li class="slide showing">Slide 1</li>
<li class="slide">Slide 2</li>
<li class="slide">Slide 3</li>
<li class="slide">Slide 4</li>
<li class="slide">Slide 5</li>
</ul>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<script type="text/javascript">
var slides = document.querySelectorAll('#slides .slide');
var currentSlide = 0;
var slideInterval = setInterval(nextSlide,2000);

function nextSlide(){
slides[currentSlide].className = 'slide';
currentSlide = (currentSlide+1)%slides.length;
slides[currentSlide].className = 'slide showing';
}
</script>
</body>
</html>
---------------------------------------------------------------------------
Semoga bermanfaat.
http://pangeran-it.blogspot.co.id/
Cara buat slideshow sederhana dengan javascript tanpa jquery Cara buat slideshow sederhana dengan javascript tanpa jquery Reviewed by Syawaluddin Amin, S.Kom on Maret 27, 2017 Rating: 5

Tidak ada komentar:

Diberdayakan oleh Blogger.