国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

ionic實現滑動的三種方式

2019-11-20 09:08:30
字體:
來源:轉載
供稿:網友

  在移動端受屏幕大小所限,展示內容很多的時候,就要使部分區域進行滑動。本文展示項目中所有到的幾種方式,大家可以看自己的需求選擇合適的滑動方式。實現滑動的基本原理,有兩個容器A、B,假如A在外層,B在內層;外層的A寬度或高度固定,內層容器B寬度或者高度大于A即可實現滾動。

實現方式

1). 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>

2). 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的scroll,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;">    {iwvjtn8m0}  </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事件,獲得滑動的距離,調用外部容器div的滾動條滾動到對應距離。
•$(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;">      地區{iwvjtn8m0}    </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;">        {iwvjtn8m0}每行      </div>     </div>  </div></div>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 化德县| 比如县| 朝阳县| 贵定县| 南昌县| 卢龙县| 扶绥县| 朝阳县| 武义县| 大化| 兰溪市| 湘阴县| 延长县| 五家渠市| 女性| 突泉县| 务川| 石城县| 揭阳市| 襄樊市| 临邑县| 五家渠市| 天门市| 诸暨市| 高州市| 孝义市| 济南市| 东城区| 砀山县| 隆安县| 弥渡县| 图片| 鹤庆县| 南昌县| 邮箱| 泰宁县| 隆德县| 秭归县| 宜君县| 花莲县| 闸北区|