开发者

jquery setinterval not working

开发者 https://www.devze.com 2023-03-23 01:44 出处:网络
I have this: $(\"#nav-reflection li\").append(\"<span></span>\"); // Animate buttons, move reflection and fade

I have this:

    $("#nav-reflection li").append("<span></span>");    

    // Animate buttons, move reflection and fade

    $("#nav-reflection a").hover(function() {
        $(this).stop().animate({ marginTop: "-10px" }, 200);
        $(this).parent().find("span").stop().animate({ marginTop: "18px", opacity: 0.25 }, 200);
    },function(){
        $(this).stop().animate({ marginTop: "0px" }, 300);
        $(this).parent().find("span").stop().animate({ marginTop: "1px", opacity: 1 }, 300);
    });

/* =Shadow Nav
-------------------------------------------------------------------------- */

    // Append shadow image to each LI

    $("#nav-shadow li").append('<img class="shadow" src="http://admin.focuswebmedia.com/wp-content/themes/fwm/images/icons-shadow.jpg" width="79" height="19" alt="" />');
    //$("#slider_right").append('<div id="porfolio_contact_text">Our Porfolio&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Contact Us</div>');
    // Animate buttons, shrink and fade shadow

    $("#nav-shadow li").hover(function() {
        var e = this;
        $(e).find("a").stop().animate({ marginTop: "-14px" }, 250, function() {
            $(e).find("a").animate({ marginTop: "-10px" }, 250);
        });
        $(e).find("img.shadow").stop().animate({ width: "80%", height: "20px", margin开发者_如何学GoLeft: "8px", opacity: 0.25 }, 250);
    },function(){
        var e = this;
        $(e).find("a").stop().animate({ marginTop: "4px" }, 250, function() {
            $(e).find("a").animate({ marginTop: "0px" }, 250);
        });
        $(e).find("img.shadow").stop().animate({ width: "100%", height: "27px", marginLeft: "0", opacity: 1 }, 250);
    });

works great on hover. But how do I get this to play automatically every 5 or so seconds? I tried this:

window.setInterval(play_ani, 5000);

function play_ani() {
 $j("#nav-reflection a").mouseover();
}

But nothing happens... Any ideas?


You could just trigger the hover event:

window.setInterval(play_ani, 5000);

function play_ani() {
 $("#nav-reflection a").trigger('mouseover');
}

I think your code does nothing because you are not triggering the effect.

UPDATE - you must trigger mouseover and use $ not $j - example fiddle: http://jsfiddle.net/nicolapeluchetti/qdwZN/3/


You should trigger the mmouseenter event.

However, it would be better to move that code into a separate function and call it from both places:

function play(elem) {
    elem.stop().animate({ marginTop: "-10px" }, 200);
    elem.parent().find("span").stop().animate({ marginTop: "18px", opacity: 0.25 }, 200);

$("#nav-reflection a").hover(function() {
    play($(this));
}, function(){
    $(this).stop().animate({ marginTop: "0px" }, 300);
    $(this).parent().find("span").stop().animate({ marginTop: "1px", opacity: 1 }, 300);
});

setInterval(function() { play($("#nav-reflection a"); }, 5000);
0

精彩评论

暂无评论...
验证码 换一张
取 消