[英]Object doesn't support property or method 'scrollBy' in IE11 in React
I am using the following code to scroll scroll an element on mouseDown, and stop scrolling on mouseUp/mouseOut.我正在使用以下代码在 mouseDown 上滚动滚动元素,并在 mouseUp/mouseOut 上停止滚动。
scrubby(xDir) {
let dub = setInterval(() => {
document.getElementById("chart").scrollBy(8 * xDir, 0);
}, 10);
this.setState({ dub: dub });
}
scrubbyStop() {
const { dub } = this.state;
if (dub !== null) {
clearInterval(dub);
}
this.setState({ dub: null });
}
This works everywhere except IE 11. In IE 11 I get the following error:这适用于除 IE 11 以外的所有地方。在 IE 11 中,我收到以下错误:
TypeError: Object doesn't support property or method 'scrollBy'
When I console.log the element, the document.getElementById to ensure I am selecting the element.当我 console.log 元素时,document.getElementById 以确保我选择了元素。
I am using babel-polyfil我正在使用 babel-polyfil
I see a lot of questions related to scrollTo, but not scrollBy.我看到很多与 scrollTo 相关的问题,但不是 scrollBy。 Does anyone know any workarounds, polyfill or alternatives that might work for IE.
有谁知道可能适用于 IE 的任何变通方法、polyfill 或替代方案。
Babel polyfill "will emulate a full ES2015+ environment". Babel polyfill“将模拟完整的 ES2015+ 环境”。 However,
scrollBy
is not part of the ECMAScript specification and thus will not be polyfilled by Babel.然而,
scrollBy
不是 ECMAScript 规范的一部分,因此不会被 BabelscrollBy
。 You need to add a proper polyfill by yourself, for example smooth scroll behaviour polyfill which also includes scrollBy
.您需要自己添加适当的 polyfill,例如平滑滚动行为
scrollBy
,其中还包括scrollBy
。
I'm over a year late to this question, but if anyone else is facing this issue and wants a solution that only fills in scrollBy
, I'm using jQuery:我对这个问题晚了一年多,但是如果其他人面临这个问题并且想要一个只填写
scrollBy
的解决方案,我正在使用 jQuery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
...and adding this to my page's JavaScript: ...并将其添加到我页面的 JavaScript 中:
if(!window.scrollBy){
window.scrollBy = function(x, y){
if(x) $('html, body').scrollLeft($(window).scrollLeft() + x);
if(y) $('html, body').scrollTop($(window).scrollTop() + y);
};
}
if(!Element.prototype.scrollBy){
Element.prototype.scrollBy = function(x, y){
if(x) $(this).scrollLeft($(this).scrollLeft() + x);
if(y) $(this).scrollTop($(this).scrollTop() + y);
};
}
It's tested and working in IE 11.它已在 IE 11 中测试和运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.