I'm trying to make a custom jQuery toggle function. Right now, I have 2 separate functions, lightsOn
and lightsOff
. How can I bine these functions to make them toggle (so I can just link to one function)?
function lightsOn(){
$('#dim').fadeOut(500,function() {
$("#dim").remove();
});
};
function lightsOff(){
$('body').append('<div id="dim"></div>');
$('#dim').fadeIn(250);
};
I'm trying to make a custom jQuery toggle function. Right now, I have 2 separate functions, lightsOn
and lightsOff
. How can I bine these functions to make them toggle (so I can just link to one function)?
function lightsOn(){
$('#dim').fadeOut(500,function() {
$("#dim").remove();
});
};
function lightsOff(){
$('body').append('<div id="dim"></div>');
$('#dim').fadeIn(250);
};
Share
Improve this question
asked Dec 21, 2011 at 3:56
hao_maikehao_maike
3,0496 gold badges29 silver badges31 bronze badges
2 Answers
Reset to default 6function toggleLights()
{
var $dim = $('#dim');
if ($dim.length)
{
$dim.fadeOut(500, function ()
{
$dim.remove();
});
}
else
{
$dim = $('<div/>', {id: 'dim'});
$('body').append($dim);
$dim.fadeIn(250);
}
}
Demo: http://jsfiddle/mattball/hxL8s/
function toggleLights(){
if ($("body").has("#dim")) {
$('#dim').fadeOut(500, function() {
$("#dim").remove();
});
} else {
$('body').append('<div id="dim"></div>');
$('#dim').fadeIn(250);
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744276035a4566335.html
评论列表(0条)