0

好的,我的幻灯片工作正常,但我不知道如何判断鼠标是否悬停在图像上。

Javascript:

<script type="text/javascript">     
    var hovering = false;
    $('#slideshow').hover(function () {   //not sure if #slideshow is the right thing to put here
        hovering = true;
    }, function () {
        hovering = false;
        slideShow();
    });

    $(document).ready(function(){
        slideShow();
    });
    function slideShow() {
        if(!hovering) {
            var showing = $('#slideshow .show');
            var next = showing.next().length ? showing.next() : showing.parent().children(':first');
            var timer;
            showing.fadeOut(500, function() { next.fadeIn(200).addClass('show'); }).removeClass('show');
            setTimeout(slideShow, 3000);
        }
    }
</script>

HTML:

<div id="slideshow">
    <img class="show" src="../images/food/chicken_quesa.jpg">
    <img src="../images/food/beet_salad.jpg">
    <img src="../images/food/pasta_display.jpg">
</div>
4

2 回答 2

1

试试这样:

$(document).ready(function() {

    var timer;

    $("#slideshow div").hide();

    $("#slideshow")
        // when mouse enters, clear the timer if it has been set
        .mouseenter(function() {
            if (timer) { clearInterval(timer) }
        })
        // when mouse goes out, start the slideshow
        .mouseleave(function() {
            timer = setInterval(function() {
                $("#slideshow > div:first")
                    .fadeOut(1000)
                    .next()
                    .fadeIn(1000)
                    .end()
                    .appendTo("#slideshow");
            }, 3000);
        })
        // trigger mouseleave for initial slideshow starting
        .mouseleave();

});​
于 2013-03-13T05:41:33.920 回答
0

用于mouseenter and mouseleave它:

$('#slideshow').mouseenter(function () {
    hovering = true;
}).mouseleave(function () {
    hovering = false;
    slideShow();
});

阅读文档鼠标进入鼠标离开

于 2013-03-13T05:34:22.310 回答