1.匀速直线运动
原理详解
var oDiv = document.getElementsByTagName('div')[0];
var oBtn = document.getElementsByTd('btn');
var timer = null;
oBtn.onclick = function(){
clearInterval(timer);
var iSpeed = 300 - oDiv.offsetLeft > 0 ? 7 : - 7;
timer = setInterval(function (){
if(Math.abs(300 - oDiv.offsetLeft) < Math.abs(iSpeed)){
clearInterval(timer);
oDiv.style.left = '300px';
}else{
oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';
}
}, 30);
实现封装
function startMove(dom, target){
clearInterval(timer);
var iSpeed = target - oDiv.offsetLeft > 0 ? 7 : - 7;
timer = setInterval(function (){
if(Math.abs(target - dom.offsetLeft) < Math.abs(iSpeed)){
clearInterval(timer);
dom.style.left = target + 'px';
}else{
dom.style.left = dom.offsetLeft + iSpeed + 'px';
}
}, 30);
}
2.缓冲运动
物体的速度,距离目标点越近,就越慢,当到达目标点时,停止
实现封装
function startMove(dom, target){
clearInterval(timer);
var iSpeed = null;
timer = setInterval(function(){
iSpeed = (target - oDiv.offsetLeft) / 7;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(oDiv.offsetLeft == target) {
clearInterval(timer);
}else{
oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';
}
}, 30);
}
应用demo
页面侧面伸缩栏,鼠标移上去后,菜单弹出来,鼠标移走,菜单缩回去
<style>
.wrapper {
width: 400px;
height: 80px;
background: orange;
position: absolute;
left: -400px;
top: 200px;
}
.wrapper span {
width: 50px;
height: 80px;
background: red;
position: absolute;
right: -50px;
top: 0px;
}
</style>
<div class="wrapper">
<span></span>
</div>
<script>
var timer = null;
var oDiv = document.getElementsByClassName('wrapper')[0];
oDiv.onmouseenter = function (){
startMove(this, 0);
}
oDiv.onmouseleave = function (){
startMove(this, -400);
}
function startMove(dom, target){
clearInterval(timer);
var iSpeed = null;
timer = setInterval(function(){
iSpeed = (target - oDiv.offsetLeft) / 7;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(oDiv.offsetLeft == target) {
clearInterval(timer);
}else{
oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';
}
}, 30);
}
</script>
多物体运动
<style>
div {
width: 100px;
height: 100px;
background: red;
opacity: 1;
margin-bottom: 100px;
}
</style>
<div></div>
<div></div>
<div></div>
<script>
var timer = null;
var oDivArray = document.getElementsByTagName('div');
for (var i = 0; i < oDivArray.length; i++) {
oDivArray[i].onmouseenter = function () {
startMove(this, 400);
}
oDivArray[i].onmouseleave = function () {
startMove(this, 100);
}
}
function getStyle(dom, attr){
if(window.getComputedStyle){
return window.getComputedStyle(dom, null)[attr];
}else{
return dom.currentStyle[attr];
}
}
function startMove (dom, target) {
clearInterval(dom.timer);
var iSpeed = null,
iCur = null;
dom.timer = setInterval(function () {
iCur = parseInt( getStyle(dom, 'width') );
iSpeed = (target - iCur ) / 7;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if (iCur == target) {
clearInterval(dom.timer);
}else{
dom.style.width = iCur + iSpeed + 'px';
}
}, 30);
}
function startMove (dom, attr, target) {
clearInterval(dom.timer);
var iSpeed = null,
iCur = null;
dom.timer = setInterval(function () {
if (attr == 'opcity') {
iCur = parseFloat( getStyle(dom, attr) ) * 100;
}else {
iCur = parseInt( getStyle(dom, attr) );
}
iSpeed = (target - iCur) / 7;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if (iCur == target) {
clearInterval(dom.timer);
}
if (attr == 'opacity') {
dom.style.opacity = ( iCur + iSpeed ) / 100;
}else {
dom.style[attr] = iCur + iSpeed + 'px';
}
}, 30);
}
</script>
多物体多值运动封装版
<style>
div {
position: absolute;
left: 0px;
width: 100px;
height: 100px;
background: red;
opacity: 1;
}
#topDiv {
top: 200px;
}
#bottomDiv {
top: 400px;
}
</style>
<div id='topDiv'></div>
<div id='bottomDiv'></div>
<script>
var oTopDiv = document.getElemensById('topDiv');
var oBottomDiv = document.getElementsById('bottomDiv');
oTopDiv.onclick = function () {
startMove(this, {width: 400, height: 400, left: 200, top: 300, opacity: 50}, function () {
startMove(oBottomDiv, {width: 400, height: 400, left: 200, top: 300, opacity: 50}, function () {
alert('over');
})
})
}
function getStyle (dom, attr) {
if (window.getComputedStyle) {
return window.getComputedStyle(dom, null)[attr];
}else {
return dom.currentStyle[attr];
}
}
function startMove (dom, attrObj, callback) {
clearInterval(dom.timer);
var iSpeed = null,
iCur = null;
dom.timer = setInterval(function () {
var bStop = ture;
for (var attr in attrObj) {
if (attr == 'opacity') {
iCur = parseFloat( getStyle(dom, attr) ) * 100;
}else {
iCur = parseInt( getStyle(dom, attr) );
}
iSpeed = (attrObj[attr] - iCur) / 7;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if (attr == 'opacity') {
dom.style.opacity = (iCur + iSpeed + 'px');
}
if(iCur != attrObj[attr]) {
bStop = false;
}
}
if (bStop) {
calerInterval(dom.timer);
typeof callback == 'function' && callback();
}
})
}
</script>
|