How to avoid anchors in iframes to scroll parent page

后端 未结 6 2320
一向
一向 2021-02-13 03:54

So, here\'s the problem: I have a quite long page with an iframe in the middle. Now, if an anchor is clicked in the iframe, the entire page is scrolled to the iframe, which is w

6条回答
  •  太阳男子
    2021-02-13 04:00

    Yes, you have to watch the links and do it using JQuery.

    $(document).on('click', 'A[href^="#"]', function(){
        var hash = $(this).attr('href');
        var o = $(hash);
        if (o.length) {
            // move it
            $("html,body").stop().animate({ scrollTop: o.offset().top }, 300, 'swing');
            if (window.frameElement) {
                // it has parent window => stop bubbling (will not change document location)
                return false;
            }
        }
    });
    

提交回复
热议问题