1

I have a background image being stretched to fit the entire screen. I am trying write a script that will change the background to another image. My current script does not stretch the image to fit the entire screen and instead centers the image. This problem occurs in Firefox, IE, and Opera. It seems that Chrome and Safari are unaffected. I followed this tutorial to create the full page background image.

CSS:

html {
    background: url(../img/01.jpg) no-repeat center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

JavaScript:

$('#previous').click(function() {
    $('html').css('background', 'url(img/01.jpg) no-repeat center fixed');
});

$('#next').click(function() {
    $('html').css('background', 'url(img/02.jpg) no-repeat center fixed');
});
2
  • Not sure if this would help, but have you tried setting the height and width to 100% for the html tag? Commented May 7, 2012 at 0:28
  • Just tried, but that didn't work :( Commented May 7, 2012 at 0:30

2 Answers 2

3

What if try to say background-size directly in function?

$('#next').click(function() {
    $('html').css('background', 'url(img/02.jpg) no-repeat center fixed');
    $('html').css('background-size', 'cover');
});

Because I suppose that when background is changes, previouse background-size property may be not working for new image...

Sign up to request clarification or add additional context in comments.

Comments

2

Try this , it worked for me.

CSS
    body {
    background-image: url('img/bg2.jpg');
    color: #000305;
    font-size: 87.5%; /* Base font size: 14px */
    font-family: 'Trebuchet MS', Trebuchet, 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
    line-height: 1.429;
    margin: 0;
    padding: 0;
    text-align: left;
    }
    
.body {
    clear: both; 
    margin: 0 auto; 
    width: 70%;
}

And in html I used the following script

 $(document).ready(function() {

 $("#tb").click(function() {
 var body = document.getElementsByTagName('body')[0];
body.style.backgroundImage = 'url(img/p2.jpg)';
    
  });

});

where tb is the button I used to change the background image.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.