页面实现滑动JS代码

发布网友 发布时间:2022-04-21 19:07

我来回答

2个回答

热心网友 时间:2022-04-07 08:08

js实现随页面滑动效果的方法。具体如下:
页面向上向下滚动,分享到的模块随着滑动。

要点:
代码如下:
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);

获得页面垂直居中的位置

上代码:

<!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>

热心网友 时间:2022-04-07 09:26

这个很简单,你可以直接用jquery的向下滑动函数slideDown() 和向上滑动函数slideUp()就可以实现了,w3school的这个网站(www.w3school.com.cn/jquery/)里面有,连例子都是很多,你直接拷贝下来,改动下就OK哈

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com