jQuery <img> rotate and pause on hover and controls

Description

jQuery <img> rotate and pause on hover and controls

View in separate window

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

            $("#slider, #prev, #next").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();
            });

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



PreviousNext

Related