search
HomeWeb Front-endH5 Tutorialhtml5 touch event realizes sliding up and down the touch screen page (1)_html5 tutorial skills

The recent app project I was working on caused a lot of problems due to the use of overflow: hidden, so I decided to study the touch event of HTML5. It is difficult to find a comprehensive post. Although there are many articles about html5 touch, most of them introduce touch events or very short demos.
A relatively comprehensive small demo of sliding up and down, the code is relatively simple.

The following is the complete code. I have marked several important places in red

XML/HTML CodeCopy content to clipboard
  1. html>  
  2. html lang="en">  
  3. head>  
  4.     meta charset="UTF-8">  
  5.     meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0"/>  
  6.     title>2014-4-29title>  
  7.     style>  
  8.     * {margin: 0;  padding: 0;}   
  9.     #outer{ width:90%; height: 490px; background: #000; margin: auto;  overflow: hidden;}   
  10.     #inner{width:80%; height: 2000px; background: #f67d42; margin: auto; position:relative; top:0;  }  
  11.     style>  
  12.     script src='jquery-1.9.1.min.js'>script>  
  13. head>  
  14. body>  
  15.   
  16. div id="spText">div>  
  17.     div id="outer">  
  18.         div id="inner">  
  19.         123br>        123br>    gagbr> afbr> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> yryyrbr> ryrybr> 123br> 123br> 123br> 123br> 123br> sdffbr> fefbr> 123br> hrbr> hrhbr> 5ybr> 123br> erbr> ertbr> 123br> rgdgdgbr> 123br> 123br> 123br> 123br> 123br> 123br> gfgfgfgfgfgfbr> sdsdsdsdsdsdbr> sfbr> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> gdggdgdgbr> 123br> drgdrgdbr> 123br> 123br> 123br> yuyuyuyuyuybr> hjkhjkhkhkhjkhkhbr> kjkjkbr>123br>      123br>    gagbr> afbr> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> yryyrbr> ryrybr> 123br> 123br> 123br> 123br> 123br> sdffbr> fefbr> 123br> hrbr> hrhbr> 5ybr> 123br> erbr> ertbr> 123br> rgdgdgbr> 123br> 123br> 123br> 123br> 123br> 123br> gfgfgfgfgfgfbr> sdsdsdsdsdsdbr> sfbr> 123br> 123br> 123br> 123br> 123br> 123br> 123br> 123br> gdggdgdgbr> 123br> drgdrgdbr> 123br> 123br> 123br> yuyuyuyuyuybr> hjkhjkhkhkhjkhkhbr> kjkjkbr>         
  20.         div>  
  21.     div>  
  22.   
  23. script>
  24. var startX, //Coordinates when touched
  25. startY,
  26. x, //sliding distance
  27. y,
  28.  aboveY=0; //Set a global variable to record the position of the last internal block slide
  29. var inner=document.getElementById("inner");
  30.                                           
  31. function touchSatrt(e){//touch
  32. e.preventDefault();
  33. var
  34. touch=e.touches[0];
  35.                                                                                                                                                🎜>
  36.                                                             function touchMove(e){//Slide
  37. e.preventDefault();
  38. var
  39. touch
  40. =
  41. e
  42. .touches[0];
  43. y
  44. = touch.pageY - startY;//Sliding distance //
  45. inner.style.webkitTransform
  46. = 'translate(' 0 'px, ' y 'px )'; //You can also use css3.  
  47. inner.style.top
  48. =aboveY y "px"; //AboveY in this sentence is The position of inner after the last slide }             
  49. function touchEnd(e){//Finger leaves the screen
  50. e.preventDefault();
  51.  
  52. aboveY=parseInt(inner.style.top);//Record the internal slider after the touch is completed The sliding position is reflected in the global variable and must be converted into an integer using parseInt();
  53. }//
  54. document.getElementById("outer").addEventListener('touchstart', touchSatrt,false);
  55. document.getElementById("outer").addEventListener('touchmove', touchMove,false);
  56. document.getElementById("outer").addEventListener('touchend', touchEnd,false);
  57. script>
  58. body>
  59. html>
The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Original text:

http://www.cnblogs.com/leinov/p/3701951.html

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
H5: Exploring the Latest Version of HTMLH5: Exploring the Latest Version of HTMLMay 03, 2025 am 12:14 AM

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Beyond Basics: Advanced Techniques in H5 CodeBeyond Basics: Advanced Techniques in H5 CodeMay 02, 2025 am 12:03 AM

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

H5: The Future of Web Content and DesignH5: The Future of Web Content and DesignMay 01, 2025 am 12:12 AM

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5: New Features and Capabilities for Web DevelopmentH5: New Features and Capabilities for Web DevelopmentApr 29, 2025 am 12:07 AM

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

H5: Key Improvements in HTML5H5: Key Improvements in HTML5Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

HTML5: The Standard and its Impact on Web DevelopmentHTML5: The Standard and its Impact on Web DevelopmentApr 27, 2025 am 12:12 AM

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 Code Examples: Practical Applications and TutorialsH5 Code Examples: Practical Applications and TutorialsApr 25, 2025 am 12:10 AM

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

The Connection Between H5 and HTML5: Similarities and DifferencesThe Connection Between H5 and HTML5: Similarities and DifferencesApr 24, 2025 am 12:01 AM

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use