![](/img/trans.png)
[英]What's wrong with my code when I scroll down the result with Javascript
[英]What is wrong with my scroll down effect?
我想在我的网站上使用向下滚动效果。 我有几个锚链接。 用户在使用锚链接时应该平滑滚动到链接目标。
我的HTML代码看起来像这样(与锚点链接):
<li class="indent1">
<a href="#eigenschaften-dartscheibe"
onclick="test('eigenschaften-dartscheibe'); return false;">
Welche Eigenschaften einer Dartscheibe sind entscheidend
</a>
</li>
链接所针对的内容部分:
<h2 id="eigenschaften-dartscheibe" class="curtain">
Welche Eigenschaften einer Dartscheibe sind entscheidend?
</h2>
和我的Javascript:
function scrollTo(to, duration) { if (document.body.scrollTop == to) return; var diff = to - document.body.scrollTop; var scrollStep = Math.PI / (duration / 10); var count = 0, currPos; start = element.scrollTop; scrollInterval = setInterval(function(){ if (document.body.scrollTop != to) { count = count + 1; currPos = start + diff * (0.5 - 0.5 * Math.cos(count * scrollStep)); document.body.scrollTop = currPos; } else { clearInterval(scrollInterval); } },10); } function test(elID) { var dest = document.getElementById(elID); scrollTo(dest.offsetTop, 500); }
scoll效果不起作用......我的错误在哪里? 有人能帮助我吗?
最好的祝福
您正在尝试在未定义element
时访问该element
。 使用开发人员工具 - >控制台(F12或右键单击 - >在Chrome上检查)。
将元素从test
传递给scrollTo
:
function test(elID)
{
var dest = document.getElementById(elID);
// Here (dest)
scrollTo(dest, dest.offsetTop, 500);
}
然后使用它来检查滚动完成的时间:
if ((document.body.scrollTop + window.innerHeight - element.offsetHeight) < to) {
小提琴: https : //jsfiddle.net/xdmfpjm2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.