scrolltop javascript函数?

时间:2011-08-29 22:45:18

标签: html scroll href

在html中,通常人们使用滚动到页面顶部,如:

<a href="#top" title="Scroll back to the top">Back to the top</a>

但是我需要在点击链接后将网页转到顶部。 left.php文件:

<div class"ida0">
   <a href="news.php" target="right_frame"  onclick="scrollToTop()">Link 1</a>
</div>

因为我的网页是这样的。 * main.php * left.php * right.php

| ---------------------------- |
| ---------------------------- |
||一些图像||内容|
|| ---------------- ||的链接||
|| ---------------- || -------- ||
|| ---------------- || -------- ||
|| ---------------- || -------- ||
|| ----- link1 ------ || -------- ||
|| ----- link2 ------ || -------- ||
|| ---------------- || -------- ||
|| ---------------- || -------- ||

我用过这个但是没用。

<script language="javascript">
    function scrollToTop()
    {
   document.getElementById(ida0).scrollTop = 0; //It scrolls to top
    }
</script>

2 个答案:

答案 0 :(得分:4)

function scrollToTop() {
    window.scroll(0, 0);
}

答案 1 :(得分:1)

如果您想立即跳到顶部,可以使用window.scrollTo(0, 0),或者如果您想为滚动设置动画,则可以使用jQuery.smoothScroll之类的内容。