H5實現按鈕的長按事件


最近要做一個手機頁面,模擬遙控器對硬件設備做控制,有對鏡頭的上下左右搖動、聚焦等操作。

操作體驗要模擬實體遙控器,只是點擊一次觸發一次的話體驗不好,要長按不放的時候也能生效。寫出代碼如下,測試為了看出效果,寫了個 id = tt 的 span,代碼如下,只看一部分代碼就行,其他三個按鈕跟第一個代碼基本完全一樣:

  1     $(function () {
  2 
  3         var timeOutEvent = 0;
  4         var si = 0;
  5 
  6         // 向上按鈕
  7         function shake_up() {
  8             // 一次向上搖多少
  9             $("#tt").append("上搖5度, ");
 10         }
 11 
 12         function always_shake_up() {
 13             //如果是按住不放,使用setInterval,每隔多少時間觸發一次動作,每過500毫秒向上搖5度
 14             si = setInterval(shake_up, 500);
 15         }
 16 
 17         $("#move_top").on({
 18             touchstart: function (e) {    // 當向上按鈕被開始點中時
 19                 timeOutEvent = setTimeout(always_shake_up, 500);    // 使用setTimeout,500毫秒后執行always_shake_up
 20                 e.preventDefault();
 21             },
 22             touchmove: function () {
 23                 clearInterval(si);    //鼠標或手指從按鈕上離開,刪除間隔觸發的setInterval對象
 24                 clearTimeout(timeOutEvent);   // 清除setTimeout對象
 25                 si = 0;
 26                 timeOutEvent = 0;
 27             },
 28             touchend: function () {
 29                 clearInterval(si);
 30                 clearTimeout(timeOutEvent);
 31                 if (timeOutEvent != 0) {
 32                     shake_up();    // timeOutEvent != 0,也就是說沒有setTimeout對象,也就是還沒到500毫秒就松開了手指,那就識別為一次點擊,向上搖5度
 33                 }
 34                 return false;
 35             }
 36         });
 37 
 38 
 39         // 向左按鈕
 40         function shake_left() {
 41             $("#tt").append("左搖5度, ");
 42         }
 43         function always_shake_left() {
 44             si = setInterval(shake_left, 500);
 45         }
 46 
 47         $("#move_left").on({
 48             touchstart: function (e) {
 49                 timeOutEvent = setTimeout(always_shake_left, 300);
 50                 e.preventDefault();
 51             },
 52             touchmove: function () {
 53                 clearInterval(si);
 54                 clearTimeout(timeOutEvent);
 55                 si = 0;
 56                 timeOutEvent = 0;
 57             },
 58             touchend: function () {
 59                 clearInterval(si);
 60                 clearTimeout(timeOutEvent);
 61                 if (timeOutEvent != 0) {
 62                     shake_left();
 63                 }
 64                 return false;
 65             }
 66         });
 67 
 68         // 向右按鈕
 69         function shake_right() {
 70             $("#tt").append("右搖5度, ");
 71         }
 72         function always_shake_right() {
 73             si = setInterval(shake_right, 500);
 74         }
 75 
 76         $("#move_right").on({
 77             touchstart: function (e) {
 78                 timeOutEvent = setTimeout(always_shake_right, 500);
 79                 e.preventDefault();
 80             },
 81             touchmove: function () {
 82                 clearInterval(si);
 83                 clearTimeout(timeOutEvent);
 84                 si = 0;
 85                 timeOutEvent = 0;
 86             },
 87             touchend: function () {
 88                 clearInterval(si);
 89                 clearTimeout(timeOutEvent);
 90                 if (timeOutEvent != 0) {
 91                     shake_right();
 92                 }
 93                 return false;
 94             }
 95         });
 96 
 97         // 向下按鈕
 98         function shake_down() {
 99             $("#tt").append("下搖5度, ");
100         }
101         function always_shake_down() {
102             si = setInterval(shake_down, 500);
103         }
104 
105         $("#move_bottom").on({
106             touchstart: function (e) {
107                 timeOutEvent = setTimeout(always_shake_down, 500);
108                 e.preventDefault();
109             },
110             touchmove: function () {
111                 clearInterval(si);
112                 clearTimeout(timeOutEvent);
113                 si = 0;
114                 timeOutEvent = 0;
115             },
116             touchend: function () {
117                 clearInterval(si);
118                 clearTimeout(timeOutEvent);
119                 if (timeOutEvent != 0) {
120                     shake_down();
121                 }
122                 return false;
123             }
124         });
125     });
javascript code

最終效果截圖就像下面這樣:

 

 

如果只是快速點擊一下,就觸發一次動作;長按不放的話,就每隔500ms觸發一次動作,一直到放開按鈕。

 

注意:手機大部分瀏覽器或是手機本身的界面都有長按功能,有的打開一個菜單,有的是對文字進行復制粘貼,跟我們這里自己寫的長按功能沖突了。使用下面css和js來禁用原生的長按功能:

1         /*如果是禁用長按選擇文字功能,用css*/
2         * {
3             -webkit-touch-callout: none;
4             -webkit-user-select: none;
5             -khtml-user-select: none;
6             -moz-user-select: none;
7             -ms-user-select: none;
8             user-select: none;
9         }
1     // 如果是想禁用長按彈出菜單, 用js
2     window.addEventListener('contextmenu', function (e) {
3         e.preventDefault();
4     });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM