您的位置:首页 > Web前端 > JavaScript

JS 运动基础 提示栏拉出与隐藏

2016-03-19 15:55 671 查看
效果:



鼠标移到提示栏,自动拉出提示栏



<style>

*{
padding: 0;
margin: 0;
}
#div1{
width: 150px;
height: 200px;
background-color: orange;
position: relative;
left: -150px;
top: 10px;

}
span{
position: absolute;
width: 20px;
height: 60px;
line-height: 20px;
top: 70px;
background-color: gray;
left: 150px;
color: white;
}
</style>


<script>
window.onload=function(){
var oDiv=document.getElementById('div1');

oDiv.onmouseover=function(){
moveStart(0);
}

oDiv.onmouseout=function(){
moveStart(-150);
}
var timer=null;
//1.判断运动方向,设置速度;2.关闭已有定时器;3.if else 运动开始和停止
function moveStart(iTarget){
var speed=0;
if(oDiv.offsetLeft>iTarget){
speed=-10;
}
else{
speed=10;
}

clearInterval(timer);
timer=setInterval(function(){
if(oDiv.offsetLeft==iTarget){
clearInterval(timer);
}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30)
}
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript