Cons of MouseOver for webpages

2019-09-14 04:15发布

问题:

So I am currently creating a "button" with multiple images within it like a slideshow and whenever I MouseOver it, the image changes to another image.

However whenever the slideshow image changes, the MouseOver effect gets removed to a MouseOut state since technically the mouse is no longer on the image.

I also tried having a fade effect for my button however most of my searches lead to using hover functions instead of MouseOver and MouseOut.

So I am wondering if Hover is better than MouseOver in terms of it's potential capabilities?

Is it possible to pause the slideshow event on hover, etc? And how can I go about doing it?

Here is my current code:

function.js

$(function () {

    $('#01 img:gt(0)').hide();
    setInterval(function () {
        $('#01 :first-child').fadeOut(1500)
           .next('img').fadeIn(1500)
           .end().appendTo('#01');
    },
      3000);
});

$(document).ready(function () {

    $("#image1").mouseover(function () {
        $(this).attr("src", "images/board_01_over.jpg");
    });

    $("#image1").mouseout(function () {
        $(this).attr("src", "images/board_01_01.jpg");
    });
});

main.css

    #board {
    float: left;
    width: 998px;
    overflow: hidden;
}


.fadein {

    float: left;
    position: relative;
    width: 240px;
    height: 140px;
    margin: 1px 1px 1px 1px;
}

    .fadein img {
        position: absolute;
        left: 0;
        top: 0;
        height: 140px;
        opacity: 0.6;
        overflow: hidden;
    }

        .fadein img:hover {
            opacity: 1;
        }

Main.html

     <div id="board">
         <div class="fadein" id="01">
             <img src="images/board_01_01" id="image1" />

             <img src="images/board_01_02.jpg" id="image2" />
         </div>

     </div>

回答1:

Since you are using jQuery you can utilize the hover() function.

http://api.jquery.com/hover

$("#image1").hover(function () {
    $(this).attr("src", "images/board_01_over.jpg");
},

function () {
    $(this).attr("src", "images/board_01_01.jpg");
});

For you slider it's easier to make a little object out of it so it's easier to control.

var Slideshow = {
    interval:null,

    start: function () {
        ...
        initialize
        ...
        // catch the interval ID so you can stop it later on
        this.interval = window.setInterval(this.next, 3000);
    },

    next: function () {
        /*
         * You cannot refer to the keyword this in this function
         * since it gets executed outside the object's context.
         */
        ...
        your logic
        ...
    },

    stop: function () {
        window.clearInterval(this.interval);
    }
};

Now you can easily call

Slideshow.start();
Slideshow.stop();

from anywhere to start and stop your slider.