简体   繁体   中英

Hide/Show body background

I'm working on a new website, i'm using a background image rotaror (after refresh). I made a switch where you can turn on/off the background image.

I found some scripts that can do it with div's but not with the body in css. So when the on/off switch is click i need to get "background:" removed.

Can somebody help me out with a jquery/javascript script?

body {
margin: 0;
padding: 0;
background: url("../img/landingpage/background/random.php") fixed no-repeat;
background-size: cover;
color: white;
font-family: verdana;
}

使用jQuery你可以做这样的事情:

$('body').css('background' : '');
$("#switch").on("click", function () {
    $("body").css("background", "none");
});

I've made it work now, with the toggle function

$("#myonoffswitch").toggle( 
function (){
    $("body").css("background", "none");
}
,
function (){
    $("body").css("background", "url('http://p1.pichost.me/640/27/1502004.jpg')"); 
}
);

JSFIDDLE: http://jsfiddle.net/A97G3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM