使用js写点击一个事件使页面返回顶部以及控制一个元素在右下角的固定位置的方法
2012-11-16 10:37
986 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body{margin:0; padding:0}
#to_top{
width:30px;
height:40px;
padding:20px;
font:14px/20px arial;
text - align: center;
background: #06c;
position: absolute;
cursor: pointer;
color: #fff;
}
</style>
</head>
<body style="height:1000px;">
<h1>返回顶部</h1 >
<div id = "to_top"> 返回顶部
</div>
</body>
</html>
<script type="text/javascript">
window.onload = function(){
var oTop = document.getElementById("to_top");
var screenw = document.documentElement.clientWidth || document.body.clientWidth;
var screenh = document.documentElement.clientHeight || document.body.clientHeight;
oTop.style.left = screenw - oTop.offsetWidth +"px";
oTop.style.top = screenh - oTop.offsetHeight + "px";
window.onscroll = function(){
var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
oTop.style.top = screenh - oTop.offsetHeight + scrolltop +"px";
}
oTop.onclick = function(){
document.documentElement.scrollTop = document.body.scrollTop =0;
}
}
/**jquery的写法实现返回顶部
jQuery("#to_top").click(function(){
jQuery("html,body").animate({'scrollTop':0},100)
})
**/
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body{margin:0; padding:0}
#to_top{
width:30px;
height:40px;
padding:20px;
font:14px/20px arial;
text - align: center;
background: #06c;
position: absolute;
cursor: pointer;
color: #fff;
}
</style>
</head>
<body style="height:1000px;">
<h1>返回顶部</h1 >
<div id = "to_top"> 返回顶部
</div>
</body>
</html>
<script type="text/javascript">
window.onload = function(){
var oTop = document.getElementById("to_top");
var screenw = document.documentElement.clientWidth || document.body.clientWidth;
var screenh = document.documentElement.clientHeight || document.body.clientHeight;
oTop.style.left = screenw - oTop.offsetWidth +"px";
oTop.style.top = screenh - oTop.offsetHeight + "px";
window.onscroll = function(){
var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
oTop.style.top = screenh - oTop.offsetHeight + scrolltop +"px";
}
oTop.onclick = function(){
document.documentElement.scrollTop = document.body.scrollTop =0;
}
}
/**jquery的写法实现返回顶部
jQuery("#to_top").click(function(){
jQuery("html,body").animate({'scrollTop':0},100)
})
**/
</script>
相关文章推荐
- js控制元素显示在屏幕固定位置及监听屏幕高度变化的方法
- js练习之事件切换图片和控制css样式以及指定页面滚动位置
- js通过contentWindow控制iframe子页面元素点击事件,并把值传给父页面
- js实现html页面滚动条向下拉到一定的程度时,某个div就固定在顶部,向上拉时返回原位置。
- 使用JS控制页面元素位置移动
- js控制元素显示在屏幕固定位置及监听屏幕高度变化的方法
- 使用jquery on()方法解决html页面点击元素,事件无响应的问题
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- 使用JS控制页面元素位置移动
- js 点击一个链接后变色,但在点击页面其它链接不变色(方法二)
- js事件之点击不同按钮固定位置实现内容切换
- 在HTML页面上点击按钮按钮携带参数flag,触发一个JS函数,经过判断是否flag==1,分别跳到两个指定的方法(或者页面)
- html js点击按钮滚动跳转定位到页面指定位置(DIV)的方法代码
- js控制div 在页面的固定位置
- js 界面滚动方法 点击返回到顶部
- 点击事件然后页面跳转到指定DOM元素的位置
- 用类名做方法的返回值类型 在学习Java的初始阶段,很多同学使用基本数据类型定义变量和引用类型定义变量以及使用类名做方法的返回值类型常常不知所以。今天我以自己的解读方式和个人的理解作一个简单的解释,和
- JS检测页面中哪个HTML标签触发点击事件的方法
- 解决点击空链接返回页面顶部的方法
- js中带运动的返回顶部以及锚点的使用