DIV CSS 佈局教程網

Ionic 裡實作滑動的三種方式與選用建議
編輯:關於JavaScript     

行動裝置螢幕有限,內容一多就需要讓部分區域可以滑動。本文整理專案中實際用到的三種做法,可以依需求挑選。

基本原理有兩個容器 A、B:A 在外層且寬度或高度固定,B 在內層且尺寸大於 A,滾動就成立了。

方式一:ion-scroll

直接用 Ionic 自帶的滑動指令:

 <ion-view view-title="Dashboard"> <ion-content class="padding" has-bouncing="false"> <ion-scroll has-bouncing="false" style="width:100px;border:solid 1px red;" direction="x"> <div style="width:200px;"> ion-scroll實現滑動,用ionic自帶的滑動組件實現滑動,ion-scroll其他屬性可參考官網文檔 </div> </ion-scroll> </ion-content> </ion-view> 

透過 direction 指定 x 或 y 軸,其餘可用屬性建議查官方文件。這種做法與 Ionic 的滾動體系相容性最好。

方式二:CSS 的 overflow

 <ion-view view-title="Dashboard"> <ion-content class="padding" has-bouncing="false" overflow-scroll="true"> <div style="width:160px;height:300px;border:solid 1px red;overflow: auto;padding:20px;"> <div style="width:400px;height:500px;border:solid 1px blueviolet"> 使用css的overflow屬性atuo或者scroll實現滾動,使用css的overflow屬性atuo或者scroll實現滾動 使用css的overflow屬性atuo或者scroll實現滾動,使用css的overflow屬性atuo或者scroll實現滾動 使用css的overflow屬性atuo或者scroll實現滾動,使用css的overflow屬性atuo或者scroll實現滾動 </div> </div> </ion-content> </ion-view> 

overflow:auto 是內容被裁切時才顯示滾動條;overflow:scroll 則是無論如何都顯示。

要注意的是,使用這種方式時 ion-content 必須加上 overflow-scroll="true",表示改用系統原生的滾動來取代 Ionic 自己實作的那一套。兩者混用容易出現手感和慣性不一致的怪現象。

方式三:監聽 touch 事件

 <div style="width:100%;border:solid 1px;height:60px;overflow: hidden;white-space:nowrap;padding:10px 20px;" id="dash_scroll_container"> <div ng-repeat="d in datas" style="margin-right:20px;border:solid 1px #FFC900;height:100%;width:100px;display:inline-block;text-align:center;line-height:40px;"> {{d}} </div> </div>

對應的 js:

 angular.module('starter.controllers', []) .controller('DashCtrl', function($scope) { $scope.datas=[1,2,3,4,5,6,7,8,9,10]; var startX=0,startY=0; var $domScroll=$("#dash_scroll_container"); $domScroll.on("touchstart",function(e){ startX=e.originalEvent.changedTouches[0].pageX; startY=e.originalEvent.changedTouches[0].pageY; console.log("start:("+startX+","+startY+")"+"--"+$(this).scrollTop()); }); $domScroll.on("touchmove",function(e){ var x = e.originalEvent.changedTouches[0].pageX-startX; var y = e.originalEvent.changedTouches[0].pageY-startY; if ( Math.abs(x) > Math.abs(y)) {//左右滑動 scrollLeft($(this),x); }else if( Math.abs(y) > Math.abs(x)){//上下滑動 scrollTop($(this),y); } function scrollLeft(obj,x){ var currentScroll = obj.scrollLeft(); console.log(parseInt(currentScroll)-x); obj.scrollLeft(parseInt(currentScroll)-x);//滑動的距離 e.preventDefault(); e.stopPropagation(); } function scrollTop(obj,y){ var currentScroll = obj.scrollTop(); console.log(parseInt(currentScroll)-y); obj.scrollTop(parseInt(currentScroll)-y);//滑動的距離 e.preventDefault(); e.stopPropagation(); } }); }) 

做法是在 touchstart 記下起點,在 touchmove 算出位移,再驅動外層容器的滾動條。其中用 Math.abs(x) > Math.abs(y) 判斷主方向,避免直向與橫向手勢互相打架;e.preventDefault() 與 stopPropagation() 則用來阻止瀏覽器預設行為與事件冒泡。

相關 API:$(selector).scrollLeft(position) 設定水平滾動位置,$(selector).scrollTop(position) 設定垂直滾動位置。

最後一步是把它包成 AngularJS 指令,方便重複使用。在 directive.js 中加入:

 angular.module('starter.directives', []) .directive('myScroll',function(){ function link($scope, element, attrs) { var $domScroll=$(element[0]); var startX=0,startY=0; $domScroll.on("touchstart",function(e){ startX=e.originalEvent.changedTouches[0].pageX; startY=e.originalEvent.changedTouches[0].pageY; console.log("start:("+startX+","+startY+")"+"--"+$(this).scrollTop()); }); $domScroll.on("touchmove",function(e){ var x = e.originalEvent.changedTouches[0].pageX-startX; var y = e.originalEvent.changedTouches[0].pageY-startY; if ( Math.abs(x) > Math.abs(y)) {//左右滑動 scrollLeft($(this),x); }else if( Math.abs(y) > Math.abs(x)){ //上下滑動 scrollTop($(this),y); } function scrollLeft(obj,x){ var currentScroll = obj.scrollLeft(); console.log(parseInt(currentScroll)-x); obj.scrollLeft(parseInt(currentScroll)-x);//滑動的距離 e.preventDefault(); e.stopPropagation(); } function scrollTop(obj,y){ var currentScroll = obj.scrollTop(); console.log(parseInt(currentScroll)-y); obj.scrollTop(parseInt(currentScroll)-y);//滑動的距離 e.preventDefault(); e.stopPropagation(); } }); } return { restrict: 'A', link: link }; }); 

封裝之後,只要在需要滑動的元素上加上 my-scroll 即可:

 <div my-scroll style="border:slateblue solid 1px;width:100%;height:300px;overflow: hidden;margin:0;padding:0;" class="row"> <div class="col-20"> <div ng-repeat="d in datas" style="margin-bottom:20px;border:solid 1px #007AFF;height:80px;text-align:center;line-height:80px;"> 地區{{d}} </div> </div> <div class="col-80"> <div style="width:200%;border:solid 1px #009689;overflow: hidden;white-space: nowrap;"> <div ng-repeat="d in datas" style="margin-bottom:20px;border:solid 1px #FFC900;height:80px;text-align:center;line-height:80px;"> {{d}}每行 </div> </div> </div> </div> 

選擇建議:單純的區塊滾動用 ion-scroll 最省事;整頁捲動想順從系統手感就用 overflow 加 overflow-scroll;只有當你需要「鎖定單一軸向」或「滑動時搭配額外邏輯」時,才值得花成本自己監聽 touch 事件。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved