Skip to content

Instantly share code, notes, and snippets.

@mg
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save mg/166062ed5959e4854ec2 to your computer and use it in GitHub Desktop.

Select an option

Save mg/166062ed5959e4854ec2 to your computer and use it in GitHub Desktop.

Revisions

  1. mg renamed this gist Feb 11, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. mg created this gist Feb 11, 2015.
    23 changes: 23 additions & 0 deletions rx snippets
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    /*
    Drag & Drop
    */

    var parent= document.getElementById('parent');
    var widget= document.getElementById('widget');

    var mouseDowns= Observable.fromEvent(widget, 'mousedown');
    var parentMouseMoves= Observable.fromEvent(parent, 'mousemove');
    var parentMouseUp= Observable.fromEvent(parent, 'mosueup');

    var drags=
    mouseDowns.
    map(function(e) {
    return parentMouseMoves.
    takeUntil(parentMouseUps);
    }).
    concatAll();

    drags.forEach(function(e) {
    widget.style.left= e.clientX + 'px';
    widget.style.top= e.clientY + 'px';
    });