---
title: "js在html页面回到顶部的做法"
author: "maria3905"
date: "2015-06-01T10:54:36Z"
tags: []
categories: ["Web前端"]
slug: "js-html"
draft: false
lastmod: "2015-06-01T10:54:36Z"
id: 2215830
url: "https://maria3905.iteye.com/blog/2215830"
---

效果图  
![](http://dl2.iteye.com/upload/attachment/0109/1123/e0a67656-36d6-3283-9728-cb69cefbc533.png)  
 先引入js

&lt;script type="text/javascript" src="${contextPath}js/common/returnTop.js"&gt;&lt;/script&gt;

```
lastScrollY=0;
function heartBeat(){
var diffY;
if (document.documentElement && document.documentElement.scrollTop)
diffY = document.documentElement.scrollTop;
else if (document.body)
diffY = document.body.scrollTop
else
{/*Netscape stuff*/}
percent=.1*(diffY-lastScrollY);
if(percent>0)percent=Math.ceil(percent);
else percent=Math.floor(percent);
document.getElementById("full").style.top=parseInt(document.getElementById("full").style.top)+percent+"px";
lastScrollY=lastScrollY+percent;
}
suspendcode="<div id=\"full\" style='right:2px;POSITION:absolute;TOP:600px;z-index:100'><a onclick='window.scrollTo(0,0);'><img src='../img/www/top.png'></img></a><br></div>"
document.write(suspendcode);
window.setInterval("heartBeat()",1);
```