说明:正常状态下如图
当鼠标放到“为你推荐”上时 显示效果如图 此过程有过渡效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{padding: 0;margin: 0;}
#ad{width: 200px;height: 250px;background-color: red;position: relative;
top: 300px;left: -200px;}
span{position: absolute;display: inline-block;width: 25px;height: 86px;background-color: #ccc;
right: -25px;top: 82px;}
</style>
<script type="text/javascript">
window.onload=function () {
var o=document.getElementById('ad');
var timer;
//添加鼠标经过事件
o.onmouseover=function(){
clearInterval(timer);
timer=setInterval(moveRight,10);
};
//鼠标离开事件
o.onmouseout=function(){
clearInterval(timer);
timer=setInterval(moveLeft,10);
};
function moveRight(){
var preLeft=o.offsetLeft;
if (preLeft>=0) {
clearInterval(timer);
}else {
o.style.left=o.offsetLeft+2+'px';
}
}
function moveLeft(){
var preLeft=o.offsetLeft;
if (preLeft<=-200) {
clearInterval(timer);
}else {
o.style.left=o.offsetLeft-2+'px';
}
}
};
</script>
</head>
<body>
<div id="ad">
<span>为你推荐</span>
</div>
</body>
</html>