Javascript scrollIntoView() middle alignment?

后端 未结 10 1350
刺人心
刺人心 2020-12-13 03:41

Javascript .scrollIntoView(boolean) provide only two alignment option.

  1. top
  2. bottom

What if I want to scroll the view such t

相关标签:
10条回答
  • 2020-12-13 04:03

    You can do it in two steps :

    myElement.scrollIntoView(true);
    var viewportH = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
    window.scrollBy(0, -viewportH/2); // Adjust scrolling with a negative value here
    

    You can add the height of the element if you want to center it globaly, and not center its top :

    myElement.scrollIntoView(true);
    var viewportH = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
    window.scrollBy(0, (myElement.getBoundingClientRect().height-viewportH)/2);
    
    0 讨论(0)
  • document.getElementById("id").scrollIntoView({block: "center"});
    
    0 讨论(0)
  • 2020-12-13 04:08

    try this :

     document.getElementById('myID').scrollIntoView({
                behavior: 'auto',
                block: 'center',
                inline: 'center'
            });
    

    refer here for more information and options : https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

    0 讨论(0)
  • 2020-12-13 04:12

    None of the solutions on this page work when an container other than the window/document is scrolled. The getBoundingClientRect approach fails with absolute positioned elements.

    In that case we need to determine the scrollable parent first and scroll it instead of the window. Here is a solution that works in all current browser versions and should even work with IE8 and friends. The trick is to scroll the element to the top of the container, so that we know exactly where it is, and then subtract half of the screen's height.

    function getScrollParent(element, includeHidden, documentObj) {
        let style = getComputedStyle(element);
        const excludeStaticParent = style.position === 'absolute';
        const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
    
        if (style.position === 'fixed') {
            return documentObj.body;
        }
        let parent = element.parentElement;
        while (parent) {
            style = getComputedStyle(parent);
            if (excludeStaticParent && style.position === 'static') {
                continue;
            }
            if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) {
                return parent;
            }
            parent = parent.parentElement;
        }
    
        return documentObj.body;
    }
    
    function scrollIntoViewCentered(element, windowObj = window, documentObj = document) {
        const parentElement = getScrollParent(element, false, documentObj);
        const viewportHeight = windowObj.innerHeight || 0;
    
        element.scrollIntoView(true);
        parentElement.scrollTop = parentElement.scrollTop - viewportHeight / 2;
    
        // some browsers (like FireFox) sometimes bounce back after scrolling
        // re-apply before the user notices.
        window.setTimeout(() => {
            element.scrollIntoView(true);
            parentElement.scrollTop = parentElement.scrollTop - viewportHeight / 2;
        }, 0);
    }
    
    0 讨论(0)
提交回复
热议问题