Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set fixed position Background image in jquery mobile for iPhone app using Phonegap

I am doing a iPhone app using Phonegap & also using jquery mobile. I want to set background image for data-role=page div. In this height of page is equal to screen & hence background is set to size of screen. But the page content length is much greater than screen & hence gray background is seen after image completes. My question is whether there is a way so that we can keep the background image fixed & scroll or move only content of page & not background image. Just to mention I have tried full size background jquery pluggin. Its working on Android but not on iOS.

Can anyone please help? Thanks in advance.

like image 737
Sayali Avatar asked Jan 19 '12 09:01

Sayali


People also ask

How do I add a fixed background image to my website?

Set a background image using the HTML body elementAdd the bgproperties="fixed" code into the body element, as shown below. Next, add the background="location" attribute to the body tag, where location is the relative or absolute URL of the image. In the above example, background="bg.

Which attribute is used to keep background fixed?

The background-attachment property is used to specify that the background image is fixed or scroll with the rest of the page in the browser window. This property has three values scroll, fixed, and local. Its default value is scroll, which causes the element to not scroll with its content.

What is the use of background-attachment fixed?

The background-attachment CSS property sets whether a background image's position is fixed within the viewport, or scrolls with its containing block.


2 Answers

Ok, so what I did instead was to create a fixed element within the body element of the page. So it would look like

<body>
   <div id="background"></div>
    ...
</body>

And for the CSS I stated the following:

    #background {
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    background: url(images/bg-retina.jpg) 0 0 no-repeat fixed !important;
    background-size:contain;
}

And this did the trick for me. Hopefully it helps (someone out there :P)

like image 64
Adrian Avatar answered Sep 18 '22 13:09

Adrian


You looking for css background-attachment property.

div[data-role="page"]{
  background-attachment: fixed;
}

Update:
background-attachment:fixed is supported from iOS 5, if you using older version of iOS you may consider usage of iScroll.

like image 42
Juicy Scripter Avatar answered Sep 18 '22 13:09

Juicy Scripter