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

javascript运动功能-分享到

2015-10-08 16:57 253 查看
<script>
//窗体载入,为div控件绑定事件
window.onload = function () {

var div1 = document.getElementById('div1');
//mouseover
div1.onmouseover = function () {
startMove(0);
}
//mouseout
div1.onmouseout = function () {

startMove(-100);
}
}
//定义定时器
var timer = null;
//淡入和淡出
function startMove(targetPoint) {

clearInterval(timer);
var div1 = document.getElementById('div1');

timer = setInterval(function () {

var ispeed = 0;
if (div1.offsetLeft < targetPoint) {

ispeed = 10;
}
else {

ispeed = -10;
}
//控件位置
if (div1.offsetLeft == targetPoint) {
clearInterval(timer);
}
else {

div1.style.left = div1.offsetLeft + ispeed + "px";
}
},30);

}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: