jQuery Colorbox, iframe content not scrolling in iPad

放肆的年华 提交于 2019-12-04 11:29:09
Ian Stanway

UPDATE - AUTHOR FIXED ISSUE

Apparently Jack has fixed this issue as of today (4th Feb 2013). It's worth taking the latest release from his Github page.


Previous Solution

OK, I couldn't get jScrollPane working properly. That's not to say you won't, but I was also using custom resizing to resize the iframe and I don't think it was playing well with jScrollPane's dimensions calculations.

Solution

I did, however, manage to get it working thanks to a solution to the more general iOS iframe scroll issue, thanks to Sharon here on stackoverflow. I have made a couple of tweaks to her code to play nicer with colorbox. Please note that this only works where you control the iframe content

Simply put the following code into your iframe:

setTimeout(function () {
    var startY = 0;
    var startX = 0;
    var b = document.body;
    b.addEventListener('touchstart', function (event) {
        startY = event.targetTouches[0].screenY;
        startX = event.targetTouches[0].screenX;
    });
    b.addEventListener('touchmove', function (event) {
        event.preventDefault();
        var posy = event.targetTouches[0].screenY;
        var h = parent.document.getElementById("cboxLoadedContent");
        var sty = h.scrollTop;

        var posx = event.targetTouches[0].screenX;
        var stx = h.scrollLeft;
        h.scrollTop = sty - (posy - startY);
        h.scrollLeft = stx - (posx - startX);
        startY = posy;
        startX = posx;
    });
}, 1000);

The scrolling is not jittery, although you don't have the gradual slowdown of native scrolling, it just stops when you lift your finger. Plus, there's no scrollbar. Other than that it's a perfect solution.

The page with Sharon's solution offers an alternative to try for scenarios where you don't control the iframe.

I just have done this, in my general css I have add #cboxLoadedContent{-webkit-overflow-scrolling: touch;}

Then, in my views opened by colorbox, I have added to the css the following style body{-webkit-transform:translate3d(0, 0, 0);}

This works on iPad and iPhone.

Are you in control of your own iframe content, or is it content from an external website i.e. on a different domain? If the latter, I cannot help.

If you're in control of the iframe content you can use the jScrollPane plugin to solve your issue.

The body of your iframe should have CSS overflow:auto, and you need to wrap a div around your iframe content. On ready inside the iframe get the height of the iframe window, set the css height of the wrapper div to be the height of the window (or just a bit less), and then apply jScrollPane to your wrapper div.

$(document).ready(function() {
    var $container = $("#myDiv");
    var $win = $(window);

    if ($container.height() >= $win.height()){
        $container.css({
            'width': $win.width(),
            'height': $win.height()
        });

        $container.jScrollPane();
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!