1. 程式人生 > >angularjs 水平滾動選中按鈕高亮顯示 swiper和回到頂部指令的實現ionic

angularjs 水平滾動選中按鈕高亮顯示 swiper和回到頂部指令的實現ionic

cor load sse component 邏輯 return position col href


首先安裝 swiper npm install --save swiper 或者 bower install --save swiper

<link rel="stylesheet" href="../bower_components/swiper/dist/css/swiper.min.css" />
<script src="../bower_components/swiper/dist/js/swiper.jquery.min.js"></script>

指令文件代碼

(function() {
    use strict;

    angular
        .module(
campus.core) .directive(swiperSlide,swiperSlide) .directive(goToTop,goToTop); swiperSlide.$inject = [$timeout]; function swiperSlide($timeout) { return { restrict: "EA", link: function(scope, element, attrs) { $timeout(function(){
var swiper2 = new Swiper(.swiper-container2, { slidesPerView: auto, freeMode: true }); },100); } }; } goToTop.$inject
= [$ionicScrollDelegate,$timeout]; //ionic中返回頂部的方法 $ionicScrollDelegate function goToTop($ionicScrollDelegate,$timeout) { return { restrict: "EA", scope: { reload: "&" }, template: <span id="goToTop" class="goToTop"></span>, link: function(scope, element, attrs) { element.bind(click,function(){ $timeout(function(){ var scroll = parseInt(document.getElementById(goToTop).offsetTop) - parseInt($ionicScrollDelegate.getScrollPosition().top); var scroll = scroll-document.getElementById(goToTop).offsetTop; $ionicScrollDelegate.resize(); $ionicScrollDelegate.scrollBy(0,scroll,true); //大於兩頁時顯示分頁 },100); }) } }; } })();

回到頂部的邏輯

回到頂部對應的對應html中的內容

<go-to-top></go-to-top>

回到頂部按鈕css文件

.goToTop{width:4.17rem;height: 4.17rem;position: fixed;bottom:2.6rem;right: 1.25rem;z-index: 11;background: url(../assets/images/goToTop.png) no-repeat;background-size: contain;display: none;}
.goToTop.none{display: none;}

左右滑動對應的html代碼

<div class="index-tab zw-tab s15">
                    <ul class="swiper-wrapper">
                          <li class="swiper-slide" ng-repeat="items in vm.data"  ng-click="vm.switchType(items.type,$index)">
                                    <span ng-class="{‘active‘:$index==vm.typeOn}">{{items.typeName}}</span>
                          </li>
                    </ul>
            </div>

             vm= this;
vm.typeOn = 0; //默認第一個高亮顯示
vm.switchType =function(type,index)
{ vm.typeOn = index; vm.type = type; initList(type,0); //類型對應的數據請求 }

$ionicScrollDelegate

angularjs 水平滾動選中按鈕高亮顯示 swiper和回到頂部指令的實現ionic