原生JS实现类似百度页面的分享效果
原生JS实现类似百度页面的分享效果<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>类似于网站的分享效果</title>
<style type="text/css">
#div1 {width:150px; height:200px;
background-color:#ff6a00;
position:absolute;
left:-150px;
top:50px;
}
#div2 {vertical-align:middle;
text-align:center;
width:20px;
height:60px;
background-color:#e64e4e;
position:absolute;
right:-20px;
top:80px;
}
</style>
<script type="text/javascript">
window.onload = function () {
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
div2.onmouseover=div1.onmouseover = function () {
start(0);
};
div2.onmouseout= div1.onmouseout = function () {
start(-150);
};
};
var time = null;
function start(target) {
var speed = 0;
if (div1.offsetLeft<target) {
speed = 10;
}
if(div1.offsetLeft>target) {
speed = -10;
}
if (div1.offsetLeft == target) {
speed = 0;
}
clearInterval(time);
time = setInterval(function () {
if (div1.offsetLeft== target) {
clearInterval(time);
}
else {
div1.style.left = div1.offsetLeft + speed + "px";
}
}, 30);
}
</script>
</head>
<body>
<div id="div1">
<div id="div2">分享</div>
</div>
</body>
</html>
页:
[1]