【发布时间】:2017-01-20 01:24:02
【问题描述】:
我正在使用以下代码移动一个 div,但是在 jsfiddle 中它很流畅,在我的代码中它很跳跃,体验不是最好的。
我想知道我是否在这里跳过了一些东西,然后是关于是否有任何特定的编码方式的问题。比如只能用css做吗?
https://jsfiddle.net/59sxn1jy/2/
html
<div id="panel_log"></div>
css
#panel_log {
position:absolute;
width:100px;
height:100px;
border:1px solid #000;
background-color:#fff;
top:20px;
left:20px;
cursor:move;
}
javsascript
var spaceY;
var spaceX;
var move_start = function(e) {
var panel_log = document.getElementById('panel_log');
spaceY = parseInt(window.getComputedStyle(panel_log,null).getPropertyValue('top')) - parseInt(e.clientY);
spaceX = parseInt(window.getComputedStyle(panel_log,null).getPropertyValue('left')) - parseInt(e.clientX);
window.addEventListener('mousemove',moving,false);
window.addEventListener('mouseup',finishing,false);
};
var moving = function(e) {
var panel_log = document.getElementById('panel_log');
panel_log.style.top = parseInt(e.clientY) + spaceY + 'px';
panel_log.style.left = parseInt(e.clientX) + spaceX + 'px';
};
var finishing = function(e) {
var panel_log = document.getElementById('panel_log');
window.removeEventListener('mousemove', moving, false);
window.removeEventListener('mouseup', finishing, false);
};
document.getElementById('panel_log').addEventListener('mousedown',move_start,false);
【问题讨论】:
标签: javascript html drag-and-drop draggable