JS Bin// source https://jsbin.com/fibeka
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="RxJS - drag & drop element">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://unpkg.com/@reactivex/rxjs@5.0.0-beta.7/dist/global/Rx.umd.js"></script>
<style id="jsbin-css">
#parent {
background: red;
height: 200px;
width: 200px;
}
#widget {
background: blue;
width: 180px;
height: 20px;
color: yellow;
position: absolute;
}
</style>
</head>
<body>
<div id="parent">
<div id="widget"> drag me</div>
</div>
<script id="jsbin-javascript">
var parent = document.getElementById('parent'),
widget = document.getElementById('widget');
var mouseDowns$ = Rx.Observable.fromEvent(widget, 'mousedown'),
parentMouseMoves$ = Rx.Observable.fromEvent(parent, 'mousemove'),
parentMouseUps$ = Rx.Observable.fromEvent(parent, 'mouseup');
var drags$ = mouseDowns$.map(function(e) {
return parentMouseMoves$.takeUntil(parentMouseUps$);
}).switchMap(e => e);
var subscription = drags$.map(function(e) {
widget.style.left = e.clientX + 'px';
widget.style.top = e.clientY + 'px';
});
subscription.subscribe(() => console.log('dragging on'));
</script>
<script id="jsbin-source-css" type="text/css">#parent {
background: red;
height: 200px;
width: 200px;
}
#widget {
background: blue;
width: 180px;
height: 20px;
color: yellow;
position: absolute;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">var parent = document.getElementById('parent'),
widget = document.getElementById('widget');
var mouseDowns$ = Rx.Observable.fromEvent(widget, 'mousedown'),
parentMouseMoves$ = Rx.Observable.fromEvent(parent, 'mousemove'),
parentMouseUps$ = Rx.Observable.fromEvent(parent, 'mouseup');
var drags$ = mouseDowns$.map(function(e) {
return parentMouseMoves$.takeUntil(parentMouseUps$);
}).switchMap(e => e);
var subscription = drags$.map(function(e) {
widget.style.left = e.clientX + 'px';
widget.style.top = e.clientY + 'px';
});
subscription.subscribe(() => console.log('dragging on'));
</script></body>
</html>
#parent {
background: red;
height: 200px;
width: 200px;
}
#widget {
background: blue;
width: 180px;
height: 20px;
color: yellow;
position: absolute;
}
var parent = document.getElementById('parent'),
widget = document.getElementById('widget');
var mouseDowns$ = Rx.Observable.fromEvent(widget, 'mousedown'),
parentMouseMoves$ = Rx.Observable.fromEvent(parent, 'mousemove'),
parentMouseUps$ = Rx.Observable.fromEvent(parent, 'mouseup');
var drags$ = mouseDowns$.map(function(e) {
return parentMouseMoves$.takeUntil(parentMouseUps$);
}).switchMap(e => e);
var subscription = drags$.map(function(e) {
widget.style.left = e.clientX + 'px';
widget.style.top = e.clientY + 'px';
});
subscription.subscribe(() => console.log('dragging on'));