Nativescript background-image fullscreen

风流意气都作罢 提交于 2019-11-30 09:46:38
Brad Martin

You need to use the NativeScript supported CSS properties to achieve this.

I've used the following CSS on a background-image attached to the <Page> view before and it works fine.

.coverImage {
    background-image: url('~/images/kiss.jpg');
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
}

If you want the Page to have a fullscreen image background, add your images to /App_Resources and do this in your component:

export class MyComponent implements OnInit {
    constructor(private page:Page) {}
    ngOnInit() {
        this.page.actionBarHidden = true;
        this.page.backgroundImage = "res://bg-image";
    }
}

Update: You can add CSS to enforce fullscreen.

.page {
    /* background-image: url("res://bg-image") */
    background-size: cover;
    background-repeat: no-repeat;
    /* background-attachment: fixed; */ /* not supported in {N} yet */
    background-position: center top; /* instead set ypos to top to avoid scroll-up */
}

Note: Assign this CSS class to your Page.

if you're using nativeScipt with Angular, you can use:

/*In your .css: */

.my-class {
    background-image: url("res://image-name.png") no-repeat;
}
<!-- in your .html: -->

<ScrollView class="my-class">

This does not work with animated gif. My style:

.page{
    background-image: url("~/assets/images/animated.gif") black;
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
}

The gif is shown, centered and enlarged, so great, but static: the animation does not moves.

This worked for me:

constructor(private page: Page) { }

ngOnInit() {
  this.page.actionBarHidden=true;`
  this.page.backgroundImage = 'res://gold_bg';
  this.page.style.backgroundSize='cover';
  this.page.style.backgroundRepeat='no-repeat';
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!