Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  14] [ 7]  / answers: 1 / hits: 153120  / 12 Years ago, sun, june 3, 2012, 12:00:00

How do i change the background image in CSS at run time? I have the following background image in body and can the image be changed at run time?



body {
height: 100%;
background: #fff8db url(../images/backgrounds/header-top.jpg) no-repeat center top;
/*background-color: #fff8db;*/
/*background-size: 1650px 900px;*/
font-family: Arial, Verdana, Helvetica, sans-serif;
font-size:12px;
font-weight:normal;
color:#404040;
line-height:20px; }

More From » css

 Answers
5

If you have JQuery loaded already, you can just do this:



$('body').css('background-image', 'url(../images/backgrounds/header-top.jpg)');


EDIT:



First load JQuery in the head tag:



<script src=//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js type=text/javascript></script>


Then call the Javascript to change the background image when something happens on the page, like when it finishes loading:



<script type=text/javascript>
$(document).ready(function() {
$('body').css('background-image', 'url(../images/backgrounds/header-top.jpg)');
});
</script>

[#85180] Friday, June 1, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arturo

Total Points: 331
Total Questions: 99
Total Answers: 92

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
;