旋转时将网页滚动到移动设备上的顶部

时间:2014-05-04 01:17:53

标签: html css html5 css3 twitter-bootstrap

我遇到一个问题,当我将网页从纵向旋转到横向(例如在iOS设备上 - 例如iPhone5)时,横向视图从页面顶部开始大约50px。向后旋转显示纵向视图没有问题。

以下是显示问题的屏幕截图:

肖像(正确):

enter image description here

旋转后的风景(不正确):

enter image description here

Lanscape应该如何显示:

enter image description here

如果您想在设备上查看链接本身,请参阅以下链接:

http://bit.ly/1ukGNbB

1 个答案:

答案 0 :(得分:3)

经过一番挖掘后,这里的代码可以在旋转到移动设备上的横向视图时滚动到顶部:

window.onorientationchange = function () {

    var orientation = window.orientation;

    // Look at the value of window.orientation:

    if (orientation === 0) {

        // device is in Portrait mode.

    } else if (orientation === 90) {

        // device is in Landscape mode. The screen is turned to the left.

        $('body').animate({
            scrollTop: '0'
        }, 0);

    } else if (orientation === -90) {

        // device is in Landscape mode. The screen is turned to the right.

        $('body').animate({
            scrollTop: '0'
        }, 0);

    }

};