简体   繁体   中英

problem with scrollto and scrolltop in chrome using jquery

I've tried using both scrollTo(y,x) and scrollTop(x-pos), values in the parenthesis being the offset.top values of individual DOM elements. These seem to work in all browsers initially, however when refreshing Chrome to resend the form submission, the page jumps to where it should be for a split second then jumps back to the top. This occurs when I scroll back to the top of the page prior to refreshing the page. Does Chrome save some sort of session variable or track the page position on refresh such that when refresh is clicked, the page focuses on the portion of the screen last viewed? And if so, is there a way to prevent such a reaction from Chrome? I've tried adding a setTimeout in the hopes that my scrolltop or scrollto (As stated, I tried both) wait until Chrome has reacted to the page load, but with no change to the result.

if ($('.register_box').length > 0) {
    if ($('.register_box').find('.error').length > 0) {
        var regbox = $('.register_box').offset();
        $('body').scrollTop(regbox.top);
    }
}

I've tried 'body', 'html', a selector wrapper, window, screen, and a combination of these - no change. I've also alerted the value for regbox.top and consistently get the value of 699, so I know it's passing the correct value. Do I need to stop propagation or prevent default, and if so, where? Firefox and IE both respond correctly. Chrome, on both Mac and PC, do not.

Try $('html, body'). See jsfiddle here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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