简体   繁体   中英

Image slider hover stop and animated transition

I was testing out coding an image slider as a project to learn HTML, CSS and Javascript and it works great. I'd just like to implement a few tweaks on it and was wondering if anyone had any idea on how to do this. Bear in mind, I'm relatively new to this so a few explanatory comments would be greatly appreciated. Here are the tweaks I'd like to implement: When the user hovers over the image, I'd like the slider to stop on that particular image so the user can look at it for as long as they wish. The slider resumes once the mouse is moved (a topic not explored on any questions here as far as I can find). Another thing I'd like to be able to do is create a more aesthetic fade transition between the images. There are tutorials out there for this but they don't give a lot of context for a beginner like me to implement it. Here's the jsfiddle, as requested, http://jsfiddle.net/7m9j0ttL/

<html>

<head>
    <style type="text/css">
        .container {
            max-width: 400px;
            background-color: black;
            margin: 0 auto;
            text-align: center;
            position: relative;
        }

        .container div {
            background-color: white;
            width: 100%;
            display: inline-block;
            display: none;
        }

        .container img {
            width: 100%;
            height: auto;
        }
    </style>
</head>

<body>
    <section class="demo">
        <div class="container">
            <div style="display: inline-block;">
                <img src="Chrysanthemum.jpg" width="1024" height="768" />
            </div>
            <div>
                <img src="Desert.jpg" width="1024" height="768" />
            </div>
            <div>
                <img src="Hydrangeas.jpg" width="1024" height="768" />
            </div>
        </div>
    </section>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            var currentIndex = 0,
                items = $('.container div'),
                itemAmt = items.length;

            function cycleItems() {
                var item = $('.container div').eq(currentIndex);
                items.hide();
                item.css('display', 'inline-block');
            }

            var autoSlide = setInterval(function() {
                currentIndex += 1;
                if (currentIndex > itemAmt - 1) {
                    currentIndex = 0;
                }
                cycleItems();
            }, 9000);
        });
    </script>
</body>

</html>

Updated your fiddle

 $('.demo').hover(function(){
     clearInterval(autoSlide);

    },function(){

     autoSlide = setInterval(function() {
    currentIndex += 1;
    if (currentIndex > itemAmt - 1) {
    currentIndex = 0;
   }
   cycleItems();
   }, 1000);
  });

Added a hover handler to the .demo element. Cleared interval on hover, this would help stop the slide show. And re-set interval on mouseout to start the slideshow per the set interval.

I don't know whether such kind of answer is acceptable for you, but someday, a few years ago, I created my own slider when I was studying jquery.

Looking at your code, I have questions: 1. Why don't you use rather standard functions like fadeIn() and fadeOut() for transitions? 2. Why don't you make a function that will be able to run simultaneously with any number of tags on the page?

A few years ago I had these questions in my head and I came here, to stackoverflow to learn how to do that from other people. And I learnt (not only here, though).

And I created a function that could be loaded anywhere in the code - I studied how to do that. Then I added fade and slide effects there and also any other things...

This function is not really good, but PROBABLY it will sched some light for you in slider creation process. Sorry for many words, check what I have here: https://jsfiddle.net/7m9j0ttL/3/

I hope my effort is useful for you. If you are going to go further with this and have questions - I would be glad to answer them.

Last comments: So my main aim was to create function that could be ran like this:

$('.container').okwbSlider({ActAsDefined: 'fadeItOut', SlidingTag: 'div', timeOut: 3000});

so, here you can see that almost ANY tag, containing ANY other tags (with images, text etc in it) can be slided.

in order to make everything slided after some time, I thought that I have to break function in 2 parts: one accepts parameters and the second is called using javascript's setInterval.

So, here's the first one:

(function($){    
    $.fn.okwbSlider = function(params) {        
    //outer variables
var tgDfnr = this;
var somevar = this;
var MouseStatevar = 0;
var globalTimervar = (params.globalTimervar != undefined) ? params.globalTimervar : 4000;
var ActAsDefined    =  (params.ActAsDefined != undefined) ? params.ActAsDefined : "fadeItOut";
var SlidingTag = (params.SlidingTag != undefined) ? params.SlidingTag : 'img';
var numberOfChildren = tgDfnr.children(SlidingTag).length;
// alert('tgDfnr='+tgDfnr+' globalTimervar='+globalTimervar+' ActAsDefined='+ActAsDefined+' numberOfChildren='+numberOfChildren);
//alert("<"+tgDfnr.prop("tagName")+" id="+tgDfnr.attr('id')+">");


        if (numberOfChildren > 1){
            setInterval(function(){
                okwbSlideIt(tgDfnr, ActAsDefined, numberOfChildren, MouseStatevar, SlidingTag);

            }, globalTimervar);
        }
        if(numberOfChildren == 1){
            tgDfnr.children(SlidingTag).fadeIn(500, function(){
                        $(this).addClass('active');
            });
        }

    }
})(jQuery); 

it contains everything that needed to run the function in jquery-like way (ie placing it after $('.yourANYClassNameOrId'))

and the second one (it's place higher in the text - re-accepts the entered parameters and works with them. It's written not in the really best way (I would write it much better now), but at least I think if you look at it, you can understand something useful.

So, let me know if you have questions and/or I can help you further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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