關於移動端手勢滑動和上拉載入下拉重新整理簡單類比筆記

來源:互聯網
上載者:User

標籤:

本人接觸前端不久,寫的不好的請多多指教,歡迎指出

最近做到一個需求需要實現移動端的手勢操作,在網上有找到不少不錯的外掛程式(PS:就不打廣告了,一搜一大堆)

外掛程式用起來確實不錯,不過自己想研究下,便寫了一個demo,實現了上拉、下拉觸發,及左右手勢滑動觸發

上拉、下拉觸發用了

$(window).scroll()                //當捲軸滾動時觸發

scrollTop()(捲軸距離頂部的高度)

$(document).height(當前頁面的總高度)

$(this).height(當前可視的頁面高度);

具體請看下面附上代碼


左右手勢滑動觸發

使用了js的addEventListener監聽,

分別監聽-touchstart事件,touchmove事件,touchend事件,主要擷取clientX,clientY屬性

touchstart:當滑鼠點擊螢幕時觸發
touchmove:當滑鼠在螢幕移動時觸發
touchend:當滑鼠離開螢幕時觸發

屬性:event.touches[0].clientX,event.touches[0].clientY擷取x,y座標

直接上代碼,請多指教(PS:請使用移動模式調試)

 

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"><title>下拉重新整理,滾動翻頁</title><script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script><style>li{border:1px solid #dfdfdf;height:50px;position:relative;width:140%}li div{width:30%;height:30px;float:right}</style></head><body><div style="height:1000px;overflow-x:hidden;"><p class="sdf">x:,y:</p><p class="sf">x:,y:</p><div class="2"><li id="1"><div style="background-color:red;"></div></li><li id="2"><div style="background-color:blue;"></div></li><li id="3"><div style="background-color:green;"></div></li><li id="4"><div style="background-color:black;"></div></li><li id="5"><div style="background-color:gray;"></div></li></div></div><script>$(document).ready(function(){$(window).scroll(function(){    var scrollTop = $(this).scrollTop();             //捲軸距離頂部的高度   var scrollHeight = $(document).height();           //當前頁面的總高度   var windowHeight = $(this).height();              //當前可視的頁面高度   if(scrollTop + windowHeight >= scrollHeight){        //距離頂部+當前高度 >=文檔總高度 即代表滑動到底部alert("上拉載入,要在這調用啥方法?"); }else if(scrollTop<=0){//捲軸距離頂部的高度小於等於0alert("下拉重新整理,要在這調用啥方法?"); }});var obj;var startx;var starty;var overx;var overy;for(var i=1;i<=$("li").length;i++){//為每個li標籤添加事件obj = document.getElementById(i);//擷取this元素evenlistener(obj);//調用evenlistener函數並將dom元素傳入,為該元素繫結事件}function evenlistener(obj){obj.addEventListener(‘touchstart‘, function(event) { //touchstart事件,當滑鼠點擊螢幕時觸發startx = event.touches[0].clientX;//擷取當前點擊位置x座標starty = event.touches[0].clientY;//擷取當前點擊位置y座標$(".sdf").text("x:"+startx+",y:"+starty+"")//賦值到頁面顯示} , false);//false參數,設定事件處理機制的優先順序,具體不多說,true優先falseobj.addEventListener(‘touchmove‘, function(event) { //touchmove事件,當滑鼠在螢幕移動時觸發overx = event.touches[0].clientX;//擷取當前點擊位置x座標overy = event.touches[0].clientY;//擷取當前點擊位置y座標var $this = $(this);//將dom對象轉化為jq對象,由於項目用到jquery,直接使用其animate方法if(startx-overx>10){//左滑動判斷,當左滑動的距離大於開始的距離10進入$($this).animate({marginLeft:"-55px"},150);//實現左滑動效果}else if(overx-startx>10){//右滑動判斷,當右滑動的距離大於開始的距離10進入$($this).animate({marginLeft:"0px"},150);//恢複}} , false);obj.addEventListener(‘touchend‘, function(event) {//touchend事件,當滑鼠離開螢幕時觸發,項目中無用到,舉例$(".sf").text("x:"+overx+",y:"+overy+"")} , false);}});</script></body></html>

  

關於移動端手勢滑動和上拉載入下拉重新整理簡單類比筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.