javascript ScrollTo 方法在 Edge 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51517324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:35:30  来源:igfitidea点击:

ScrollTo method doesn't work in Edge

javascriptmicrosoft-edge

提问by Valerii Voronkov

I use scroll in my method

我在我的方法中使用滚动

const scrollToCenter = () => {
 const kon = document.querySelector('.clazz');
 const width = kon.offsetWidth;
 kon.scrollTo(width / 2, 0);
 };

and it works fine in Chrome and Mozilla. In the Edge I'd got an error "Object doesn't support property or method scrollTo". Is exist any different method to use it in Edge?

它在 Chrome 和 Mozilla 中运行良好。在 Edge 中,我收到错误消息“对象不支持属性或方法 scrollTo”。是否存在在 Edge 中使用它的任何不同方法?

回答by Roberto Kedmenec

The official report : https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/15534521/

官方报告:https: //developer.microsoft.com/en-us/microsoft-edge/platform/issues/15534521/

The issue has been assigned to someone.

问题已分配给某人。

Following the comments the workaround is

根据评论,解决方法是

" .scrollTop = 0instead of .scrollTo(0,0)In case you want to scroll somewhere else than top i haven't found a workaround "

.scrollTop = 0而不是.scrollTo(0,0)如果您想滚动到顶部以外的其他地方,我还没有找到解决方法”

回答by Valerii Voronkov

I change it on the scrollIntoView - firstly, it work and app doesn't break. I'll test in on another browsers and if it works correctly, so it will be the solve on my question.

我在 scrollIntoView 上更改它 - 首先,它可以工作并且应用程序不会中断。我将在其他浏览器上进行测试,如果它工作正常,那么这将是我问题的解决方案。

k-nut's answer was correct.

k-nut的答案是正确的。