Preventing scroll when using URI hash to identify tab

前端 未结 8 1855
陌清茗
陌清茗 2020-12-11 16:05

I\'m using JQuery UI to make tabs in my application. I needed the tabs to be linkable (direct link that opens the page and selects the correct tab). This is done by using a

8条回答
  •  星月不相逢
    2020-12-11 16:44

    I just added the following to my javascript:

        $('#tabs').tabs();
        // Direct links to the tabs, e.g. /my-page#tab-id can cause browsers
        // to scroll down to half-way down the page, which is ugly. We override that here.
        // (This can cause a brief FOUC effect where the page first displays then scrolls up)
        window.scrollTop = '0';
        window.scrollTo(0,0);
    

    In MSIE, I get a brief FOUC effect as the page loads scrolled part-way-down, then flicks to the top.

    In Firefox, this works fine without any visible FOUC.

    In Chrome, this doesn't work at all -- see scrollTop does not work in Chrome, nor do suggested workarounds

提交回复
热议问题