分享侧边栏——js代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{margin: 0;padding: 0;}
#div1{position: relative;width: 150px;height: 150px;background: red; top: 50px; left: -150px; }
#sp{position: absolute; background: #ccc;top: 40px;right: -20px; width: 20px;height: 70px;}
</style>
<script>
// //第一种方法:
// window.οnlοad=function(){
// var oDiv1=document.getElementById('div1'); 
// oDiv1.οnmοuseοver=function()
// {
// startMove();
// };
// oDiv1.οnmοuseοut=function(){
// startMove2();
// };
// }
// //设一个全局变量 timer=null; 
// var timer=null;
// function startMove(){
// clearInterval(timer);
// var oDiv1=document.getElementById('div1');
// timer=setInterval(function(){
// if (oDiv1.offsetLeft==0){
// clearInterval(timer);
// }
//                 //若用style.left要加px;即使是0也要加px;否则不能识别  不会跑startMove函数。
// if (oDiv1.style.left==0+'px'){
// clearInterval(timer);
// }
// else{
// oDiv1.style.left=oDiv1.offsetLeft+10+'px';
// }
// },30) 
// }
// function startMove2(){
// clearInterval(timer);
// var oDiv1=document.getElementById('div1');
// timer=setInterval(function(){
// if (oDiv1.offsetLeft==-150){
// clearInterval(timer);
// }
// else{
// oDiv1.style.left=oDiv1.offsetLeft-10+'px';
// }
// },30) 
//
//第二种方法:
// window.οnlοad=function(){
// var oDiv=document.getElementById('div1'); 
// oDiv.οnmοuseοver=function()
// {
// startMove(oDiv,0,10);
// };
// oDiv.οnmοuseοut=function(){
// startMove(oDiv,-150,-10);
// };
// }
// //设一个全局变量 timer=null; 
// var timer=null;
// function startMove(obj,iTarget,speed){
// clearInterval(timer);
// timer=setInterval(function(){
// if (obj.style.left==iTarget+'px'){
// clearInterval(timer);
// }
// else{
// obj.style.left=obj.offsetLeft+speed+'px';
// }
// },30) 
//


//第三种方法  只需要一个参数,使得函数调用越来越简单,但是这是在知道speed的情况下
window.οnlοad=function(){
var oDiv=document.getElementById('div1'); 
oDiv.οnmοuseοver=function()
{
startMove(0); 
};
oDiv.οnmοuseοut=function(){
startMove(-150); 
};
}
//设一个全局变量 timer=null; 
var timer=null;
function startMove(iTarget){ 
var oDiv=document.getElementById('div1');  
clearInterval(timer);
timer=setInterval(function(){
var speed=0;
               if(iTarget<oDiv.offsetLeft)
{
speed=-10;
}
else
{
speed=10;
}
if (oDiv.offsetLeft==iTarget)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
document.title=speed;
}
},30) 
}  
</script>
</head>
<body>
<div id="div1">
<span id="sp">分享到</span>
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值