javascript之点击回到页面顶部
2017-11-09 11:58
316 查看
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>回到顶部案例</title> <style type="text/css"> body{ height:3000px; } #back-top{ display: none; position:fixed; right: 20px; bottom: 20px; width: 80px; height: 80px; text-align: center; line-height: 80px; color:#fff; background-color: rgba(0,0,0,0.5); cursor:pointer; transition: all .5s; } #back-top:hover{ background-color: rgba(0, 120, 215,0.8); } </style> </head> <body> <div id="back-top"> 回到顶部 </div> <h1 style="text-align: center;">标题</h1> <h3 style="margin-top:2980px;text-align: center;">底部内容</h3> <script> //监控文档中发生的滚动事件 document.onscroll=function(){ //获取滚动条距离顶部的位置 var st = document.body.scrollTop || document.documentElement.scrollTop; if(st > 80){ //显示 回到顶部 链接 $('back-top').style.display='block'; }else{ //隐藏 回到顶部 链接 $('back-top').style.display='none'; } } //点击按钮返回顶部 $('back-top').onclick=function(){ timer = setInterval(function(){ //不断获取最新滚动条距离顶部的高度 var t = document.body.scrollTop || document.documentElement.scrollTop; t = t- t/10; if(t <= 0){ //结束定时任务 clearInterval(timer); } document.body.scrollTop = document.documentElement.scrollTop= t; },20); } //简易封装 function $(id) { return document.getElementById(id); } </script> </body> </html>
相关文章推荐
- javascript 点击 回到页面顶部
- jquery实现点击回到页面顶部
- jquery点击回到页面顶部方法
- jquery实现点击页面回到顶部
- 基于JavaScript实现回到页面顶部动画代码
- 点击图标快速回到页面顶部
- 回到顶部,当页面下拉到一定高度时,就会出现回到顶部的按钮,点击回到顶部之后,会有一个速度的变化回滚到顶部,如果正在回到顶部时,鼠标的滚轮转动了就会停止回到顶部
- javascript回到顶部滑动效果及点击导航滑动到相应热点的效果
- 安卓开发启用线程动态显示和隐藏悬浮控件(ImageButton),点击回到页面顶部
- JavaScript---快速回到页面顶部
- JQuery点击事件回到页面顶部效果的实现代码
- 网站点击导航栏标题,对应的元素平滑上移以及点击图片或按钮回到页面顶部的特效
- javascript实现页面中回到顶部功能
- html页面标记 点击目录跳转到页面相应位置 简易回到顶部
- JQuery点击事件回到页面顶部效果
- 【JQ+锚标记实现点击页面回到顶部】
- 【JQ+锚标记实现点击页面回到顶部】
- 基于JavaScript实现点击页面任何位置返回
- jquery javascript 回到顶部功能
- 页面:回到顶部