<!DOCTYPE html> <html> <head> <title>页面中常见返回顶部效果(TOP)</title> <meta charset="UTF-8"> <link rel="shortcut icon" href="static/images/logo.png" type="image/x-icon" /> <link rel="stylesheet" href="static/css/style.css" type="text/css"> <link rel="stylesheet" type="text/css" href="static/font-awesome/css/font-awesome.min.css"> <script type="text/javascript" src="static/js/jquery.js"></script> <script> $(function(){ $('#back').hide(); $(window).scroll(function(){ if($(window).scrollTop()>150){ $('#back').fadeIn(1000); }else{ $('#back').fadeOut(1000); } }) }) </script> </head> <body> <a href="#top"></a> <div>网页顶部 </div> <p id="back"><a href="" id="top"><span class="fa fa-arrow-circle-up"></span><br/>返回顶部</a> </p> </body> </html>
*{ margin: 0px; padding: 0px; } body{ text-align: center; } div{ width: 1200px; height:2000px; background: lightblue; margin: 30px auto; } p#back{ width:80px; height:60px; background: #ccc; position: fixed; bottom: 90px; right:10px; text-align: center; border-radius: 10px; } p#back:hover{ background: #6D6E6A; } a{ text-decoration: none; color:black; }
总结:通过这个案例,复习了返回页面顶部的操作,即锚点的使用。同时巩固了jquery中的scroll()方法。