//The host is the browser//Pass the reference of the corresponding element object to the function function candrag(drager) { drager.onmousedown = function (down) { var offx = drager.offsetLeft var offy = drager.offsetTop; var offxl = down.clientX - offx; var offyl = down.clientY - offy; window.condition = 0;//Added condition attribute for window to resolve the contradiction between click document.onmousemove = function (move) { drager.style.left = move.clientX - offxl + "px"; dragger.style.top = move.clientY - offyl + "px"; dragger.style.cursor = "move"; condition = Math.abs(move.clientX - down.clientX) + Math.abs(move.clientY - down.clientY); } } dragger.onmouseup = function () { document.onmousemove = null; draggerr.style.cursor = "auto"; } } /*For the resolution of the contradiction between click, it is necessary to judge the condition *For example: candrag(dragger); d01.onclick = function () { if (!condition) { d01.style.backgroundColor = "red"; } } *where d01 is a child element of dragger*/