0

我有一种方法可以每隔几秒更改一次 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;
    }
}
4

1 回答 1

0

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

如果它返回一个使用 setInterval 或 setTimeout 创建的纯 JavaScript 计时器,您可以使用 clearInterval(bgChangeTimer); 取消它;或 clearTimeout(bgChangeTimer); 分别。

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

否则,您可以尝试 Cycle 插件。它非常容易和灵活地做到这一点:http: //jquery.malsup.com/cycle/

于 2010-09-28T11:39:22.227 回答