无法调用方法“scrollTo”未定义的

发布于 2024-12-03 15:13:28 字数 748 浏览 5 评论 0原文

我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-12-10 15:13:28

我认为您存在范围问题,因为“myScroll”不在您定义的函数的范围内。如果它是 DOM 元素,您可能应该获取该元素并将其在函数范围内定义为 myScroll,然后调用scrollTo() 函数。


<script>
$(document).ready(function() {
    myScroll = /* Get your element here */;
    myScroll.scrollTo(0, -1389, 200);
});
</script>

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.


<script>
$(document).ready(function() {
    myScroll = /* Get your element here */;
    myScroll.scrollTo(0, -1389, 200);
});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文