您的位置:首页 > Web前端 > JavaScript

使用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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐