jQuery <img> rotate

Description

jQuery <img> rotate

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Image Rotator</title>
<style>
.container {//from   w  w w.ja  va2s.c o  m
    position: relative;
    width: 200px;
    height: 200px;
}

#slider img {
    position: absolute;
    display: none;
    border-radius: 3px;
}

#prev, #next {
    position: absolute;
    bottom: 10px;
    padding: 5px 10px;
    color: #000;
    background: #FFF;
    border-radius: 3px;
    text-decoration: none;
    opacity: 0.7;
}

#prev:hover, #next:hover {
    opacity: 1;
    cursor: pointer;
}

#prev {
    left: 10px;
}

#next {
    right: 10px;
}

#pagination {
    position: absolute;
    top: 10px;
    width: 100%;
    text-align: center;
}

#pagination a {
    padding: 2px 5px;
    color: #000;
    background: #FFF;
    border-radius: 3px;
    text-decoration: none;
    opacity: 0.7;
}

#pagination a:hover {
    opacity: 1;
    cursor: pointer;
}
</style>
</head>
<body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
        $(function() {
            var image = $("#slider img");
            var numSlides = image.length;
            var activeSlide = 0;
            var speed = 2000;
            var fade = 1000;
            var timer = setInterval(rotate, speed);
            image.eq(activeSlide).show();

            function rotate() {
                activeSlide++;

                if (activeSlide == numSlides) {
                    activeSlide = 0;
                }

                image.not(activeSlide).fadeOut(fade);
                image.eq(activeSlide).fadeIn(fade);
            }
        });
    </script>

    <div class="container">
        <div id="slider">
            <img src="image1.png"> <img src="image2.png"> <img
                src="image3.png"> <img src="image4.png"> <img
                src="image5.png"> <img src="image6.png"> <img
                src="image7.png">
        </div>
    </div>
</body>
</html>



PreviousNext

Related