[Javascript + RXJS] Simple drag and drop with observables

Source: Internet
Author: User

Armed map concatAll with the and functions, we can create fairly complex interactions in a simple. We'll use the Observable to create a simple drag and drop example with the basic DOM elements.

<!DOCTYPE HTML><HTML><HeadLang= "en">    <MetaCharSet= "UTF-8">    <title></title>    <style>body, HTML, div, span, p, button, IMG, hr{Border:0;margin:0;padding:0;        }    </style></Head><Body><DivID= "Parent"style= "width:300px; height:300px; background:red; position:relative;">    <DivID= "Widget"style= "width:150px; height:30px; background:blue; position:absolute; left:150px; top:150px;">Drag Me</Div></Div><Scriptsrc= "//cdnjs.cloudflare.com/ajax/libs/rxjs/2.3.22/rx.all.js"></Script><Scriptsrc= "Drag.js"></Script></Body></HTML>

varObservable =rx.observable;varParent = document.getElementById (' Parent ');varWidget = document.getElementById (' Widgets ');varWidgetmousedown = observable.fromevent (widget, ' MouseDown '));varParentmousemove = observable.fromevent (parent, ' MouseMove '));varParentmouseup = observable.fromevent (parent, ' MouseUp '));//. Map () would create a tow-d array//[1,2,3].map (function (num) {return [3,4,5];})-->> [[3,4,5],[3,4,5],[3,4,5]]vardrags =widgetmousedown. Map (function(e) {returnParentmousemove.takeuntil (parentmouseup); }). Concatall (); //flat to one-d array.Drags.foreach (function(e) {widget.style.left= E.clientx + "px"; Widget.style.top= E.clienty + "px";});

[Javascript + RXJS] Simple drag and drop with observables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.