拖拽事件--select外边框拖拽

地图上面的搜索框要可拖拽
但是搜索框是有点击事件的,点击显隐下拉菜单,如果拖拽的事件源选择select框的话,会有样式(十字拖动符cursor:move与selelt默认点击的箭头)冲突
思索良久,就用外边距区域做事件源啊,废话不多,看代码。。。。
1、html页面放置的select搜索框(注意看哦,重点是h6标签哦)

2、以下是样式 #searchbox { border:1px solid #fff; position:absolute; left:50px; top:50px; } #searchbox select{ width:350px; height: 40px; } #searchbox h6.search-top { position:absolute; top:-6px; left:0; width:100%; height:6px; line-height:6px; color:white; cursor:move; } #searchbox h6.search-bottom { position:absolute; bottom:-6px; left:0; width:100%; height:6px; line-height:6px; color:white; cursor:move; } #searchbox h6.search-left { position:absolute; bottom:0; left:-6px; width:6px; height:100%; color:white; cursor:move; } #searchbox h6.search-right { position:absolute; bottom:0; right:-6px; width:6px; height:100%; color:white; cursor:move; } 3、以下是js方法哦(方法有参考某个童鞋的,时间久忘记名字了,此处不明示了,理解一下哦) function drag(obj) { if (typeof obj === 'string') { var obj = document.getElementById(obj); } else { var obj = obj; } function fixEvent(event) { event.target = event.srcElement; event.preventDefault = fixEvent.preventDefault; return event; } fixEvent.preventDefault = function () { this.returnValue = false; }; obj.onmousedown = mousedown; function mousedown(e) { var e = e || fixEvent(window.event); var disX = e.clientX - obj.offsetLeft; var disY = e.clientY - obj.offsetTop; if (e.target.tagName === 'H6') { document.onmousemove = move; document.onmouseup = up; } else { document.onmousemove = null; document.onmouseup = null; } function move(e) { var e = e || fixEvent(window.event); var left = e.clientX - disX; var top = e.clientY - disY; if (obj.setCapture) { obj.setCapture(); } if (left < 0) { left = 0; } else if (left > document.documentElement.clientWidth - obj.offsetWidth) { left = document.documentElement.clientWidth - obj.offsetWidth; } if (top < 0) { top = 0; } else if (top > document.documentElement.clientHeight - obj.offsetHeight) { top = document.documentElement.clientHeight - obj.offsetHeight; } obj.style.left = left + 'px'; obj.style.top = top + 'px'; return false; }; function up() { if (obj.releaseCapture) { obj.releaseCapture(); } document.onmousemove = null; document.onmouseup = null; } }; } 4、然后是js页面调用方法 window.onload = function () { var trigger = document.getElementById('searchbox'); drag(trigger); }; 综上:此拖拽事件已搞定,如有问题留言探讨,转载请注明出处。
posted @ 2017-04-24 15:35  飞天龙猫  阅读(631)  评论(0编辑  收藏  举报