JavaScript元素拖动不工作
JavaScript element dragging not work
我有一个HTML元素,它通常是可拖动的。在个人文件上,它可以工作,在Codepen上不行。
我不明白问题出在哪里。
我Codepen或JS, CSS和HTML…
var clicEnCours = false;
var position_x = 0;
var position_y = 0;
var origineDiv_x = 0;
var iExplorer = false;
var deplacable = "";
if (document.all){
iExplorer = true;
}
function elempress(pDiv){
chaineX = document.getElementById(pDiv).style.left;
chaineY = document.getElementById(pDiv).style.top;
origineDiv_x = x - chaineX.substr(0,chaineX.length-2);
origineDiv_y = y - chaineY.substr(0,chaineY.length-2);
clicEnCours = true;
deplacable = pDiv;
document.getElementById(deplacable).style.cursor = 'pointer';
document.getElementById(deplacable).style.zIndex = 100;
}
function elemrelache(pDiv){
clicEnCours = false;
document.getElementById(deplacable).style.cursor = 'grab';
document.getElementById(deplacable).style.zIndex = 0;
deplacable = "";
}
function deplacementSouris(e){
x = (iExplorer) ? event.x + document.body.scrollLeft : e.pageX;
y = (iExplorer) ? event.y + document.body.scrollTop : e.pageY;
if (clicEnCours && document.getElementById){
position_x = x - origineDiv_x;
position_y = y - origineDiv_y;
document.getElementById(deplacable).style.left = position_x;
document.getElementById(deplacable).style.top = position_y;
}
}
if(!iExplorer){
document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = deplacementSouris;
@import url(https://fonts.googleapis.com/css?family=Nova+Mono);
#divdrag {
font-family:'Nova Mono';
border-radius:10px;
background:#26A69A;
padding:10px;
width:30%;
}
<div id="divdrag" onmousedown="elempress('divdrag');" onmouseup="elemrelache('divdrag');">Drag me</div>
top
和left
属性需要单位。你只是在给他们分配数字。
document.getElementById(deplacable).style.left = position_x + 'px';
document.getElementById(deplacable).style.top = position_y + 'px';
这两个属性也只适用于元素的位置为relative
或absolute
。
#divdrag {
position:absolute;
}
http://codepen.io/anon/pen/garopb 相关文章:
- 推特Boostrap模式窗口-可拖动不工作
- dropable的Over事件是't工作正常,在可拖动对象被拖放到贪婪的可拖动对象上并再次拖动后
- 为什么我的左拖动/滑块工作,但右拖动/滑块不起作用
- “appendTo”-方法在将可拖动元素连接到 jQueryUI 中的可排序元素时无法正常工作
- JQuery 可排序、可拖动和可拖放不协同工作
- d3.js强制布局拖动在删除节点后停止工作
- 无法通过在d3系列图表上拖动矩形来进行缩放以正常工作
- Ajax X和Y位置更新无法使用Jquery UI可拖动程序正常工作
- 为什么我的javascript拖动调整大小框没有'没有按预期工作
- HTML5将文件从一个框拖动到另一个框.工作不正常
- Cordova (phonegap):在画布内拖动图像在浏览器中工作良好,但在移动设备中不工作
- Jquery可缩放&可拖动同时不工作
- D3拖动不正常工作
- React组件拖动不工作,没有错误或控制台日志
- 阻止对象链接在拖动时工作
- 如何访问拖动的文本(或:如何将文本拖动到输入“工作”中?)
- JavaScript元素拖动不工作
- Jquery dragtable动态添加的列拖动选项不工作
- jQuery UI可拖动Snap恢复不工作
- jQuery可拖动小部件不与li在ul中工作