溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用js寫的函數(shù)有哪些

發(fā)布時間:2021-10-21 13:56:37 來源:億速云 閱讀:101 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“使用js寫的函數(shù)有哪些”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“使用js寫的函數(shù)有哪些”這篇文章吧。

/*

my js的庫

*/

//隨機(jī)產(chǎn)生n個從x-y中不重復(fù)的整數(shù)

function check(n,x,y){

var num=[];

for(var i=0;i<n;i++){

num[i]=Math.ceil(Math.random()*(y-x)+x);

for(var j=0;j<num.length-1;j++){

if(num[j]===num[++j]){

num.splice(j,1);

n++;

}

}

}

//alert(num.length);

return num;

}

//返回一個X-Y之間的隨機(jī)數(shù)

function stochastic(x,y){

//四舍五入和0-1之間的隨機(jī)數(shù)

return Math.round(Math.random()*(y-x)+x);

}

//需要檢測的標(biāo)簽對象-->檢測標(biāo)簽中的內(nèi)容是否全部都是數(shù)字(僅對與input標(biāo)簽)

function check(obj){

var text = obj.value;

//var judge=true;

for(var i=0;i<text.length;i++){

//alert(text.charCodeAt(i)>57);

if(text.charCodeAt(i)>57||text.charCodeAt(i)<48){

//judge=false;

return false;

}

}

/*

if(panduan==false){

alert("這不是一串?dāng)?shù)字");

}*/

//改進(jìn)-->簡單明了

//return judge==false? false: true;

return true;

}

//透明函數(shù),對象,速度,目標(biāo)點,改變頻率,回調(diào)函數(shù)

function diaphaneity(obj,speed,target,rate,enfn){

var setTime =null;

var target=target;

speed = parseFloat(getStyle(obj,'opacity'))*100<target?speed:-speed;

setTime=setInterval(function(){

//console.log(parseFloat(getStyle(obj,'opacity'))*100);

var extent=parseFloat(getStyle(obj,'opacity'))*100+speed;//改變后的透明度

obj.style.opacity=extent*0.01+"";

if(parseFloat(getStyle(obj,'opacity'))*100===target){

obj.style.opacity=target*0.01+"";

clearInterval(setTime);

enfn&&enfn();

}

},rate);

}

//需要抖動的對象,抖動的頻率,幀數(shù),方向,初始位置

function shake(obj,rate,frame,att,size){

var arr=[];

var setTime=null;

for(var i=rate;i>0;i-=frame){

arr.push(i,-i);

}

arr.push(0);

var index=0;

var setdu=parseInt(size);//有問題的,待解決。

console.log(setdu);

setTime=setInterval(function(){

if(index>=arr.length){

index=0;

clearInterval(setTime);

setTime=null;

}else{

setdu=setdu+arr[index];

obj.style[att]=setdu+"px";

index++;

}

},50);

}

//讓層上下左右移動 ,參數(shù)   標(biāo)簽對象,上/下/左/右,終點距離,回調(diào)函數(shù) 

function doMove ( obj, attr, dir, target, endFn ) {

var target=target;

dir = parseInt(getStyle( obj, attr )) < target ? dir : -dir;

clearInterval( obj.timer );

obj.timer = setInterval(function () {

var speed = parseInt(getStyle( obj, attr )) + dir;// 步長

if ( speed > target && dir > 0 ||  speed < target && dir < 0  ) {

speed = target;

}

obj.style[attr] = speed + 'px';

if ( speed == target) {

clearInterval( obj.timer );

/*

if ( endFn ) {

endFn();

}

*/

endFn && endFn();

}

}, 30);

}

//獲取樣式的值

function getStyle ( obj, attr ) { return obj.currentStyle?obj.currentStyle[attr] : getComputedStyle( obj )[attr]; }

以上是“使用js寫的函數(shù)有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI