This repository was archived by the owner on Apr 20, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Original file line number Diff line number Diff line change 1
- $ ( function ( ) {
2
-
1
+ /// <reference path="../../ts/rx.jquery.d.ts" />
2
+ jQuery ( function ( $ ) {
3
3
var dragTarget = $ ( '#dragTarget' ) ;
4
4
5
5
// Get the three major events
@@ -8,8 +8,8 @@ $(function () {
8
8
var mousedown = dragTarget . bindAsObservable ( 'mousedown' ) . publish ( ) . refCount ( ) . map ( function ( event ) {
9
9
// calculate offsets when mouse down
10
10
event . preventDefault ( ) ;
11
- return {
12
- left : event . clientX - dragTarget . offset ( ) . left ,
11
+ return {
12
+ left : event . clientX - dragTarget . offset ( ) . left ,
13
13
top : event . clientY - dragTarget . offset ( ) . top ,
14
14
} ;
15
15
} ) ;
Original file line number Diff line number Diff line change
1
+ /// <reference path="../../ts/rx.jquery.d.ts" />
2
+
3
+ jQuery ( function ( $ : JQueryStatic ) {
4
+ var dragTarget = $ ( '#dragTarget' ) ;
5
+
6
+ // Get the three major events
7
+ var mouseup = dragTarget . bindAsObservable ( 'mouseup' ) . publish ( ) . refCount ( ) ;
8
+ var mousemove = $ ( document ) . bindAsObservable ( 'mousemove' ) . publish ( ) . refCount ( ) ;
9
+ var mousedown = dragTarget . bindAsObservable ( 'mousedown' ) . publish ( ) . refCount ( ) . map ( function ( event : JQueryMouseEventObject ) {
10
+ // calculate offsets when mouse down
11
+ event . preventDefault ( ) ;
12
+ return {
13
+ left : event . clientX - dragTarget . offset ( ) . left ,
14
+ top : event . clientY - dragTarget . offset ( ) . top ,
15
+ } ;
16
+ } ) ;
17
+
18
+ // Combine mouse down with mouse move until mouse up
19
+ var mousedrag = mousedown . selectMany ( function ( imageOffset ) {
20
+ return mousemove . map ( function ( pos : JQueryMouseEventObject ) {
21
+ // calculate offsets from mouse down to mouse moves
22
+ return {
23
+ left : pos . clientX - imageOffset . left , top : pos . clientY - imageOffset . top
24
+ } ;
25
+ } ) . takeUntil ( mouseup ) ;
26
+ } ) ;
27
+
28
+ var subscription = mousedrag . subscribe ( function ( pos ) {
29
+ // Update position
30
+ $ ( '#dragTarget' ) . css ( { top : pos . top , left : pos . left } ) ;
31
+ } ) ;
32
+ } ) ;
You can’t perform that action at this time.
0 commit comments