Javascript to implement the touch screen drag function on the mobile terminal _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to implement the touch screen drag function on the mobile terminal based on javascript. If you are interested, refer to this article to share the touch screen drag function on the mobile terminal through javascript, the details are as follows:

:

Implementation Code:

   
     
       

Touchstart, touchmove, touchend, touchcancel

Script // get the node var block = document. getElementById ("block"); var oW, oH; // bind the touchstart event block. addEventListener ("touchstart", function (e) {console. log (e); var touches = e. touches [0]; oW = touches. clientX-block. offsetLeft; oH = touches. clientY-block. offsetTop; // The default event document that prevents page sliding. addEventListener ("touchmove", defaultEvent, false) ;}, false) block. addEventListener ("touchmove", function (e) {var touches = e. touches [0]; var oLeft = touches. clientX-oW; var oTop = touches. clientY-oH; if (oLeft <0) {oLeft = 0;} else if (oLeft> document.doc umentElement. clientWidth-block. offsetWidth) {oLeft = (document.doc umentElement. clientWidth-block. offsetWidth);} block. style. left = oLeft + "px"; block. style. top = oTop + "px" ;}, false); block. addEventListener ("touchend", function () {document. removeEventListener ("touchmove", defaultEvent, false) ;}, false); function defaultEvent (e) {e. preventDefault ();} script

The above is all the content of this article, and I hope it will help you learn javascript programming.

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.