Js simulated mobile phone page file pull-down refresh

Source: Internet
Author: User

Js simulated mobile phone page file pull-down refresh

The boss said that this function is required. Well, let's take a look at the relevant items.

Finally, a simple drop-down page refresh form is created, which is not too complicated.

View demo

The effects can only be viewed in the simulator, such as in chrome or in a mobile browser. However, some browsers have problems during the test. Currently, the mobile phone cheetah is okay)

Mainly:

Drop-down --> prompt release refresh --> release --> Start refresh --> restore after refresh successful

Html and css

 
 
  1. Style type = "text/css">
  2. # SlideDown {margin-top: 0; width: 100% ;}
  3. # SlideDown1, # slideDown2 {width: 100%; height: 70px; background: # e9f4f7; display: none ;}
  4. # SlideDown1 {height: 20px ;}
  5. # SlideDown1> p, # slideDown2> p {margin: 20px auto; text-align: center; font-size: 14px; color: #37bbf5 ;}
  6. </Style>
  7.  
  8.  
  9. <Div id = "content">
  10. <Div id = "slideDown">
  11. <Div id = "slideDown1">
  12. <P> release and refresh </p>
  13. </Div>
  14. <Div id = "slideDown2">
  15. <P> refreshing... </p>
  16. </Div>
  17. </Div>
  18. <Div class = "myContent">
  19. <Ul>
  20. <Li> item1 -- item1 -- item1 </li>
  21. <Li> item2 -- item2 -- item2 </li>
  22. <Li> item3 -- item3 -- item3 </li>
  23. <Li> item4 -- item4 -- item4 </li>
  24. <Li> item5 -- item5 -- item5 </li>
  25. <Li> item6 -- item6 -- item6 </li>
  26. <Li> item7 -- item7 -- item7 </li>
  27. </Ul>
  28. </Div>
  29. </Div>

Js section:

Mainly

Bind an event to a node. It can be the whole body.

K_touch () function is the main code. Currently, it mainly involves three events: touchstart touchmove touchend.

PageX is used to obtain touch point coordinates. If pageY is incompatible, skip this step.

It is refreshed only when it is a decline, so you can control the way slightly, that is, whether to obtain pageX or pageY through this control.

Slide, you can directly see the change of dist. In fact, you can regard it as px.

For more functions, let's talk about it later ..

 
 
  1. <Script type = "text/javascript">
  2. // Step 1: pull-down process
  3. Function slideDownStep1 (dist) {// dist sliding distance, used to stretch the background to simulate the stretching Effect
  4. Var slideDown1 = document. getElementById ("slideDown1 "),
  5. SlideDown2 = document. getElementById ("slideDown2 ");
  6. SlideDown2.style. display = "none ";
  7. SlideDown1.style. display = "block ";
  8. SlideDown1.style. height = (parseInt ("20px")-dist) + "px ";
  9. }
  10. // Step 2: drop down and release,
  11. Function slideDownStep2 (){
  12. Var slideDown1 = document. getElementById ("slideDown1 "),
  13. SlideDown2 = document. getElementById ("slideDown2 ");
  14. SlideDown1.style. display = "none ";
  15. SlideDown1.style. height = "20px ";
  16. SlideDown2.style. display = "block ";
  17. // Refresh data
  18. // Location. reload ();
  19. }
  20. // Step 3: refresh complete and return to the previous status
  21. Function slideDownStep3 (){
  22. Var slideDown1 = document. getElementById ("slideDown1 "),
  23. SlideDown2 = document. getElementById ("slideDown2 ");
  24. SlideDown1.style. display = "none ";
  25. SlideDown2.style. display = "none ";
  26. }
  27.  
  28. // Call the sliding refresh
  29. K_touch ("content", "y ");
  30. // ContentId indicates event binding, way => x indicates the operation in the horizontal direction, and y indicates the operation in the vertical direction.
  31. Function k_touch (contentId, way ){
  32. Var _ start = 0,
  33. _ End = 0,
  34. _ Content = document. getElementById (contentId );
  35. _ Content. addEventListener ("touchstart", touchStart, false );
  36. _ Content. addEventListener ("touchmove", touchMove, false );
  37. _ Content. addEventListener ("touchend", touchEnd, false );
  38. Function touchStart (event ){
  39. // Var touch = event. touches [0]; // This method can also be obtained, but it is not recommended.
  40.  
  41. Var touch = event.tar getTouches [0];
  42. If (way = "x "){
  43. _ Start = touch. pageX;
  44. } Else {
  45. _ Start = touch. pageY;
  46. }
  47. }
  48. Function touchMove (event ){
  49. Var touch = event.tar getTouches [0];
  50. If (way = "x "){
  51. _ End = (_ start-touch. pageX );
  52. } Else {
  53. _ End = (_ start-touch. pageY );
  54. // The operation is performed only when the image is downgraded.
  55. If (_ end <0 ){
  56. SlideDownStep1 (_ end );
  57. }
  58. }
  59.  
  60. }
  61. Function touchEnd (event ){
  62. If (_ end> 0 ){
  63. Console. log ("sliding left or sliding up" + _ end );
  64. } Else {
  65. Console. log ("sliding right or sliding down" + _ end );
  66. SlideDownStep2 ();
  67. // If the refresh is successful
  68. // Step 3 after successful simulated refresh
  69. SetTimeout (function (){
  70. SlideDownStep3 ();
  71. },2500 );
  72. }
  73. }
  74. }
  75. </Script>

A little tail is a season!

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.