頁面向上向下滾動,分享到的模塊隨著滑動。
要點:
代碼如下:
var scrtop =document.documentElement.scrollTop||document.body.scrollTop;
var height = document.documentElement.clientHeight||document.body.clientHeight;
var top = scrtop + (height - jb51.offsetHeight)/2;
top = parseInt(top);
獲得頁面垂直居中的位置
上代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312" />
<title>無標題文檔</title>
<style>
body{margin:0; padding:0; font:12px/1.5 arial; height:2000px;}
#jb51{width:100px; height:200px; line-height:200px;
text-align:center; border:1p solid #ccc;
background:#f5f5f5; position:absolute; left:-100px; top:0;}
#jb51_tit{position:absolute; right:-20px; top:60px;
width:20px; height:60px; padding:10px 0;
background:#06c; text-align:center;
line-height:18px; color:#fff;}
</style>
<script>
window.onload = function(){
var jb51 = document.getElementById("jb51");
jb51.onmouseover = function(){
startrun(jb51,0,"left")
}
jb51.onmouseout = function(){
startrun(jb51,-100,"left")
}
window.onscroll = window.onresize = function(){
var scrtop=document.documentElement.scrollTop||document.body.scrollTop;
var height=document.documentElement.clientHeight||document.body.clientHeight;
var top = scrtop + (height - jb51.offsetHeight)/2;
top = parseInt(top);
startrun(jb51,top,"top")
}
}
var timer = null
function startrun(obj,target,direction){
clearInterval(timer);
timer = setInterval(function(){
var speed = 0;
if(direction == "left"){
speed = (target-obj.offsetLeft)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetLeft == target){
clearInterval(timer);
}else{
obj.style.left = obj.offsetLeft + speed + "px";
}
}
if(direction == "top"){
speed = (target-obj.offsetTop)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetTop == target){
clearInterval(timer);
}else{
obj.style.top = obj.offsetTop + speed + "px";
}
document.title = obj.offsetTop + ',' + target + ',' +speed;
}
},30)
}
</script>
</head>
<body>
<div id="jb51">
分享到內容
<span id="jb51_tit">分享到</span>
</div>
</body>
</html>