不說費話了,直接看代碼,唯一的說明 ,需要自己添加jQuery庫
<html><head><script language="javascript" src="jquery.min-1.4.2.js"></script></head><body><pre>12345678901234567890123456789012345678901234567890</pre><div style="bottom: 0;left: 50%;margin-left: 400px;position: fixed;width: 24px;" id="GoTop"><a href="javascript:;">返回頂部</a></div> <script type="text/javascript"> jQuery.fn.topLink = function(settings) { settings = jQuery.extend({ min: 1, fadeSpeed: 200, ieOffset: 50 }, settings); return this.each(function() { var el = $(this); el.css('display', 'none'); $(window).scroll(function() { if (!jQuery.support.hrefNormalized) { el.css({ 'position': 'absolute', 'top': $(window).scrollTop() + $(window).height() - 100 }); } if ($(window).scrollTop() >= settings.min) { el.show(); } else { el.hide(); } }); }); }; $(document).ready(function () { $('#GoTop').topLink({ min: 10, fadeSpeed: 500 }); $('#GoTop').click(function (e) { $("html,body").animate({"scrollTop":"0"},"slow") }); });</script></body><html>