The basic idea of drag and drop was also discussed in the previous article. Now that we have the theory, what about practice? Where can it be applied? Below I will bring you a small example written using drag-and-drop thinking for your reference. The approximate effect can be seen in the picture below:
It is such a simple drag bar. You can understand it as a scroll bar, a quantity selection control in a form, a progress bar, etc. Anyway, you can change it to many Without further ado, let’s see how this is done!
After thinking about it, I feel that there is no need to explain the principle. I have already made it very clear in the drag effect. Students who are not sure can go out and turn left to see a small example of javascript and drag and drop on a PC web page. I will post the code directly:
css:
<style> #drag_wrap{ width:220px; height:10px; position:relative; margin:100px auto; } .dis_bg{ width:200px; height:10px; border-radius:10px; background:#ccc; margin-left:10px; } #drag_bg{ width:0; height:10px; border-radius:10px; background:#0CF; } #drag_box{ width:20px; height:20px; border-radius:10px; background:#F30; position:absolute; top:-5px; left:0; cursor:move; } #drag_box span{ width:40px; height:20px; text-align:center; line-height:20px; border:1px solid #ccc; position:absolute; top:-25px; left:-10px; color:#333; background:#fff; } #drag_wrap1{ width:10px; height:220px; position:relative; margin:100px auto; } .dis_bg1{ width:10px; height:200px; border-radius:10px; background:#ccc; position:absolute; top:10px; } #drag_bg1{ width:10px; height:0; border-radius:10px; background:#0CF; } #drag_bg1{ width:10px; height:0; border-radius:10px; background:#0CF; } #drag_box1{ width:20px; height:20px; border-radius:10px; background:#F30; position:absolute; top:-5px; left:-5px; cursor:move; } #drag_box1 span{ width:40px; height:20px; text-align:center; line-height:20px; border:1px solid #ccc; position:absolute; top:0; left:25px; color:#333; background:#fff; } </style>
html:
<div id="drag_wrap"> <div class="dis_bg"> <div id="drag_bg"></div> </div> <div id="drag_box"><span>0</span></div> </div> <div id="drag_wrap1"> <div class="dis_bg1"> <div id="drag_bg1"></div> </div> <div id="drag_box1"><span>0</span></div> </div>
JavaScript:
window.onload = function(){ drag("drag_box","drag_wrap","drag_bg","left"); drag("drag_box1","drag_wrap1","drag_bg1","top"); function drag(obj,parentNode,bgObj,attr,endFn){ var obj = document.getElementById(obj); var parentNode = document.getElementById(parentNode); var bgObj = document.getElementById(bgObj); var oNum = obj.getElementsByTagName('span')[0]; obj.onmousedown = function(ev){ var ev = ev || event; //非标准设置全局捕获(IE) if(obj.setCapture){ obj.setCapture() }; var disX = ev.clientX - this.offsetLeft, disY = ev.clientY - this.offsetTop; var oWidth = obj.offsetWidth, oHeight = obj.offsetHeight; var pWidth = parentNode.offsetWidth, pHeight = parentNode.offsetHeight; document.onmousemove = function(ev){ var ev = ev || event; if(attr == "left"){ //横向 var left = ev.clientX - disX; //左侧 if(left <= 0){ left = 0; }else if(left > pWidth - oWidth){//右侧 left = pWidth - oWidth; }; obj.style.left = bgObj.style.width = left + 'px'; oNum.innerHTML = left; }else if(attr == "top"){ //竖向 var top = ev.clientY - disY; //上面 if(top <= 0){ top = 0; }else if(top > pHeight - oHeight){//下面 top = pHeight - oHeight; }; obj.style.top = bgObj.style.height = top + 'px'; oNum.innerHTML = top; }; }; document.onmouseup = function(ev){ var ev = ev || event; document.onmousemove = document.onmouseup = null; endFn && endFn(); //非标准释放全局捕获(IE) if(obj.releaseCapture){ obj.releaseCapture() }; }; return false; }; } }
Parameter description:
Five parameters are given here, obj, parentNode, bgObj, attr, endFn, respectively:
obj: the dragged object
parentNode: an object that limits the active area of the dragged object, usually set to its parent
bgObj: color background object when dragging
attr: 2 parameters left, top, indicating whether to drag horizontally or vertically
endFn: Return function, it will be executed if it exists, otherwise it will not be executed, optional
The above is the entire content of this article, I hope it will be helpful to everyone’s study.