如何更改 PhoneGap 应用程序中的背景图像?

发布于 2025-01-03 02:56:01 字数 1018 浏览 0 评论 0原文

我试图在方向改变时改变身体背景图像。起初这看起来很简单,所以我尝试了这段代码:

    function onBodyLoad() {     
        document.addEventListener("deviceready", onDeviceReady, false);
        document.addEventListener("orientationchange", onOrientationChange, true);
    }

    function onOrientationChange(e) {
        var orientation = (window.orientation == -90 || window.orientation == 90) ? "landscape" : "portrait";
        console.log("orientation: " + orientation);
        $("body").css("background-image", "url(images/" + orientation + ".png) no-repeat fixed center top");
    }

    function onDeviceReady() {
        console.log("at onDeviceReady");
        onOrientationChange();
    }

每次更改方向时,我都会在控制台中显示正确的方向 - 这意味着我的事件被正确触发。但背景不会改变,甚至最初设置(即使我在 onDeviceReady 中手动调用处理程序 - 并且我看到它被调用的控制台输出)。 $("body") 行中的某些内容不起作用。我尝试了几种 CSS 属性的组合,甚至尝试了 .css({"backgroundImage" : "..."}),但没有成功。

根据记录,图像文件夹中有一个 Landscape.png 和 Portrait.png,与 html 页面位于同一目录中。

我缺少什么?

感谢您抽出时间。

I'm trying to change the body background image whenever orientation changes. This seemed pretty easy at first, so I tried this code:

    function onBodyLoad() {     
        document.addEventListener("deviceready", onDeviceReady, false);
        document.addEventListener("orientationchange", onOrientationChange, true);
    }

    function onOrientationChange(e) {
        var orientation = (window.orientation == -90 || window.orientation == 90) ? "landscape" : "portrait";
        console.log("orientation: " + orientation);
        $("body").css("background-image", "url(images/" + orientation + ".png) no-repeat fixed center top");
    }

    function onDeviceReady() {
        console.log("at onDeviceReady");
        onOrientationChange();
    }

Every time I change the orientation, I get the correct orientation displayed in the console - meaning my event gets trigrerred properly. But the background does not change, or even initially set (even though I call the handler manually in onDeviceReady - and I see the console output that it was called). Something in the $("body") line just doesn't work. I tried several combinations of CSS properties and even .css({"backgroundImage" : "..."}), but to no avail.

For the record, there's a landscape.png and portrait.png in an images folder, in the same directory as the html page.

What am I missing?

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦亿 2025-01-10 02:56:01

也许没有 jQuery?

function onOrientationChange(e) {
    var orientation = (window.orientation == -90 || window.orientation == 90) ? "landscape" : "portrait";
    console.log("orientation: " + orientation);
    document.body.style.backgroundImage="url('images/" + orientation + ".png')";
    document.body.style.backgroundRepeat="no-repeat";
    document.body.style.backgroundAttachment="fixed";
    document.body.style.backgroundPosition="top center";
}

正如 @scessor 所说,你只能设置 background-image 属性的 url ,所以你应该使用 background 或其他 backgroundXXX 属性。

使用类

function onOrientationChange(e){
    var orientation = (window.orientation == -90 || window.orientation == 90) ? "landscape" : "portrait";
    console.log("orientation: " + orientation);
    $("body").removeClass("portrait").removeClass("landscape").addClass(orientation);
}

Maybe without jQuery?

function onOrientationChange(e) {
    var orientation = (window.orientation == -90 || window.orientation == 90) ? "landscape" : "portrait";
    console.log("orientation: " + orientation);
    document.body.style.backgroundImage="url('images/" + orientation + ".png')";
    document.body.style.backgroundRepeat="no-repeat";
    document.body.style.backgroundAttachment="fixed";
    document.body.style.backgroundPosition="top center";
}

As @scessor said you can only set the url of the background-image property, so you should use background or the other backgroundXXX properties instead.

Using classes

function onOrientationChange(e){
    var orientation = (window.orientation == -90 || window.orientation == 90) ? "landscape" : "portrait";
    console.log("orientation: " + orientation);
    $("body").removeClass("portrait").removeClass("landscape").addClass(orientation);
}
枕梦 2025-01-10 02:56:01

background-image 仅定义一个 url(请参阅定义 ):

$("body").css("background-image", "url(\"images/" + orientation + ".png\")");

或使用带有 url 和更多背景参数的 background (请参阅定义< /a>):

$("body").css("background", "url(\"images/" + orientation + ".png\") no-repeat fixed center top transparent");

===更新 ===

尝试以下操作:

$("body").css({ 
    backgroundImage: "url('images/" + orientation + ".png')"
    ,backgroundRepeat: "no-repeat"
    ,backgroundAttachment: "fixed"
    ,backgroundPosition: "top center"
});

 $("body").css({ 
    background: "url('images/" + orientation + ".png') no-repeat fixed top center transparent"
});

background-image only defines an url (see definition):

$("body").css("background-image", "url(\"images/" + orientation + ".png\")");

or use background with an url and more background parameters (see definition):

$("body").css("background", "url(\"images/" + orientation + ".png\") no-repeat fixed center top transparent");

=== UPDATE ===

Try following:

$("body").css({ 
    backgroundImage: "url('images/" + orientation + ".png')"
    ,backgroundRepeat: "no-repeat"
    ,backgroundAttachment: "fixed"
    ,backgroundPosition: "top center"
});

or

 $("body").css({ 
    background: "url('images/" + orientation + ".png') no-repeat fixed top center transparent"
});
请止步禁区 2025-01-10 02:56:01

找到两篇可能对你有用的文章。

mobile-safari-background-image -scaling-quirk

另一个与重复的CSS有关。

Background-image-doesn-t-show-up-in-Safari

乍一看,您可能没有这样做;但也许改变你的背景图像会导致同样的问题。

Found two articles that may be of use to you.

mobile-safari-background-image-scaling-quirk

and the other has to do with duplicate css.

Background-image-doesn-t-show-up-in-Safari

at first it may not seem like you are doing this; but maybe changing your bgimage is causing the same problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文