开发者

Jquery toggle with 2 triggers

I have two different links to fire a toggle function. However, they act independently:

Link 1: On

Link 2: On

Link 2: Off

Link 1: On

However, I would like them to work as 开发者_StackOverflow社区if they were the same link:

Link 1: On

Link 2: Off

Link 2: On

Link 1: Off

Here is my current code... any help would be appreciated.

http://www.jsfiddle.net/Gavyz/


Add a global toggled variable.

var toggled = false;

$(document).ready(function(){
    $("button").click(function(){
        if(toggled == false)
            change = 250;
        else
            change = -250;

        toggled = !toggled;

        $('p').animate({
            top: '+='+change
        }, 700) ;
    }
    );
});


Modified Andrew M's code so it works...

var toggled = false;

$(document).ready(function(){
    $("button").click(function(){
        if(toggled == false)
            change = 250;
        else
            change = -250;

        toggled = !toggled;

        $('p').animate({
            top: '+=' + change
        }, 700) ;
    });    
});

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜