1

Hello on my project I have a background image

background: url('img/1.JPG') fixed;

I am trying to change this background image using jQuery when a div (".boxy") will be hoverd. Here is what i managed to do in jQuery

$(document).ready(function() {
$(".boxy").hover(
    function()
    {
        $("body").css(??????); //what should i write here in order to change background image in img/2.JPG
    },
    function()
    {
        $("body").css(??????); // what should i write here in order to chage background image in img/1.JPG
    }
);
});
1
  • Why dont you add class on hover and remove on mouse out? $("body").addClass("hoverd") and same-wise removeClass("hobved") Commented Apr 25, 2014 at 13:23

4 Answers 4

2
$(document).ready(function () {
    $(".boxy").hover(function () {
        $("body").css('backgroundImage', 'url(img/2.JPG)');
    }, function () {
        $("body").css('backgroundImage', 'url(img/1.JPG)');
    });
});

jsFiddle example

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

3 Comments

backgroundImage this looks like a property used in javascript rather than a property used in CSS?
@KingKing - per the docs, "Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css( "background-color" ) and .css( "backgroundColor" )."
the upvote sign (up triangle) is orange now. I will toggle it for you to see?
1
$('body').css('background-image', 'url(' + imageUrl + ')'); 

something like this !

2 Comments

it dosent work how it should be, i have in css "background: url('img/1.JPG') fixed;" but not "background-image: url('img/1.JPG') fixed;" and it should be fixed like in my css
Just remove -image. $('body').css('background', 'url(' + imageUrl + ') fixed');
1
$('body').css("background", "url('/image.jpg') fixed");

Comments

0

Try

$(document).ready(function() {
$(".boxy").hover(
function()
{
    $("body").css("background-image", "url(/img.png)");
},
function()
{
    $("body").css("background-image", "url(/img2.png)");
}
);
});

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.