日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

用js实现分享到随页面滚动而滑动效果

發(fā)布時間:2025/7/14 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用js实现分享到随页面滚动而滑动效果 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

頁面向上向下滾動,分享到的模塊隨著滑動。

要點:

var scrtop =document.documentElement.scrollTop||document.body.scrollTop;
var height = document.documentElement.clientHeight||document.body.clientHeight;
var top = scrtop + (height - share.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;}
#share
{width:100px; height:200px; line-height:200px; text-align:center; border:1p solid #ccc; background:#f5f5f5; position:absolute; left:-100px; top:0;}
#share_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>
/*jingangel http://www.cnblogs.com/jingangel/ */
window.onload
= function(){
var share = document.getElementById("share");
share.onmouseover
= function(){
startrun(share,
0,"left")
}
share.onmouseout
= function(){
startrun(share,
-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 - share.offsetHeight)/2;
top = parseInt(top);
startrun(share,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="share">
分享到內(nèi)容
<span id="share_tit">分享到</span>
</div>

</body>
</html>



轉(zhuǎn)載于:https://www.cnblogs.com/jingangel/archive/2012/03/13/2394002.html

總結(jié)

以上是生活随笔為你收集整理的用js实现分享到随页面滚动而滑动效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。