Javascript漂流記(下)_jsスポーツ編
7594 ワード
一、均等運動
function startMove(dom, destination){
clearInterval(timer);
var iSpeed = destination - dom.offsetLeft > 0 ? 5 : -5;
timer = setInterval(function(){
if( Math.abs(destination - dom.offsetLeft) < Math.abs(iSpeed) ){
dom.style.left = '500px';
clearInterval(timer);
}else{
dom.style.left = dom.offsetLeft + iSpeed + 'px';
}
},30);
}
二、均等加速運動 function startMove(dom){
clearInterval(timer);
var a = 2;
var iSpeed = 10;
timer = setInterval(function(){
iSpeed = iSpeed + a;
dom.style.left = dom.offsetLeft + iSpeed + 'px';
},30)
}
三、緩衝運動 function startMove(dom, destination){
clearInterval(timer);
timer = setInterval(function(){
var iSpeed = (destination - dom.offsetLeft) / 11;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(dom.offsetLeft == destination){
clearInterval(timer);
}else{
dom.style.left = dom.offsetLeft + iSpeed + 'px';
}
},30)
}
四、弾性運動(加速度変化の運動) function startMove(dom, target){
clearInterval(timer);
var a;
var iSpeed = 0;
var u = 0.8;
timer = setInterval(function(){
a = (target - dom.offsetLeft) / 10;
iSpeed += a;
iSpeed *= u;
if(Math.abs(iSpeed < 1) && Math.abs(target - dom.offsetLeft) < 1){
clearInterval(timer);
dom.style.left = target + 'px';
}else{
dom.style.left = dom.offsetLeft + iSpeed + 'px';
}
},30);
}
五、アナログ重力場 function startMove(dom){
clearInterval(dom.timer);
var iSpeedX = 6,
iSpeedY = 6,
g = 2;
dom.timer = setInterval(function(){
iSpeedY += g;
var newTop = dom.offsetTop + iSpeedY;
var newLeft = dom.offsetLeft + iSpeedX;
if(newTop >= document.documentElement.clientHeight - dom.clientHeight){
iSpeedY *= -1;
iSpeedY *= 0.8;
iSpeedX *= 0.9;
newTop = document.documentElement.clientHeight - dom.clientHeight;
}
if(newTop <= 0){
iSpeedY *= -1;
iSpeedY *= 0.8;
iSpeedX *= 0.9;
newTop = 0;
}
if(newLeft >= document.documentElement.clientWidth - dom.clientWidth){
iSpeedX *= -1;
iSpeedY *= 0.8;
iSpeedX *= 0.9;
newLeft = document.documentElement.clientWidth - dom.clientWidth;
}
if(newLeft <= 0){
iSpeedX *= -1;
iSpeedY *= 0.8;
iSpeedX *= 0.9;
newLeft = 0;
}
if(Math.abs(iSpeedX) < 1){iSpeedX = 0;}
if(Math.abs(iSpeedY) < 1){iSpeedY = 0;}
if(iSpeedX == 0 && iSpeedY == 0 && newTop == document.documentElement.clientHeight - dom.clientHeight){
clearInterval(dom.timer);
alert('gameover');
}else{
dom.style.top = newTop + 'px';
dom.style.left = newLeft + 'px';
}
},30)
}
六、多物体運動 function getStyle(dom, attr){
if(window.getComputedStyle){
return window.getComputedStyle(dom, null)[attr];
}else{
return dom.currentStyle[attr];
}
}
function startMove(dom, destination){
clearInterval(dom.timer);
var iSpeed = null, iCur = null;
dom.timer = setInterval(function(){
iCur = parseInt(getStyle(dom, 'width'));
iSpeed = (destination - iCur) / 11;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(iCur == destination){
clearInterval(dom.timer);
}else{
dom.style.width = iCur + iSpeed + 'px';
}
},30)
}
七、多物体の異なる値の運動 function getStyle(dom, attr){
if(window.getComputedStyle){
return window.getComputedStyle(dom, null)[attr];
}else{
return dom.currentStyle[attr];
}
}
function startMove(dom, attr, destination){
clearInterval(dom.timer);
var iSpeed = null, iCur = null;
dom.timer = setInterval(function(){
if(attr == 'opacity'){
iCur = parseFloat(getStyle(dom, attr)) * 100;
}else{
iCur = parseInt(getStyle(dom, attr));
}
iSpeed = (destination - iCur) / 10;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(iCur == destination){
clearInterval(dom.timer);
}
if(attr == 'opacity'){
dom.style.opacity = (iCur + iSpeed) / 100;
}else{
dom.style[attr] = iCur + iSpeed + 'px';
}
},30)
}
八、多物体多値運動帯の調整メカニズム 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 = true;
for(var attr in attrObj){
if(attr == 'opacity'){
iCur = parseFloat(getStyle(dom, attr)) * 100;
}else{
iCur = parseInt(getStyle(dom, attr));
}
iSpeed = (attrObj[attr] - iCur) / 10;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(attr == 'opacity'){
dom.style.opacity = (iCur + iSpeed) / 100;
}else{
dom.style[attr] = iCur + iSpeed + 'px';
}
if(iCur != attrObj[attr]){
bStop = false;
}
}
if(bStop){
clearInterval(dom.timer);
typeof callback == 'function' && callback();
}
},30);
}
以上の内容は次兄のオリジナルで、「渡一教育Javascript課程」から整理しました.おすすめの「渡一教育」です.