console.log(document.documentElement.scrollTop);
console.log($("body").scrollTop());
console.log($(document).scrollTop());
这些打印出来都是0
现在有个需求:点击tab栏上的按钮,滚动(无需动画)到页面的对应位置
有两个思路:
1.用滚动事件做,但是滚动的距离始终为0
2.用锚点链接做,但是无法跳转,也不报错
有什么解决办法吗?或者有别的思路能实现这个需求吗?
console.log(document.documentElement.scrollTop);
console.log($("body").scrollTop());
console.log($(document).scrollTop());
这些打印出来都是0
现在有个需求:点击tab栏上的按钮,滚动(无需动画)到页面的对应位置
有两个思路:
1.用滚动事件做,但是滚动的距离始终为0
2.用锚点链接做,但是无法跳转,也不报错
有什么解决办法吗?或者有别的思路能实现这个需求吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="target" style="margin-top: 5000px;height: 10px;background-color: #ccc;"></div>
<script>
setTimeout(() => {
document.getElementById("target").scrollIntoView();
}, 1000);
</script>
</body>
</html>
参考下