【发布时间】:2017-08-17 11:08:30
【问题描述】:
我正在为我支持的项目制作可拖动的 div 元素。我发现这个AngularJS directive 允许一个元素是可拖动的。
我修改了指令以更改起始位置,但是当我进行更改时,鼠标方向没有反转(即向右移动鼠标,div 向左移动)。这是AngularJS directive 的更新版本,其中包含以下更改以展示我遇到的问题。
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
});
app.directive('ngDraggable', function($document, $window){
function makeDraggable(scope, element, attr) {
var startX = 0;
var startY = 0;
var x = 20;
var y = 20;
// Start with a random pos
// var x = Math.floor((Math.random() * 500) + 40);
// var y = Math.floor((Math.random() * 360) + 40);
element.css({
position: 'absolute',
cursor: 'pointer',
bottom: y + 'px',
right: x + 'px'
});
element.on('mousedown', function(event) {
event.preventDefault();
startX = event.pageX - x;
startY = event.pageY - y;
$document.on('mousemove', mousemove);
$document.on('mouseup', mouseup);
});
function mousemove(event) {
y = event.pageY - startY;
x = event.pageX - startX;
element.css({
bottom: y + 'px',
right: x + 'px'
});
}
function mouseup() {
$document.unbind('mousemove', mousemove);
$document.unbind('mouseup', mouseup);
}
}
return {
link: makeDraggable
};
});
【问题讨论】:
标签: javascript css angularjs ngdraggable