Search code examples
jqueryiphonejquery-animateiphone-5

jQuery-animation on touchend after scrolling on iPhone5


I have a pretty strange problem with the jQuery animate() function on an iphone 5. The scenario:

I attach an event handler to the touchend event on an element, starting an animation. Usually this works pretty fine. But as soon as I scrolled the page once swiping on this element, the animate-function doesn't animate anymore (on all future gestures). The event is fired and the handler is called, but animate doesn't do anything. I tried with the jQuery mobile events and now with oldscool element.attachaddEventListener both leading to the same result. The funny thing is:

  1. If I attach it to touchstart, it works before and after scrolling (but I need the touchend event!)
  2. If I do something that doesn't animate, toggle for example, it works too.

A small demonstration page:

<!DOCTYPE html>
<html>
<head>
    <title>Testpage</title>
    <meta content="width=device-width, initial-scale=1.0, user-scalable=yes" name="viewport">
    <script src="/3rdParty/scripts/jquery/jquery-1.8.3.js" type="text/javascript"></script>
    <style>
        body { padding: 10px;}
        #mover { position: relative; width: 100px; background-color: red; color: #fff; padding: 5px; }
        #hider { width: 100%; overflow: hidden; }
        #touchme {  border:1px solid #808080; margin-bottom:10px;line-height: 50px; text-align:center;}
    </style>
    <script>
        $(document).ready(function(){
            var elem = document.getElementById('touchme');
            elem.addEventListener('touchend', positionHandlerEnd, false );
            elem.addEventListener('mouseup', positionHandlerEnd, false );
        });
    </script>
    <script>
        function positionHandlerEnd(e) {
            $('#mover').animate({left: '+=40px'});
            e.preventDefault();
            return;
        }

    </script>
</head>
<body>
<div id="touchme">Touch me</div>
<div id="hider"><div id="mover">This moves</div></div>
<div> dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate</div>
<div> dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate</div>
<div> dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate</div>
</body>
</html>

Any ideas what exactly is happening here? Or any workarrounds?


Solution

  • As I was struggling with the same problem, I stumbled upon a Safari 'setTimeout bug'. It appears that Safari (under IOS6) doesn't execute animations or timers when scrolling. I have no idea why this influences animations after scrolling, but this guy wrote a little script that solved the problem for me. Be sure to check it out:

    https://gist.github.com/3755461

    I'm not 100% sure if it will cause errors in other browsers (didn't encounter any, yet), but it saved my day and I'm glad to share it with you.