admin管理员组文章数量:1332724
Hi I have problem with my code. I am trying to do simple scroll function, but i still received an error.
<script>
function pageScroll(el) {
var yPos;
page = document.getElementById(el);
var height = page.offsetTop;
window.scroll(0, height);
}
</script>
<button class="module-box" onclick="pageScroll(about)">READ</button>
Hi I have problem with my code. I am trying to do simple scroll function, but i still received an error.
<script>
function pageScroll(el) {
var yPos;
page = document.getElementById(el);
var height = page.offsetTop;
window.scroll(0, height);
}
</script>
<button class="module-box" onclick="pageScroll(about)">READ</button>
Share
Improve this question
asked May 26, 2016 at 9:46
matimati
971 gold badge3 silver badges11 bronze badges
5
-
This means
page
doesn't store what you think it does. – Mitya Commented May 26, 2016 at 9:47 -
1
Try sending the argument as a string - instead of
pageScroll(about)
try usingpageScroll('about')
. This should do the trick. – Leo Napoleon Commented May 26, 2016 at 9:49 - I suppose that the problem lies in the variable page, but you can explain to me what I'm doing wrong? – mati Commented May 26, 2016 at 9:50
- @LeoNapoleon THANKS! – mati Commented May 26, 2016 at 9:51
- about should be a string to be able call getElementById so replace pageScroll(about) with pageScroll('about') – Vladu Ionut Commented May 26, 2016 at 9:51
1 Answer
Reset to default 2Working code. Need to pass id as string to the function.
function showTop(el)
{
var page = document.getElementById(el);
var height = page.offsetTop;
alert(height);
}
<div id="new"></div>
<button onclick = "showTop('new')">Click me</button>
<div id='op'></div>
本文标签: javascriptCannot read property 39offsetTop39 of nullStack Overflow
版权声明:本文标题:javascript - Cannot read property 'offsetTop' of null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742340739a2456552.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论