取消mouseleave上的jquery方法

时间:2010-09-28 11:25:04

标签: jquery javascript-events mouseleave

嗨我有一个方法可以每隔几秒更改一次div的背景,就像旋转木马一样,当用户翻过一段文字时。在#h2Create mouseleave事件中,我想取消changingCarosel方法(停止播放旋转木马)。我怎么能这样做?

$(document).ready(function () {


    $("#h2Create").mouseenter(function () {
        changingCarosel('create');
        $("#h2Create").css("color", "#99eee5");
        $("#ServiceDesc").text('WEB/MOBILE/APPS/BESPOKE DESIGN AND BUILD');

    });
    $("#h2Create").mouseleave(function () {

        $("#h2Create").css("color", "#fff");
    });
});

changingCaroselvar bgChangeTimer = {};
var bgSecondRotation = {};
var bgThirdRotation = {};
var bgFourthRotation = {};
function changingCarosel(param) {


    switch (param.toString())
    {
    case 'create':
    $("#ServicesBackgroundImage").css("background-image", "url(/images/spreadVenture.jpg)");
        bgChangeTimer = $.timer(2700, function () {

        $("#ServicesBackgroundImage").css("background-image", "url(/images/Spreadthorntons.jpg)");

        bgSecondRotation = $.timer(2700, function () {

            $("#ServicesBackgroundImage").css("background-image", "url(/images/spreadC2K.jpg)");
            bgSecondRotation = $.timer(2700, function () {
                changingCarosel("create");
            });
        });

    });
    break;
case 'buzz':
    $("#ServicesBackgroundImage").css("background-image", "url(/images/jr.jpg)");
    bgChangeTimer = $.timer(2700, function () {

        $("#ServicesBackgroundImage").css("background-image", "url(/images/vufold.jpg)");

        bgSecondRotation = $.timer(2700, function () {

            $("#ServicesBackgroundImage").css("background-image", "url(/images/prmothean.jpg)");
            bgThirdRotation = $.timer(2700, function () {
                $("#ServicesBackgroundImage").css("background-image", "url(/images/dayParade.jpg)");
                bgFourthRotation = $.timer(2700, function () {
                    changingCarosel("buzz");
                });
            });
        });

    });
    break;
default:
    $("#ServicesBackgroundImage").css("background-image", "url(/images/ifIruledTheWorld.jpg)");
    bgChangeTimer = $.timer(2700, function () {

        $("#ServicesBackgroundImage").css("background-image", "url(/images/Spreadthorntons.jpg)");

        bgSecondRotation = $.timer(2700, function () {

            $("#ServicesBackgroundImage").css("background-image", "url(/images/spreadC2K.jpg)");
            bgSecondRotation = $.timer(2700, function () {
                changingCarosel("spread");
            });
        });

    });
    break;
    }
}

1 个答案:

答案 0 :(得分:0)

我不认识$ .timer()函数。它是一些插件吗?我找不到任何文件。

如果它返回使用setInterval或setTimeout创建的普通javascript计时器,则可以使用clearInterval(bgChangeTimer)取消它;或clearTimeout(bgChangeTimer);分别

查看您正在使用的插件的文档。

否则,您可以试用Cycle插件。它非常容易和灵活地完成这项工作: http://jquery.malsup.com/cycle/