javascript来个大神帮我封装下函数
答案:3 悬赏:40 手机版
解决时间 2021-04-18 23:18
- 提问者网友:伴风望海
- 2021-04-18 12:50
javascript来个大神帮我封装下函数
最佳答案
- 五星知识达人网友:低血压的长颈鹿
- 2021-04-18 14:01
这不是已经封装好了么-_-
--回复评论--
原来你是想要这样的封装呀,其实也简单,把里面的东东再封装一个函数,传一个id进去就好了:
window.onload=function() {
function draggable(id) {
var d =document.getelementbyid(id)
var disx=0;
var disy=0;
d.onmousedown=mousedown;
function mousedown(ev) {
var oevent=ev||event;
disx=oevent.clientx-d.offsetleft;
disy=oevent.clienty-d.offsettop;
if (d.setcapture) {
d.onmousemove=mousemove;
d.onmouseup=mouseup;
d.setcapture();
} else{
document.onmousemove=mousemove;
document.onmouseup=mouseup;
}
return false;
}
function mousemove(ev) {
var oevent=ev||event;
l=oevent.clientx-disx;
y=oevent.clienty-disy;
d.style.left=l+'px';
d.style.top=y+'px';
};
function mouseup() {
this.onmousemove=null
this.onmouseup=null
if(d.releasecapture) {
d.releasecapture()
};
};
}
draggable('d1');
draggable('d2');
draggable('d3');
draggable('d4');
};或者直接传个DOM进去也行:
function draggable(d) {
//...
}
draggable(document.getElementById('d1'));
draggable(document.getElementById('d2'));不过感觉更麻烦了
--回复评论--
原来你是想要这样的封装呀,其实也简单,把里面的东东再封装一个函数,传一个id进去就好了:
window.onload=function() {
function draggable(id) {
var d =document.getelementbyid(id)
var disx=0;
var disy=0;
d.onmousedown=mousedown;
function mousedown(ev) {
var oevent=ev||event;
disx=oevent.clientx-d.offsetleft;
disy=oevent.clienty-d.offsettop;
if (d.setcapture) {
d.onmousemove=mousemove;
d.onmouseup=mouseup;
d.setcapture();
} else{
document.onmousemove=mousemove;
document.onmouseup=mouseup;
}
return false;
}
function mousemove(ev) {
var oevent=ev||event;
l=oevent.clientx-disx;
y=oevent.clienty-disy;
d.style.left=l+'px';
d.style.top=y+'px';
};
function mouseup() {
this.onmousemove=null
this.onmouseup=null
if(d.releasecapture) {
d.releasecapture()
};
};
}
draggable('d1');
draggable('d2');
draggable('d3');
draggable('d4');
};或者直接传个DOM进去也行:
function draggable(d) {
//...
}
draggable(document.getElementById('d1'));
draggable(document.getElementById('d2'));不过感觉更麻烦了
全部回答
- 1楼网友:玩家
- 2021-04-18 15:27
window.onload = function() {
var disX = 0;
var disY = 0;
d1.onmousedown = function(ev) {
ev = ev || event;
disX = ev.clientX - d1.offsetLeft;
disY = ev.clientY - d1.offsetTop;
if (d1.setCapture) {
d1.onmousemove = mouseMove;
d1.onmouseup = mouseUp;
d1.setCapture();
} else {
document.onmousemove = function(ev) {
ev = ev || event;
l = ev.clientX - disX;
y = ev.clientY - disY;
d1.style.left = l + 'px';
d1.style.top = y + 'px';
};
document.onmouseup = function() {
this.onmousemove = null;
this.onmouseup = null;
if (d1.releaseCapture) {
d1.releaseCapture();
}
};
}
return false;
};
};追问不能有d1啊大神,把d1用属性传进去啊,那样能多次使用,这样只能d1专用啊,我是想弄多个物体移动,不是单个啊.
var disX = 0;
var disY = 0;
d1.onmousedown = function(ev) {
ev = ev || event;
disX = ev.clientX - d1.offsetLeft;
disY = ev.clientY - d1.offsetTop;
if (d1.setCapture) {
d1.onmousemove = mouseMove;
d1.onmouseup = mouseUp;
d1.setCapture();
} else {
document.onmousemove = function(ev) {
ev = ev || event;
l = ev.clientX - disX;
y = ev.clientY - disY;
d1.style.left = l + 'px';
d1.style.top = y + 'px';
};
document.onmouseup = function() {
this.onmousemove = null;
this.onmouseup = null;
if (d1.releaseCapture) {
d1.releaseCapture();
}
};
}
return false;
};
};追问不能有d1啊大神,把d1用属性传进去啊,那样能多次使用,这样只能d1专用啊,我是想弄多个物体移动,不是单个啊.
- 2楼网友:往事隔山水
- 2021-04-18 14:37
按照你的思路函数是这样的function $(id){ return document.getElementById(id)}function ad(){ return parseInt($('ab1').innerHTML) * parseInt($('cd1').innerHTML) + parseInt($('ab2').innerHTML) * parseInt($('cd2').innerHTML) + parseInt($('ab3').innerHTML) * parseInt($('cd3').innerHTML)}这种方法比较简单易懂如果有规律的话,可自行封装
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯