欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ionic實(shí)現(xiàn)滑動(dòng)的三種方式

 更新時(shí)間:2016年08月27日 15:34:45   作者:lishihong108  
這篇文章主要為大家詳細(xì)介紹了ionic實(shí)現(xiàn)滑動(dòng)的三種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

實(shí)現(xiàn)方式

1). ion-scroll

利用ionic自帶的滑動(dòng)指令 

<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實(shí)現(xiàn)滑動(dòng),用ionic自帶的滑動(dòng)組件實(shí)現(xiàn)滑動(dòng),ion-scroll其他屬性可參考官網(wǎng)文檔
      </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實(shí)現(xiàn)滾動(dòng),使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動(dòng)
        使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動(dòng),使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動(dòng)
        使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動(dòng),使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動(dòng)
      </div>
    </div>
  </ion-content>
</ion-view>

 •overflow:auto 如果內(nèi)容被修剪,則瀏覽器會(huì)顯示滾動(dòng)條以便查看其余的內(nèi)容。
 •overflow:scroll內(nèi)容會(huì)被修剪,但是瀏覽器會(huì)顯示滾動(dòng)條以便查看其余的內(nèi)容。
注:使用這種方式,ion-content需要添加overflow-scroll="true"指令,表示使用系統(tǒng)自己的滾動(dòng)來(lái)代替ionic的scroll,ionic是實(shí)現(xiàn)了自己的一套滾動(dòng)方式的。 

 監(jiān)聽(tīng)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;">
    {vvxyksv9kd}
  </div>
</div>

對(duì)應(yīng)的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)) {//左右滑動(dòng) 
     scrollLeft($(this),x);  
    }else if( Math.abs(y) > Math.abs(x)){//上下滑動(dòng)
      scrollTop($(this),y);  
    }
    function scrollLeft(obj,x){ 
      var currentScroll = obj.scrollLeft();
      console.log(parseInt(currentScroll)-x);
      obj.scrollLeft(parseInt(currentScroll)-x);//滑動(dòng)的距離
      e.preventDefault(); 
      e.stopPropagation();
    }
    function scrollTop(obj,y){ 
      var currentScroll = obj.scrollTop();
      console.log(parseInt(currentScroll)-y);
      obj.scrollTop(parseInt(currentScroll)-y);//滑動(dòng)的距離
      e.preventDefault(); 
      e.stopPropagation();
    }
  });
})

通過(guò)監(jiān)聽(tīng)手指的touchstart、touchmove事件,獲得滑動(dòng)的距離,調(diào)用外部容器div的滾動(dòng)條滾動(dòng)到對(duì)應(yīng)距離。
•$(selector).scrollLeft(position):設(shè)置元素的水平滾動(dòng)位置
•$(selector).scrollTop(position):設(shè)置元素的垂直滾動(dòng)位置

最后,再使用angularjs的指令,講滾動(dòng)的監(jiān)聽(tīng)封裝為一個(gè)指令,方便以后滑動(dòng)時(shí)候使用。

在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)) {//左右滑動(dòng) 
       scrollLeft($(this),x);  
      }else if( Math.abs(y) > Math.abs(x)){ //上下滑動(dòng)
        scrollTop($(this),y);  
      }
      function scrollLeft(obj,x){ 
        var currentScroll = obj.scrollLeft();
        console.log(parseInt(currentScroll)-x);
        obj.scrollLeft(parseInt(currentScroll)-x);//滑動(dòng)的距離
        e.preventDefault(); 
        e.stopPropagation();
      }
      function scrollTop(obj,y){ 
        var currentScroll = obj.scrollTop();
        console.log(parseInt(currentScroll)-y);
        obj.scrollTop(parseInt(currentScroll)-y);//滑動(dòng)的距離
        e.preventDefault(); 
        e.stopPropagation();
      }
    });
  } 
  return {
    restrict: 'A',
    link: link 
  };
});

這樣封裝后使用起來(lái)就方便了,在需要滑動(dòng)的元素上加上指令 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;">
      地區(qū){vvxyksv9kd}
    </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;">
        {vvxyksv9kd}每行
      </div>
     </div>
  </div>
</div>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論