JavsScript实现网页左右侧淡入淡出效果

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
body{overflow-X:hidden;}
#box1{
width: 150px;
height: 200px;
background-color: red;
position: absolute;
left: -150px;top: 100px;
}
#box1 span{
width: 20px;
height: 60px;
line-height: 20px;
display: inline-block;
position: absolute;right: -20px;top: 70px;
cursor: default;
}
#box2{
width: 150px;
height: 200px;
background-color: red;
position: absolute;
right: -150px;
top: 100px;
}
#box2 span{
width: 20px;
height: 60px;
line-height: 20px;
display: inline-block;
position: absolute;
left: -20px;
top: 70px;
cursor: default;
}
</style>
</head>
<body>
<div id="box1">
<span>分享到</span>
</div>
<div id="box2">
<span>分享到</span>
</div>
</body>
<script>
//左侧效果
(function(){
var oDiv = document.getElementById('box1');
var handler = null;
oDiv.onmouseover = function(){ 
startMove(0);
};
oDiv.onmouseout = function(){
startMove(-150);
};
function startMove(target){
clearInterval(handler);
var speed;
if(oDiv.offsetLeft>target){
speed = -5;
}else{
speed = 5;
};
handler = setInterval(function(){
if(oDiv.offsetLeft == target){
clearInterval(handler);
}else{
oDiv.style.left = oDiv.offsetLeft + speed + 'px';
};
},30);
};
})();



//右侧效果
(function(){
var oDiv = document.getElementById('box2');
var handler = null;
var bodyWidth = document.documentElement.clientWidth;//获取可视区域的宽度
oDiv.onmouseover = function(){
startMove(bodyWidth-150);
};
oDiv.onmouseout = function(){
startMove(bodyWidth);
};
function startMove(target){
clearInterval(handler);
var speed;
if(oDiv.offsetLeft > target){
speed = -5;
}else{
speed = 5;
};
handler = setInterval(function(){
if (oDiv.offsetLeft == target){
clearInterval(handler);
}else{
oDiv.style.left = oDiv.offsetLeft + speed +'px';
};
},30);
};
})();
</script>
</html>



























































































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值