简体   繁体   English

Element.scrollIntoView(); 不在 Chrome 和 Edge 中工作,在 Firefox 中工作?

[英]Element.scrollIntoView(); Not working in Chrome and Edge, Works in Firefox?

Let me preface by saying I am relatively green in respect to JavaScript.让我先说我在 JavaScript 方面比较环保。 However, I am trying to create a JavaScript function that will listen for a wheel event, determine the direction of the scroll and scroll the next part of the page into view.但是,我正在尝试创建一个 JavaScript function 来侦听滚轮事件,确定滚动方向并将页面的下一部分滚动到视图中。 Similar to a swipe gesture.类似于滑动手势。 I have it working in Firefox as intended, user scrolls down the page moves down, up the page moves up.我按预期在 Firefox 中工作,用户向下滚动页面向下移动,向上页面向上移动。 However, in Chrome and Edge, the element.scrollIntoView() function seems to be called, but nothing happens.但是,在 Chrome 和 Edge 中, element.scrollIntoView() function 似乎被调用,但没有任何反应。

I am using Blazor with JSInterop to get the page to scroll.我正在使用 Blazor 和 JSInterop 来滚动页面。

Here is the gist of the file:这是文件的要点:

// scrolldetection.js //

window.scrolldetection {
    scrollSetup: function (elementId) {
        autoscroller(elementId);
    }
}

function autoscroller(elementId) {
    // Set up vars
    var idList = document.getElementById(elementId).children.id;
    var idListIndex = // Gets current index based on current location on page
    var curDirection;
    
    document.addEventListener("wheel", function () {
        var currentPos = window.scrollY;
        if (!curDirection) {
            // Check for what direction user scrolls

            idListIndex = // Function that determines new index
            scrollScreen(idList, idListIndex);
        }
        var timerId = setTimeout(function () {
            curDirection = undefined;
            clearTimeout(timerId);
        }, 700);
    }
}

function scrollScreen(idList, curIndex) {
    console.log("list index: " + curIndex);
    element = document.getElementById(idList[curIndex]);
    console.log(element.id);
    element.scrollIntoView({ behavior: 'smooth' });
}

I have another function that calls scrollIntoView() on the same elements, via a button press.我有另一个 function 通过按下按钮在相同的元素上调用 scrollIntoView() 。 Works just fine.工作得很好。

Here is that function:这是function:

// anchorlink.js // This one works as intended //

window.anchorlink = {
    scrollIntoView: function (elementId) {
        var elem = document.getElementById(elementId);
        if (elem) {
            elem.scrollIntoView({ behavior: 'smooth' });
        }
    }
}

The console.log() calls within the scrollScreen() function show up properly in the console in each browser. scrollScreen() function 中的 console.log() 调用正确显示在每个浏览器的控制台中。 Leading me to believe that element.scrollIntoView() is being called, but something is going wrong to make it not function appropriately.让我相信 element.scrollIntoView() 正在被调用,但是出了点问题,使它不适合 function 。 If there is any other information you need, I will be happy to provide, thanks.如果您需要任何其他信息,我将很乐意提供,谢谢。

After working on this some more, I decided the concept I was using wasn't as user friendly as I had hoped.在做了更多的工作之后,我决定我使用的概念并不像我希望的那样用户友好。

I decided to let the user scroll freely along the page and when they stop scrolling, then determine the closest DOM element and scroll to it.我决定让用户在页面上自由滚动,当他们停止滚动时,确定最近的 DOM 元素并滚动到它。 Works on Edge, Chrome and Firefox.适用于 Edge、Chrome 和 Firefox。

window.contentcenter = {
    contentCenter: function (elementId) {
        var centeringFunction = debounce(function () { autocenter(elementId) }, 200);
        document.addEventListener("scroll", centeringFunction);
    }
}

function autocenter(elementId) {
    var currentElement = detectCurrentElement(elementId);
    currentElement.scrollIntoView({ behavior: "smooth" });
}

function detectCurrentElement(elementId) {
    var element = document.getElementById(elementId);
    var currentPos = window.scrollY;
    var contentIdList = getContentIdList(elementId);
    var currentElement = closestContent(currentPos, element, contentIdList);

    return currentElement;
}

function closestContent(pos, element, contentIdList) {
    var contentId = Math.round(pos / (element.offsetHeight / contentIdList.length));
    var currentElement = document.getElementById(contentIdList[contentId]);
    return currentElement;
}

function getContentIdList(elementId) {
    var idList = []
    var childElements = document.getElementById(elementId).children;
    for (var i = 0; i < childElements.length; i++) {
        idList.push(childElements[i].id);
    }
    return idList;
}

function debounce(func, timeout) {
    var timer;
    return function () {
        var context = this, args = arguments;
        var later = function () {
            timer = null;
            func.apply(context, args);
        };
        clearTimeout(timer);
        timer = setTimeout(later, timeout)
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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