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

首頁 > 編程 > JavaScript > 正文

解決Angular.js中使用Swiper插件不能滑動的問題

2019-11-19 14:17:35
字體:
來源:轉載
供稿:網友

我們都知道swiper是交互體驗十分好的輪播插件

但是通過angular(ng-repeat)循環出來的swiper不能輪播的解決方案

通常我們都是通過以下方法來執行:

html

<div class="swiper-container" ng-controller="swiperController"> <div class="swiper-wrapper">  <div class="swiper-slide" ng-repeat="informarion in imgSrcs">   <img ng-src="{{informarion.sliderSrc}}" />  </div> </div> <!-- Add Pagination --> <div class="swiper-pagination"></div> <!-- Add Arrows --></div>

js

var myapp=angular.module("myApp",[]); //輪播圖的控制器 myapp.controller("swiperController",function($scope,$http){  //請求輪播圖路徑  $http({   method: 'post',   url: 'json/myJson.json'  }).then(function successCallback(response) {   $scope.imgSrcs = response.data.sites;  }, function errorCallback(response) {   // 請求失敗執行代碼  }); });

可是還是不行,注意:如果是在json中獲取數據,要把輪播js代碼寫在獲取數據中,因為他是先獲取數據才執行輪播的,如果你把他放在外部,實行輪播數據獲取不到。

所以解決方案便是將swiper的初始化方法放到$http請求里面執行,

將如下代碼加到function successCallback()方法里面即可實現輪播

var swiper = new Swiper('.swiper-container', {//重置輪播加載方法    pagination: '.swiper-pagination',    slidesPerView: 1,    paginationClickable: true,    spaceBetween: 30,    keyboardControl: true,    nextButton: '.swiper-button-next',    prevButton: '.swiper-button-prev',    observer:true,//修改swiper自己或子元素時,自動初始化swiper    observeParents:true//修改swiper的父元素時,自動初始化swiper   });

完整的js代碼如下:

var myapp=angular.module("myApp",[]); //輪播圖的控制器 myapp.controller("swiperController",function($scope,$http){  //請求輪播圖路徑  $http({   method: 'post',   url: 'json/myJson.json'  }).then(function successCallback(response) {   $scope.imgSrcs = response.data.sites;   var swiper = new Swiper('.swiper-container', {//重置輪播加載方法    pagination: '.swiper-pagination',    slidesPerView: 1,    paginationClickable: true,    spaceBetween: 30,    keyboardControl: true,    nextButton: '.swiper-button-next',    prevButton: '.swiper-button-prev',    observer:true,//修改swiper自己或子元素時,自動初始化swiper    observeParents:true//修改swiper的父元素時,自動初始化swiper   });  }, function errorCallback(response) {   // 請求失敗執行代碼  }); });

以上這篇解決Angular.js中使用Swiper插件不能滑動的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成都市| 清苑县| 绍兴市| 阳春市| 长子县| 南华县| 阿合奇县| 苗栗市| 阿拉尔市| 射洪县| 中卫市| 合水县| 阿城市| 防城港市| 道孚县| 万山特区| 元阳县| 子长县| 临安市| 华宁县| 教育| 定西市| 黄骅市| 松滋市| 类乌齐县| 双柏县| 永济市| 嘉兴市| 大足县| 班戈县| 奈曼旗| 崇礼县| 通道| 务川| 遂溪县| 平和县| 保靖县| 卢龙县| 利辛县| 嘉鱼县| 资中县|