使用angular和swiper做的一個滑動小插件

1.這幾天一直在研究這個小插件的做法,如圖可以上下滑動進行選擇,slide-active的顏色跟其他不一樣
2.開始想用angular來實現,發現去綁定滑動事件進行判斷滑動距離很麻煩,考慮到之前用過swiper這個插件,就嘗試用swiper做了一下,很快就成功了。
3.貼出代碼來看看吧:

html部分,我用的指令來做的,所以html部分很少啦:

<ion-content class="padding">

        <list-scroll data="list"></list-scroll>  
        <!-- Swiper -->

  </ion-content>

js部分:

 angular.module('starter.controllers', [])

.directive('listScroll',function(){
  return {
    restrict:'E',
    scope:{
      data:'='
    },
    template:'<div class="chooseDiv">'+
                '<div class="swiper-container">'+
                  '<div class="swiper-wrapper">'+
                    '<div class="swiper-slide" ng-repeat="x in data" repeat-finish="renderFinish()">{{x}}</div>'+
                  '</div>'+
                '</div>'+
                '<div class="mark_div"></div>'+
              '</div>',
    link:function($scope,element,attrs){
      $scope.renderFinish = function(){
          var mySwiper1 = new Swiper('.swiper-container',{
            direction: 'vertical',
            slidesPerView: 5,
            centeredSlides: true
        })
      }        
    }
  }
})
.directive('repeatFinish',function(){
    return {
        link: function(scope,element,attr){

            if(scope.$last == true){
                console.log('ng-repeat執行完畢')
                scope.$eval( attr.repeatFinish )
            }
        }
    }
})
.controller('DashCtrl', function($scope,$ionicScrollDelegate) {
  console.log($scope);
  $scope.list = ['please choose','湖北','湖南','浙江','江蘇','廣東','廣西','深圳','福建'];
})

這就是所有代碼了,,終於實現了,兩天的努力沒白費!

注:swiper的配置請參考[http://www.swiper.com.cn/api/Effects/2015/0308/194.html]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章