jQuery <img> rotate and pause on hover, controls and pagination

Description

jQuery <img> rotate and pause on hover, controls and pagination

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Image Rotator | Features: Pause on hover, controls &
    pagination</title>
<style>
.container {//from  ww  w .  j  a va 2  s.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 pause;
            var paging = "";
            var timer = setInterval(rotate, speed);
            image.eq(activeSlide).show();

            for (var page = 0; page < numSlides; page++) {
                paging += "<a class=\"paging\" rel=\"" + (page + 1) + "\">"
                        + (page + 1) + "</a>\n";
            }
            $("#pagination").html(paging);

            $("#slider, #prev, #next, #pagination").hover(function() {
                clearInterval(timer);
                pause = true;
            }, function() {
                timer = setInterval(rotate, speed);
                pause = false;
            });

            $("#prev").click(function(event) {
                event.preventDefault();
                activeSlide--;
                rotate();
            });

            $("#next").click(function(event) {
                event.preventDefault();
                activeSlide++;
                rotate();
            });

            $("#pagination a").click(function(event) {
                event.preventDefault();
                activeSlide = $(this).attr("rel") - 1;
                rotate();
            });

            function rotate() {
                if (!pause == true) {
                    activeSlide++;
                }

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

                if (activeSlide < 0) {
                    activeSlide = numSlides - 1;
                }

                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>
        <a id="prev">prev</a> <a id="next">next</a>
        <div id="pagination"></div>
    </div>
</body>
</html>



PreviousNext

Related