简体   繁体   English

在iPad / iPhone上为window.scrollTo

[英]window.scrollTo on iPad/iPhone orientation

Upon load, I am positioning the users scroll to the top of the '#gallery-view' element. 加载后,我将用户滚动到“#gallery-view”元素的顶部。

This is working correctly on iPad and iPhone. 在iPad和iPhone上可以正常使用。 However, if I change the orientation of the device, its not positioning. 但是,如果我更改设备的方向,则不会定位。

I have run tests to check the code is run on orientation change. 我已经运行测试以检查代码是否在方向更改上运行。 The 'resize' option is triggering correctly and the 'test' console.log is output. “调整大小”选项正确触发,并且输出“ test” console.log。

Here is the code: 这是代码:

jQuery.noConflict();

function winResize() {

    if ( jQuery('#gallery-view').length ) {

        console.log('test');

        window.setTimeout(function() {
            window.scrollTo(0, jQuery('#gallery-view').offset().top);
        }, 0);

    }

}

jQuery(window).on('load', function() {

    jQuery(window).resize(winResize); 
    winResize();

});

Does anyone know of any reason why this wouldn't trigger on orientation change? 有谁知道什么原因不会导致方向改变?

Try attaching your winResize handler function to the orientationchange event as well. 试试你的连接winResize处理函数的orientationchange事件也是如此。 See the MDN docs . 请参阅MDN文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM