原生js实现回到顶部
网页可见区域宽: document.body.clientWidth;
网页可见区域高: document.body.clientHeight;
网页可见区域宽: document.body.offsetWidth (包括边线的宽);
网页可见区域高: document.body.offsetHeight (包括边线的宽);
网页正文全文宽: document.body.scrollWidth;
网页正文全文高: document.body.scrollHeight;
网页被卷去的高: document.body.scrollTop;
网页被卷去的左: document.body.scrollLeft;
网页正文部分上: window.screenTop;
网页正文部分左: window.screenLeft;
屏幕分辨率的高: window.screen.height;
屏幕分辨率的宽: window.screen.width;
屏幕可用工作区高度: window.screen.availHeight;
屏幕可用工作区宽度:window.screen.availWidth;
scrollHeight: 获取对象的滚动高度。
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置
offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置
event.clientX 相对文档的水平座标
event.clientY 相对文档的垂直座标
event.offsetX 相对容器的水平坐标
event.offsetY 相对容器的垂直坐标
document.documentElement.scrollTop 垂直方向滚动的值
event.clientX+document.documentElement.scrollTop 相对文档的水平座标+垂直方向滚动的量
scrollTo 表示滚动到多少像素
scrollBy 表示滚动多少像素
scrollTop 不太兼容
//非chrom
document.documentElement.scrollTop
//chrom
document.body.scrollTop
//兼容写法
scrollTop=document.documentElement.scrollTop||document.body.scrollTop
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style>
body {
height: 5000px;
}
a {
position: fixed;
right: 100px;
width: 50px;
height: 50px;
background: red;
color: white;
text-align: center;
line-height: 50px;
display: block;
}
.toTop {
bottom: 160px;
}
.toBottom {
bottom: 100px;
}
</style>
<script>
window.onload = function() {
var oA = document.getElementsByTagName('a');
oA[0].onclick = function() {
toScroll("top")
}
oA[1].onclick = function() {
toScroll("bottom")
}
}
function toScroll(target) {
clearInterval(this.timer)
var scrollHeight = document.body.offsetHeight - document.documentElement.clientHeight;
this.timer = setInterval(function() {
var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
switch (target) {
case 'top':
console.log(scrolltop);
window.scrollBy(0, -100);
if (Math.ceil(scrolltop) <= 0) {
clearInterval(this.timer)
};
break;
case 'bottom':
console.log(scrolltop);
window.scrollBy(0, 100);
if (Math.ceil(scrolltop) >= scrollHeight) {
clearInterval(this.timer)
};
break;
}
}, 30)
}
</script>
</head>
<body>
<a href="javascript:;" class="toTop">上</a>
<a href="javascript:;" class="toBottom">下</a>
</body>
</html>
原生js实现回到顶部的更多相关文章
- JS实现回到顶部效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 基于原生js的返回顶部组件,兼容主流浏览器
基于原生js的返回顶部插件,兼容IE8及以上.FF.chrome等主流浏览器. js文件中封装了getScrollTop()和changeScrollTop()函数分别用于获取滚动条滚动的高度和修改滚 ...
- js实现回到顶部功能
js实现回到顶部功能 一.总结 一句话总结: 可以通过js或者jquery可以很快的控制页面的属性,比如高度等等 //设置当前视口的顶端数值 var setScrollTop = function(t ...
- 页面滚动事件和利用JS实现回到顶部效果
页面滚动 事件:window.onscroll, 获得页面滚动位置:document.body.scrollTop: HTML代码: 这里注意此处逻辑,大于500就显示,否则就隐藏,还有注意如果变量名 ...
- 通过js实现回到顶部功能
许多商城网址,当我们滚动到一定高度时,我们会发现一般会出现一个回到顶部的js选项,点击轻松实现回到顶部,交互效果会显得比较人性化,且回到顶部过程中若在滚动滚动条时可以停止滚动,现在让我们来实现吧 我总 ...
- js——页面回到顶部
很久都没有去慕课网学习学习了,刚恰好就看见了一个用的比较多的小例子——页面回到顶部,记得之前自己也是在初学web时,被这个坑了一回,因此今天特地拿来分享分享…… <!DOCTYPE html&g ...
- js实现回到顶部操作
<a href="javascript:;" id="btn" title="回到顶部"></a> 样式: #btn ...
- 原生js实现返回顶部特效
index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- 原生JS实现返回顶部和滚动锚点
;(function (window,doc,undefined) { function rollingAnchor(){ this.timer =''; } rollingAnchor.protot ...
随机推荐
- nyoj 239 月老的难题【匈牙利算法+邻接表】
月老的难题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 月老准备给n个女孩与n个男孩牵红线,成就一对对美好的姻缘. 现在,由于一些原因,部分男孩与女孩可能结成幸福 ...
- 3D视频的质量评价报告 (MSU出品)
俄罗斯的MSU Graphics & Media Lab (Video Group)出品的3D视频的质量评价报告.测试了一些3D视频的质量,其测试方法值得我们参考.在此翻译一下部分文字. 注: ...
- K倍动态减法游戏
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2580 #include <iostream> #include <string.h> ...
- BufferedInputStream详解
BufferedInputStream是一个带有缓冲区域的InputStream,它的继承体系如下: InputStream |__FilterInputStream |__Bu ...
- config 数据库字符串的读取、修改 分类: WebForm 2014-12-16 10:24 203人阅读 评论(0) 收藏
config数据库字符串: <connectionStrings> <add name="MyWebDataString" connectionString= ...
- 关于java里小数点的保留
关于java里小数点的保留 1.先给大家看一个代码. import java.util.*;import java.text.*; public class A { public static voi ...
- Linq to Object实现分页获取数据
最后调用的CSharp代码可以是: int totalcount = dboperator.Customers.Count(); //总记录数 int pagesize = 100;//每页显示数 / ...
- [io PWA] Great libraries and tools for great Progressive Web Apps
sw-toolbox: Github It provides a cononical implementation of all the runtime caching strategies that ...
- CHAR 详解
CHAR(20):20指的是表中的a字段能存储的最大字符个数 CREATE TABLE `a` ( `a` char(20) DEFAULT NULL) ENGINE=InnoDB DEFAULT C ...
- 用 Qt 中的 QDomDocument类 处理 XML 文件(下)
QDomDocument doc; 1).创建根节点:QDomElement root = doc.documentElement("rootName " ); 2).创建元素 ...