无法调用方法“scrollTo”未定义的
我希望 iScroll 在页面加载时滚动到某个位置,我将此代码放在页脚的最底部。
<script>
$(document).ready(function() {
myScroll.scrollTo(0, -1389, 200);
});
</script>
但是在 Chrome 中遇到此错误
Uncaught TypeError: Cannot call method 'scrollTo' of undefined
当我在命令行上单独执行时,
myScroll.scrollTo(0, -1389, 200);
它工作正常,文档滚动到正确的位置。
myScroll 在 iScroll 实例化时设置,这段代码在 header 中
<script type="text/javascript">
var myScroll;
function loaded() {
setTimeout(function () {
myScroll = new iScroll('content');
}, 100);
}
window.addEventListener('load', loaded, false);
</script>
I want an iScroll to scroll to a certain location on page load, I put this code in the very bottom of my footer.
<script>
$(document).ready(function() {
myScroll.scrollTo(0, -1389, 200);
});
</script>
But am getting this error in Chrome
Uncaught TypeError: Cannot call method 'scrollTo' of undefined
when I execute
myScroll.scrollTo(0, -1389, 200);
alone on the command line it works fine, the document scrolls to the right location.
myScroll is set when iScroll is instantiated, this code is in the header
<script type="text/javascript">
var myScroll;
function loaded() {
setTimeout(function () {
myScroll = new iScroll('content');
}, 100);
}
window.addEventListener('load', loaded, false);
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您存在范围问题,因为“myScroll”不在您定义的函数的范围内。如果它是 DOM 元素,您可能应该获取该元素并将其在函数范围内定义为 myScroll,然后调用scrollTo() 函数。
I think you have a scope issue in that "myScroll" is not within the scope of your defined function. If it's a DOM element, you should probably get the element and define it as myScroll within the scope of your function and then call the scrollTo() function.