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: OffHere 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) ;
});
});
精彩评论