jQuery悬停在固定位置更改背景图像

时间:2014-04-25 13:21:46

标签: javascript jquery css

你好我的项目我有一个背景图片

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

我正在尝试使用jQuery更改此背景图片,而div (".boxy")将被悬停。 这是我在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
    }
);
});

4 个答案:

答案 0 :(得分:2)

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

<强> jsFiddle example

答案 1 :(得分:1)

$('body').css('background-image', 'url(' + imageUrl + ')'); 
像这样的东西!

答案 2 :(得分:1)

$('body').css("background", "url('/image.jpg') fixed");

答案 3 :(得分:0)

尝试

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