国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

原生javascript運動函數的封裝示例【勻速、拋物線、多屬性的運動

2024-05-06 15:44:46
字體:
來源:轉載
供稿:網友

本文實例講述了原生javascript運動函數的封裝。分享給大家供大家參考,具體如下:

//封裝勻速運動//參數:// 1、dom對象// 2、樣式屬性(top,left,width,height,opacity等等)// 3、起始位置,結束位置// 4、速度:時間間隔,步長// 5、方向://返回值function moveObj(domObj,attr,startValue,endValue,timeSpace,step,direction) { let currValue = startValue; let myTimer = setInterval(function(){ //1、改變數據 currValue = currValue+direction*step; //2、判斷邊界 // if(currValue>=endValue){//?? // currValue = endValue;//?? // window.clearInterval(myTimer); // } if(Math.abs(currValue-endValue)<step){  currValue = endValue;  window.clearInterval(myTimer); } //3、改變外觀 if(attr=="opacity"){  domObj.style[attr] = currValue; }else{  domObj.style[attr] = currValue+"px"; } },timeSpace);}//封裝勻速運動//參數:// 1、dom對象// 2、樣式屬性(top,left,width,height,opacity等等)// 3、結束位置// 4、時長://返回值function moveObj02(domObj,attr,endValue,timeLong) { let startValue = parseFloat(getStyle(domObj,attr));//?? let direction= endValue-startValue>0?1:-1;//?? let timeSpace = 10; let step = Math.abs(endValue-startValue)/(timeLong/timeSpace) // endValue-startValue/步子數;// let currValue = startValue; let myTimer = setInterval(function(){ //1、改變數據 currValue = currValue+direction*step; //2、判斷邊界 if(Math.abs(currValue-endValue)<step){  currValue = endValue;  window.clearInterval(myTimer); } //3、改變外觀 if(attr=="opacity"){  domObj.style[attr] = currValue; }else{  domObj.style[attr] = currValue+"px"; } },timeSpace);}//封裝拋物線運動(右開口為例)//參數:// dom對象// 起點// 終點// 總時長////返回值:無function parabola(domObj,startPoint,endPoint,timeLong,func){ //一、初始化 let offsetX = endPoint.x-startPoint.x; let offsetY = endPoint.y-startPoint.y; let p = (offsetY*offsetY)/(2*offsetX); let left1 = 0; domObj.style.left = startPoint.x+"px"; domObj.style.top = startPoint.y+"px"; let timeSpace = 10; let step = Math.abs(endPoint.x-startPoint.x)/(timeLong/timeSpace) // endValue-startValue/步子數;// //二、啟動定時器 let myTimer = setInterval(function(){ //1、改變數據 left1=left1+step; let top1=Math.sqrt(2*p*left1); //2、判斷邊界 if(left1>=offsetX){  left1 = offsetX;  top1=Math.sqrt(2*p*left1);  window.clearInterval(myTimer);  if(func){  func();  } } //3、改變外觀 domObj.style.left = left1+startPoint.x+"px"; domObj.style.top = top1+startPoint.y+"px"; },timeSpace);}//淡入://參數:// dom對象// 時長;//返回值:無function fadeIn(domObj,timeLong){ domObj.style.opacity = 0; moveObj02(domObj,"opacity",1,timeLong);}//淡出://參數:// dom對象// 時長;//返回值:無function fadeOut(domObj,timeLong){ domObj.style.opacity = 1; moveObj02(domObj,"opacity",0,timeLong);}//淡入和淡出://參數:// domObjIn:淡入的dom對象// domObjOut:淡出的dom對象// 時長;//返回值:無function fadeInOut(domObjIn,domObjOut,timeLong,func){ domObjIn.style.opacity = 0; domObjOut.style.opacity = 1; let startValue = 0; let endValue = 1; let direction= 1; let timeSpace = 10; let step = Math.abs(endValue-startValue)/(timeLong/timeSpace) // endValue-startValue/步子數;// let currValue = startValue; let myTimer = setInterval(function(){ //1、改變數據 currValue = currValue+direction*step; //2、判斷邊界 if(Math.abs(currValue-endValue)<step){  currValue = endValue;  window.clearInterval(myTimer);  func&&func(); } //3、改變外觀 domObjIn.style.opacity = currValue; domObjOut.style.opacity = 1-currValue; },timeSpace);}//多屬性的運動//參數:// 1、dom對象// 2、每個樣式屬性的結束值// 4、時長://返回值//調用示例:/*animate($("box"),{ "width":600, "height":400, "left":50},2000)*/function animate(domObj,endObj,timeLong) { // let startValue = parseFloat(getStyle(domObj,attr));//?? let startObj = {} for(let key in endObj){ startObj[key] = parseFloat(getStyle(domObj,key)); } // let direction= endValue-startValue>0?1:-1;//?? let directionObj = {}; for(let key in endObj){ directionObj[key] = endObj[key]>startObj[key]?1:-1; } let timeSpace = 10; // let step = Math.abs(endValue-startValue)/(timeLong/timeSpace) // endValue-startValue/步子數;// let stepObj = {}; for(let key in endObj){ stepObj[key] = Math.abs(endObj[key]-startObj[key] )/(timeLong/timeSpace); } //let currValue = startValue; let currObj = {}; for(let key in endObj){ currObj[key] = startObj[key]; } let myTimer = setInterval(function(){ //1、改變數據 //currValue = currValue+direction*step; for(let key in endObj){  currObj[key] = currObj[key]+directionObj[key]*stepObj[key]; } //2、判斷邊界 let firstKey = Object.keys(endObj)[0]; if(Math.abs(currObj[firstKey]-endObj[firstKey])<stepObj[firstKey]){  for(let key in endObj){  currObj[key] = endObj[key];  }  window.clearInterval(myTimer); } //3、改變外觀 for(let key in endObj){  if(key=="opacity"){  domObj.style[key] = currObj[key];  }else{  domObj.style[key] = currObj[key]+"px";  } } },timeSpace);}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 乐陵市| 临猗县| 蒙阴县| 新巴尔虎右旗| 织金县| 循化| 龙南县| 苏尼特左旗| 舞钢市| 合川市| 华坪县| 灯塔市| 方山县| 华安县| 文水县| 神池县| 高碑店市| 玉屏| 佳木斯市| 岳普湖县| 宝鸡市| 丹棱县| 澄江县| 晋州市| 新泰市| 民乐县| 柞水县| 石林| 荆门市| 黄平县| 思茅市| 元江| 来安县| 泽库县| 马尔康县| 洪雅县| 珠海市| 泸水县| 鲁山县| 阿瓦提县| 大港区|