Skip to content

Commit 1c7847b

Browse files
committed
#1599: Support for transforms on ghost element
1 parent 4f17fb0 commit 1c7847b

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Demo: http://sortablejs.github.io/Sortable/
1616
* Advanced swap detection
1717
* Smooth animations
1818
* [Multi-drag](https://github.com/SortableJS/Sortable/tree/master/plugins/MultiDrag) support
19+
* Support for CSS transforms
1920
* Built using native HTML5 drag and drop API
2021
* Supports
2122
* [Meteor](https://github.com/SortableJS/meteor-sortablejs)

src/Sortable.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ let dragEl,
120120

121121
tapEvt,
122122
touchEvt,
123+
lastDx,
124+
lastDy,
125+
tapDistanceLeft,
126+
tapDistanceTop,
123127

124128
moved,
125129

@@ -548,6 +552,7 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
548552
el = _this.el,
549553
options = _this.options,
550554
ownerDocument = el.ownerDocument,
555+
dragRect = getRect(target),
551556
dragStartFn;
552557

553558
if (target && !dragEl && (target.parentNode === el)) {
@@ -566,6 +571,9 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
566571
clientY: (touch || evt).clientY
567572
};
568573

574+
tapDistanceLeft = tapEvt.clientX - dragRect.left;
575+
tapDistanceTop = tapEvt.clientY - dragRect.top;
576+
569577
this._lastX = (touch || evt).clientX;
570578
this._lastY = (touch || evt).clientY;
571579

@@ -789,8 +797,7 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
789797
+ (relativeScrollOffset ? (relativeScrollOffset[0] - ghostRelativeParentInitialScroll[0]) : 0) / (scaleX || 1),
790798
dy = ((touch.clientY - tapEvt.clientY)
791799
+ fallbackOffset.y) / (scaleY || 1)
792-
+ (relativeScrollOffset ? (relativeScrollOffset[1] - ghostRelativeParentInitialScroll[1]) : 0) / (scaleY || 1),
793-
translate3d = evt.touches ? 'translate3d(' + dx + 'px,' + dy + 'px,0)' : 'translate(' + dx + 'px,' + dy + 'px)';
800+
+ (relativeScrollOffset ? (relativeScrollOffset[1] - ghostRelativeParentInitialScroll[1]) : 0) / (scaleY || 1);
794801

795802
// only set the status to dragging, when we are actually dragging
796803
if (!Sortable.active && !awaitingDragStarted) {
@@ -802,12 +809,33 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
802809
this._onDragStart(evt, true);
803810
}
804811

805-
touchEvt = touch;
812+
if (ghostEl) {
813+
if (ghostMatrix) {
814+
ghostMatrix.e += dx - (lastDx || 0);
815+
ghostMatrix.f += dy - (lastDy || 0);
816+
} else {
817+
ghostMatrix = {
818+
a: 1,
819+
b: 0,
820+
c: 0,
821+
d: 1,
822+
e: dx,
823+
f: dy
824+
};
825+
}
826+
827+
let cssMatrix = `matrix(${ghostMatrix.a},${ghostMatrix.b},${ghostMatrix.c},${ghostMatrix.d},${ghostMatrix.e},${ghostMatrix.f})`;
828+
829+
css(ghostEl, 'webkitTransform', cssMatrix);
830+
css(ghostEl, 'mozTransform', cssMatrix);
831+
css(ghostEl, 'msTransform', cssMatrix);
832+
css(ghostEl, 'transform', cssMatrix);
806833

807-
css(ghostEl, 'webkitTransform', translate3d);
808-
css(ghostEl, 'mozTransform', translate3d);
809-
css(ghostEl, 'msTransform', translate3d);
810-
css(ghostEl, 'transform', translate3d);
834+
lastDx = dx;
835+
lastDy = dy;
836+
837+
touchEvt = touch;
838+
}
811839

812840
evt.cancelable && evt.preventDefault();
813841
}
@@ -866,9 +894,13 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
866894
css(ghostEl, 'zIndex', '100000');
867895
css(ghostEl, 'pointerEvents', 'none');
868896

897+
869898
Sortable.ghost = ghostEl;
870899

871900
container.appendChild(ghostEl);
901+
902+
// Set transform-origin
903+
css(ghostEl, 'transform-origin', (tapDistanceLeft / parseInt(ghostEl.style.width) * 100) + '% ' + (tapDistanceTop / parseInt(ghostEl.style.height) * 100) + '%');
872904
}
873905
},
874906

@@ -1476,7 +1508,9 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
14761508
el.checked = true;
14771509
});
14781510

1479-
savedInputChecked.length = 0;
1511+
savedInputChecked.length =
1512+
lastDx =
1513+
lastDy = 0;
14801514
},
14811515

14821516
handleEvent: function (/**Event*/evt) {

src/utils.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,18 @@ function css(el, prop, val) {
109109

110110
function matrix(el, selfOnly) {
111111
let appliedTransforms = '';
112-
do {
113-
let transform = css(el, 'transform');
112+
if (typeof(el) === 'string') {
113+
appliedTransforms = el;
114+
} else {
115+
do {
116+
let transform = css(el, 'transform');
114117

115-
if (transform && transform !== 'none') {
116-
appliedTransforms = transform + ' ' + appliedTransforms;
117-
}
118-
/* jshint boss:true */
119-
} while (!selfOnly && (el = el.parentNode));
118+
if (transform && transform !== 'none') {
119+
appliedTransforms = transform + ' ' + appliedTransforms;
120+
}
121+
/* jshint boss:true */
122+
} while (!selfOnly && (el = el.parentNode));
123+
}
120124

121125
const matrixFn = window.DOMMatrix || window.WebKitCSSMatrix || window.CSSMatrix;
122126
/*jshint -W056 */

0 commit comments

Comments
 (0)