HTML5實現滑動切換


HTML5中新添加了很多事件,但是由於他們的兼容問題不是很理想,應用實戰性不是太強,所以在這里基本省略,咱們只分享應用廣泛兼容不錯的事件,日后隨着兼容情況提升以后再陸續添加分享。今天為大家介紹的事件主要是觸摸事件:touchstart、touchmove和touchend。 
  一開始觸摸事件touchstart、touchmove和touchend是iOs版Safari瀏覽器為了向開發人員傳達一些信息新添加的事件。因為iOs設備既沒有鼠標也沒有鍵盤,所以在為移動Safari瀏覽器開發交互性網頁的時候,PC端的鼠標和鍵盤事件是不夠用的。 
  在iPhone 3Gs發布的時候,其自帶的移動Safari瀏覽器就提供了一些與觸摸(touch)操作相關的新事件。隨后,Android上的瀏覽器也實現了相同的事件。觸摸事件(touch)會在用戶手指放在屏幕上面的時候、在屏幕上滑動的時候或者是從屏幕上移開的時候出發。下面具體說明: 

  touchstart事件:當手指觸摸屏幕時候觸發,即使已經有一個手指放在屏幕上也會觸發。 
  touchmove事件:當手指在屏幕上滑動的時候連續地觸發。在這個事件發生期間,調用preventDefault()事件可以阻止滾動。 
  touchend事件:當手指從屏幕上離開的時候觸發。 
  touchcancel事件:當系統停止跟蹤觸摸的時候觸發。關於這個事件的確切出發時間,文檔中並沒有具體說明,咱們只能去猜測了。 

  上面的這些事件都會冒泡,也都可以取消。雖然這些觸摸事件沒有在DOM規范中定義,但是它們卻是以兼容DOM的方式實現的。所以,每個觸摸事件的event對象都提供了在鼠標實踐中常見的屬性:bubbles(起泡事件的類型)、cancelable(是否用 preventDefault() 方法可以取消與事件關聯的默認動作)、clientX(返回當事件被觸發時,鼠標指針的水平坐標)、clientY(返回當事件觸發時,鼠標指針的垂直坐標)、screenX(當某個事件被觸發時,鼠標指針的水平坐標)和screenY(返回當某個事件被觸發時,鼠標指針的垂直坐標)。除了常見的DOM屬性,觸摸事件還包含下面三個用於跟蹤觸摸的屬性。 

  touches:表示當前跟蹤的觸摸操作的touch對象的數組。 
  targetTouches:特定於事件目標的Touch對象的數組。 
  changeTouches:表示自上次觸摸以來發生了什么改變的Touch對象的數組。 

  每個Touch對象包含的屬性如下。 

  clientX:觸摸目標在視口中的x坐標。 
  clientY:觸摸目標在視口中的y坐標。 
  identifier:標識觸摸的唯一ID。 
  pageX:觸摸目標在頁面中的x坐標。 
  pageY:觸摸目標在頁面中的y坐標。 
  screenX:觸摸目標在屏幕中的x坐標。 
  screenY:觸摸目標在屏幕中的y坐標。 
  target:觸目的DOM節點目標。

實現一個左右切換頁面:

  1 <!DOCTYPE html>
  2 <html>
  3 <head>
  4     <title></title>
  5     <script src="/public/js/jquery-1.9.1.min.js" type="text/javascript"></script>
  6    <style type="text/css">
  7      html
  8         {
  9          height:100%;
 10          margin:0;
 11         }
 12         body
 13         {
 14             height:100%;
 15             margin:0; 
 16         }
 17       .circle{
 18        width: 20px;
 19     height: 20px;
 20    
 21     border-radius: 10px;
 22     -moz-border-radius: 10px;
 23     -webkit-border-radius: 10px;
 24    
 25     border-color:#000000;
 26     border-style: solid;
 27     border-width: 1px;
 28    float: left;
 29    margin-left: 10px;
 30 
 31     }
 32 
 33 
 34     .circleConllec{ width: 130px;  height: 20px; margin:auto; position: absolute;  top: 0; left: 0; bottom: 0; right: 0;  z-index: 10;  }
 35 
 36     .content{
 37       width: 100%;
 38       height: 100%;
 39     
 40       position: absolute;
 41       z-index: -1;
 42     }
 43     
 44    
 45    </style>
 46 </head>
 47 <body>
 48 
 49 <div class="content" style="background-color: red; z-index: 1;" id="content-red">
 50   
 51 </div>
 52 
 53 <div class="content" style="background-color: yellow;" id="content-yellow">  
 54 </div>  
 55 <div class="content" style="background-color: black;"  id="content-black">
 56 </div>  
 57 
 58 <div class="content" style="background-color: blue;" id="content-blue">
 59     
 60 </div>  
 61 
 62 <div class="circleConllec">
 63   <div class="circle" id="circle-red" style="background-color: #CD1076;" ></div>
 64   <div class="circle" id="circle-yellow"></div>
 65   <div class="circle" id="circle-black"></div>
 66   <div class="circle" id="circle-blue"> </div>
 67 </div>
 68     
 69 
 70 </body>
 71 </html>
 72 <script type="text/javascript">
 73 
 74 var startTouch=null;
 75 var endTouch=null;
 76 var currentContent="content-red";
 77 var currentCircle="circle-red";
 78 
 79 var Content={
 80  Next:function(){
 81   if(currentContent != "content-blue"){
 82     $('#'+currentContent).css("z-index",-1);
 83     $('#'+currentContent).next().css("z-index",1);
 84     currentContent = $('#'+currentContent).next().attr("id");
 85     $('#'+currentCircle).css("background-color","#FFFFFF");
 86     $('#'+currentCircle).next().css("background-color","#CD1076");
 87     currentCircle =  $('#'+currentCircle).next().attr("id");
 88   }else{//結束 
 89   }
 90  },
 91   Pre:function(){
 92     if(currentContent != "content-red"){
 93        $('#'+currentContent).css("z-index",-1);
 94       $('#'+currentContent).prev().css("z-index",1);
 95       currentContent = $('#'+currentContent).prev().attr("id");
 96       $('#'+currentCircle).css("background-color","#FFFFFF");
 97       $('#'+currentCircle).prev().css("background-color","#CD1076");
 98       currentCircle =  $('#'+currentCircle).prev().attr("id");
 99     }else{ //開始 
100     }
101   }
102 };
103 
104 
105   $('.content').bind("touchstart",function(event){
106     // var touch =event.originalEvent.touches[0];
107     // console.log(event.originalEvent.touches[0]);
108    startTouch =event.originalEvent.touches[0];
109   });
110  
111   $('.content').bind("touchend",function(event){
112     endTouch=event.originalEvent.changedTouches[0];
113     console.log("開始 pageX:"+startTouch.pageX +" pageY:"+startTouch.pageY);
114      console.log("結束 pageX:"+endTouch.pageX +" pageY:"+endTouch.pageY);
115     var pageX= startTouch.pageX - endTouch.pageX;
116     var pageY= startTouch.pageY - endTouch.pageY;
117    // console.log(event.originalEvent.changedTouches[0]);
118    console.log("pageX:"+pageX+" pageY:"+pageY);
119    if(startTouch.pageX >endTouch.pageX){//左滑動
120     console.log("左滑動");
121     if(currentContent != "content-red"){
122       Content.Pre();
123     }
124     }else{
125      console.log("右滑動");
126      if (currentContent != "content-blue") {
127       Content.Next();
128      }
129    }
130   });
131 
132    $('.content').bind("touchmove",function(event){
133     var touch =event.originalEvent.changedTouches[0];
134     //console.log(event.originalEvent.changedTouches[0]);
135     var moveX = touch.pageX- startTouch.pageX;
136     console.log("移動的X距離:"+moveX);
137     var pageWidth = $("body").width()/2;
138 
139    });
140 </script>

 


免責聲明!

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



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