ScrollIntoView() causing the whole page to move

后端 未结 12 1950
庸人自扰
庸人自扰 2020-11-30 21:40

I am using ScrollIntoView() to scroll the highlighted item in a list into view. When I scroll downwards ScrollIntoView(false) works perfectly. But when I scroll upwards, Scr

相关标签:
12条回答
  • 2020-11-30 22:01

    Fixed it with:

    element.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' })
    

    see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

    0 讨论(0)
  • 2020-11-30 22:01

    Using Brilliant's idea, here's a solution that only (vertically) scrolls if the element is NOT currently visible. The idea is to get the bounding box of the viewport and the element to be displayed in browser-window coordinate space. Check if it's visible and if not, scroll by the required distance so the element is shown at the top or bottom of the viewport.

        function ensure_visible(element_id)
        {
            // adjust these two to match your HTML hierarchy
            var element_to_show  = document.getElementById(element_id);
            var scrolling_parent = element_to_show.parentElement;
    
            var top = parseInt(scrolling_parent.getBoundingClientRect().top);
            var bot = parseInt(scrolling_parent.getBoundingClientRect().bottom);
    
            var now_top = parseInt(element_to_show.getBoundingClientRect().top);
            var now_bot = parseInt(element_to_show.getBoundingClientRect().bottom);
    
            // console.log("Element: "+now_top+";"+(now_bot)+" Viewport:"+top+";"+(bot) );
    
            var scroll_by = 0;
            if(now_top < top)
                scroll_by = -(top - now_top);
            else if(now_bot > bot)
                scroll_by = now_bot - bot;
            if(scroll_by != 0)
            {
                scrolling_parent.scrollTop += scroll_by; // tr.offsetTop;
            }
        }
    
    0 讨论(0)
  • 2020-11-30 22:04

    I've added a way to display the imporper behavior of the ScrollIntoView - http://jsfiddle.net/LEqjm/258/ [it should be a comment but I don't have enough reputation]

    $("ul").click(function() {
        var target = document.getElementById("target");
    if ($('#scrollTop').attr('checked')) {
        target.parentNode.scrollTop = target.offsetTop;    
    } else {
            target.scrollIntoView(!0);
    }
    });
    
    0 讨论(0)
  • 2020-11-30 22:08

    i had the same problem, i fixed it by removing the transform:translateY CSS i placed on the footer of the page.

    0 讨论(0)
  • 2020-11-30 22:09

    Adding more information to @Jesco post.

    • Element.scrollIntoViewIfNeeded() non-standard WebKit method for Chrome, Opera, Safari browsers.
      If the element is already within the visible area of the browser window, then no scrolling takes place.
    • Element.scrollIntoView() method scrolls the element on which it's called into the visible area of the browser window.

    Try the below code in mozilla.org scrollIntoView() link. Post to identify Browser

    var xpath = '//*[@id="Notes"]';
    ScrollToElement(xpath);
    
    function ScrollToElement(xpath) {
        var ele = $x(xpath)[0];
        console.log( ele );
    
        var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
        if (isChrome) { // Chrome
            ele.scrollIntoViewIfNeeded();
        } else {
            var inlineCenter = { behavior: 'smooth', block: 'center', inline: 'start' };
            ele.scrollIntoView(inlineCenter);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 22:09

    in my context, he would push the sticky toolbar off the screen, or enter next to a fab button with absolute.

    using the nearest solved.

    const element = this.element.nativeElement;
    const table = element.querySelector('.table-container');
    table.scrollIntoView({
      behavior: 'smooth', block: 'nearest'
    });
    
    0 讨论(0)
提交回复
热议问题