js中定義了一個(gè)move(first,modify)方法,需要傳入first,modify。因?yàn)槲蚁胫貜?fù)使用move函數(shù)可是有的地方不需要modify,有的地方需要,傳空值的話也不行,無(wú)法運(yùn)行。有什么方法能不需要傳入固定個(gè)數(shù)的參數(shù)運(yùn)行嘛。調(diào)用function moveTest() { var first = document.getElementById(id); var modify = { y: function () { statements } }; move(first, modify); //這里一定要兩個(gè)參數(shù)不然運(yùn)行不了。}方法 function move(first, modify) { first.onmousedown = function (e) { //把onclick改成onmouseover就是一獲得焦點(diǎn)圖片就跟隨鼠標(biāo)移動(dòng),onmousedown鼠標(biāo)拖動(dòng)效果 modify.y(); //方法 var x = e.clientX - first.offsetLeft; var y = e.clientY - first.offsetTop; document.onmousemove = function (e) { var left = e.clientX - x; var top = e.clientY - y; first.style.left = left + 'px'; first.style.top = top + 'px'; document.onmouseup = function () { //鼠標(biāo)松開(kāi)時(shí)釋放函數(shù),如果需要拖至指定位置可在此添加運(yùn)算邏輯; document.onmousemove = null; document.onmouseup = null; } } },
js構(gòu)造了一個(gè)函數(shù),內(nèi)部有兩個(gè)變量,我調(diào)用的時(shí)候可以只傳一個(gè)進(jìn)去嗎。
慕碼人8056858
2019-03-07 14:13:21