169

I'm currently developing a web app using html5 and jQuery for iPad Safari. I'm running into a problem wherein large scroll areas cause the elements that are offscreen to appear after a delay when I scroll down to them.

What I mean by that is, if I have a row of images (or even a div with a gradient) that is offscreen, when I scroll down (or up) to it, the expected behavior is for the element to appear on screen as I am scrolling to it.

However, what I'm seeing is that the element does not appear until I lift my finger off the screen and the scroller finishes all its animations.

This is causing a super noticeable problem for me, making the whole thing look choppy, although it is not. I'm guessing the iPad Safari is trying to do something to save memory. Is there any way in which I can prevent this choppy-ness from happening. Additionally, I would also appreciate if anyone can shed light on what the iPad Safari is actually trying to do.

Vertexwahn
  • 6,759
  • 6
  • 50
  • 78
codeBearer
  • 4,704
  • 4
  • 23
  • 28
  • This problem/solution helped me fix an issue with jPanelMenu 1.3 CSS Transforms version, which turned everything on my site invisibie until I added the above snippet. – 75th Trombone Mar 04 '13 at 20:20
  • 2
    Using *:not(html) will apply the translate3d to all other aspects of your site and I do not recommend. It will cause images in tabs to disappear as you scroll down, etc, bugs that you might be use to seeing on just your 3d images will now be present in other aspects of your site. – Jonathan Tonge Jul 09 '14 at 14:40
  • 1
    I had a few `` elements which were exhibiting similar delayed drawing/rendering. Unfortunately, `*:not(html) { ... }` led to all sorts of weird behaviors, as @JonathanTonge pointed out might occur. However, selecting only the `` elements and using `translate3d(0, 0, 0,);` seems to have solved my scrolling issues. – Zephyr Mays Aug 19 '14 at 02:39
  • Except for very specific use cases, this is garbage. Really messes up layouts that depend on absolute position elements. – Stoutie Sep 04 '14 at 18:30
  • 2
    Please post answers as answers, not “EDIT”s in your question.  I know you like your answer best, and that's fine, but StackOverflow has a Q&A format that works best when the Q's are distinct from the A's. – Slipp D. Thompson Oct 29 '14 at 03:42

15 Answers15

188

You need to trick the browser to use hardware acceleration more effectively. You can do this with an empty 3d transform:

-webkit-transform: translate3d(0,0,0)

Particularly, you'll need this on child elements that have a position:relative; declaration (or, just go all out and do it to all child elements).

Not a guaranteed fix, but fairly successful most of the time.

Hat tip: https://web.archive.org/web/20131005175118/http://cantina.co/2012/03/06/ios-5-native-scrolling-grins-and-gothcas/

cvrebert
  • 8,264
  • 2
  • 33
  • 47
Colin Williams
  • 1,926
  • 2
  • 12
  • 5
  • 3
    I tried that as well. Sadly, adding a translate3d is chopping off elements that were being displayed properly before. I also needed hardware acceleration for a couple of objects that were offscreen and had to be animated to "fly in" on screen. I was using jQuery's animate(), which was super slow. I switched over to using hardware acceleration. Although that sped up the animation, it produced erratic results, in the sense that some of the child elements of the parent (animating) div were chopped off. This was not happening when I was using animate(). – codeBearer Apr 25 '12 at 18:26
  • 1
    @Colin Williams you just made my day! :D – Luke May 17 '12 at 15:25
  • 10
    Wow, that is horrible but effective. Thank you. – Tim Down Jun 21 '12 at 11:54
  • Link is broken. New link: http://cantina.co/thought_leadership/ios-5-native-scrolling-grins-and-gothcas/# Cached: http://web.archive.org/web/20131005175118/http://cantina.co/2012/03/06/ios-5-native-scrolling-grins-and-gothcas/ – thinsoldier Jan 23 '14 at 14:59
  • 1
    Gold freakin star man! I added it to all my li elements in a scrollable div. Poof. Worked. – Bryan Johnson May 01 '14 at 21:04
  • Fantastic! Saved my bacon. To people who are having issues on applying this globally, try to narrow it down and apply it to the container div that has the items (that are not being rendered). So instead of *:not(html), #myDivContainer:not(html){...} and then apply myDivContainer as the Id to the div. Ugly, but effective in narrowing down global issues. BTW I have had to do this for latest Safari on the lastest OSX (not mobile/ipad only). – sumitkm Aug 11 '14 at 14:55
  • Thanks! Worked for me. – Jake Dec 17 '15 at 20:14
  • Thank you, you just helped me solving a very similar and annoying problem! – Adam Benedek Nov 03 '16 at 13:40
  • If we apply this for the page, the fixed elements are collapsed. – Jeeva J Mar 10 '17 at 10:18
  • I have applied it only to the elements affected and it worked! In my case it was the images inside a container overflow:hidden and scrollable horizontally: `.elem-affected{-webkit-transform: translate3d(0, 0, 0);}` I didn't need the :not(html) bit, not sure if it makes sense when applying to a specific eg. html class anyway. – Maciek Rek Nov 10 '18 at 11:32
  • Thank You! Just saved me from a few hours of frustration. – Inc33 Mar 31 '21 at 19:43
72

This is the complete answer to my question. I had originally marked @Colin Williams' answer as the correct answer, as it helped me get to the complete solution. A community member, @Slipp D. Thompson edited my question, after about 2.5 years of me having asked it, and told me I was abusing SO's Q & A format. He also told me to separately post this as the answer. So here's the complete answer that solved my problem:

@Colin Williams, thank you! Your answer and the article you linked out to gave me a lead to try something with CSS.

So, I was using translate3d before. It produced unwanted results. Basically, it would chop off and NOT RENDER elements that were offscreen, until I interacted with them. So, basically, in landscape orientation, half of my site that was offscreen was not being shown. This is a iPad web app, owing to which I was in a fix.

Applying translate3d to relatively positioned elements solved the problem for those elements, but other elements stopped rendering, once offscreen. The elements that I couldn't interact with (artwork) would never render again, unless I reloaded the page.

The complete solution:

*:not(html) {
    -webkit-transform: translate3d(0, 0, 0);
}

Now, although this might not be the most "efficient" solution, it was the only one that works. Mobile Safari does not render the elements that are offscreen, or sometimes renders erratically, when using -webkit-overflow-scrolling: touch. Unless a translate3d is applied to all other elements that might go offscreen owing to that scroll, those elements will be chopped off after scrolling.

So, thanks again, and hope this helps some other lost soul. This surely helped me big time!

codeBearer
  • 4,704
  • 4
  • 23
  • 28
  • 2
    Wow. Thanks for this. It saved me a huge amount of headache for my phonegap/cordova app. In my case I had to change `*:not(html)` to `body *` – anon Jul 09 '15 at 14:33
  • 9
    just applying `-webkit-transform: translate3d(0, 0, 0);` to the problem element worked for me. In my case I was using [ScrollMagic](https://github.com/janpaepke/ScrollMagic) – fidev Aug 28 '15 at 16:04
  • None of this worked for me either. As soon as I scroll down to a certain point, the no-longer-visible stuff above the window gets munged, as well as some elements in the viewport. I notice a very slight but unmistakable "jolt" while it's scrolling after which the elements get hosed. (Ie: smooth--jittery--smooth behavior, like it's hiccuping during scrolling.) This is in on iPad. – cbmtrx Nov 10 '15 at 18:29
  • BTW I just discovered that on an iPad Air 2, when a specific on-screen element (a navbar, in this case) goes off screen, the device "reloads" that block--only using the large version, not the original medium one that the page loaded with. What the... – cbmtrx Nov 10 '15 at 18:45
  • In case anyone is experiencing the same weirdness as me--slightly different from what is described on this page--I found that using `$(window).width()` instead of `window.innerWidth` in jQuery made all the difference for iOS. Who knows. – cbmtrx Nov 10 '15 at 19:17
13

Targeting all elements but html : *:not(html) caused problems on other elements in my case. It modified the stacking context, causing some z-index to break.

We should better try to target the right element and apply -webkit-transform: translate3d(0,0,0) to it only.

Edit : sometimes the translate3D(0,0,0) doesn't work, we can use the following method, targeting the right element :

@keyframes redraw{
    0% {opacity: 1;}
    100% {opacity: .99;}
}

// ios redraw fix
animation: redraw 1s linear infinite;
  • I was facing same issue. You can use `body *` selector instead of `*:not(html)`. It will solve you issue. – kunal Apr 05 '18 at 04:48
  • Kudos for the suggestion to target the right elements rather than polluting everything with the * – Maciek Rek Nov 10 '18 at 11:38
8

When the translate3d doesn't work, try to add perspective. It always works for me

transform: translate3d(0, 0, 0);
-webkit-transform: translate3d(0, 0, 0);
perspective: 1000;
-webkit-perspective: 1000;

http://blog.teamtreehouse.com/increase-your-sites-performance-with-hardware-accelerated-css

Fellipe Lima
  • 81
  • 1
  • 3
2

Adding -webkit-transform: translate3d(0,0,0) to an element statically doesn't work for me.

I apply this property dynamically. For example, when a page is scrolled, I set -webkit-transform: translate3d(0,0,0) on a element. Then after a short delay, I reset this property, that is, -webkit-transform: none This approach seems to work.

Thank you, @Colin Williams for pointing me in the right direction.

  • This hint helped me a lot, since these styles bing some unwanted side effects, I usually want to avoid. So I use touchstart/touchend events to add and remove them. Thanks! – Windwalker Apr 20 '16 at 09:24
1

I had the same issue with iscroll 4.2.5 on ios7. The whole scroll element just disappear. I've tried to add translate3d(0,0,0) as was suggested here, it have solved the problem, but it disabled the iscroll "snap" effect. The solution came with giving "position:relative; z-index:1000;display:block" css properties to the whole container that holds the scroll element and there is no need to give translate3d to child elements.

SilverlightFox
  • 28,804
  • 10
  • 63
  • 132
yudarik
  • 95
  • 1
  • 10
  • I used this technique to solve a similar problem on Android Chrome. Vertical scrolling seemed to perform better than when I tried the translate3d fix. In my case `` element is what I used for scrolling and a simplified version worked: `body { position: relative; z-index: 0; }` – BumbleB2na Aug 28 '17 at 02:39
1

I had the same issue using an older version of Fancybox. Upgrading to v3 will solve your problem OR you can just add:

html, body {
    -webkit-overflow-scrolling : touch !important;
    overflow: auto !important;
    height: 100% !important;
}
Adam Touhou
  • 492
  • 5
  • 11
1

At time translate3d may not work, in those cases perspective can be used

transform: translate3d(0, 0, 0);
-webkit-transform: translate3d(0, 0, 0);
perspective: 1000;
-webkit-perspective: 1000;
Nidhin Joseph
  • 8,658
  • 3
  • 18
  • 40
0

I'm pretty darn sure I just solved this with:

overflow-y: auto;

(Presumably just overflow: auto; would work too depending on your needs.)

David Notik
  • 1,456
  • 10
  • 20
  • It did nothing for me whereas the accepted answer did so probably a different scenario – tony Sep 06 '18 at 10:52
0

There are cases where a rotation is applied and/or a Z index is used.

Rotation: An existing declaration of -webkit-transform to rotate an element might not be enough to tackle the appearance problem as well (like -webkit-transform: rotate(-45deg)). In this case you can use -webkit-transform: translateZ(0px) rotateZ(-45deg) as a trick (mind the rotateZ).

Z index: Together with the rotation you might define a positive z-index property, like z-index: 42. The above steps described under "Rotation" were in my case enough to resolve the issue, even with the empty translateZ(0px). I suspect though that the Z index in this case may have caused the disappearing and reappearing in the first place. In any case the z-index: 42 property needs to be kept -- -webkit-transform: translateZ(42px) only is not enough.

0

This is a very common problem faced by developers and that is mainly due to Safari's property of not recreating elements defined as position : fixed.

So either change the position property or some hack needs to be applied as mentioned in other answers.

Link1

Link2

arqam
  • 2,856
  • 4
  • 24
  • 57
0

In my case (an iOS Phonegap app), applying translate3d to relative child elements did not resolve the issue. My scrollable element didn't have a set height as it was absolutely positioned and I was defining the top and bottom positions. What fixed it for me was adding a min-height (of 100px).

b4tch
  • 274
  • 2
  • 12
0

I faced this problem in a Framework7 & Cordova project. I tried all the solutions above. They did not solve my problem.

In my case, I was using 10+ css animations on the same page with infinite rotation (transform). I had to remove the animations. It is ok now with the lack of some visual features.

If the solutions above do not help you, you may start eliminating some animations.

yavuzkirez
  • 37
  • 5
0

the -webkit-transform: translate3d(0, 0, 0); trick didn't work for me. In my case I had set a parent to:

// parent
height: 100vh;

Changing that to:

height: auto;
min-height: 100vh;

Solved the issue in case someone else is facing the same situation.

funador
  • 434
  • 3
  • 8
0

In my case, CSS did not fix the issue. I noticed the problem while using jQuery re-render a button.

$("#myButton").html("text")

Try this

$("#myButton").html("<span>text</span>")
Josh Stovall
  • 330
  • 2
  • 6