开发者

Cannot call method 'scrollTo' of undefined

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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜