解决Angular.js中使用Swiper插件不能滑动的问题
网络编程 2021-07-04 17:33www.168986.cn编程入门
狼蚁网站SEO优化长沙网络推广就为大家分享一篇解决Angular.js中使用Swiper插件不能滑动的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧
我们都知道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 suessCallback(response) { $scope.imgSrcs = response.data.sites; }, function errorCallback(response) { // 请求失败执行代码 }); });
可是还是不行,注意:如果是在json中获取数据,要把轮播js代码写在获取数据中,因为他是先获取数据才执行轮播的,如果你把他放在外部,实行轮播数据获取不到。
所以解决方案便是将swiper的初始化方法放到$http请求里面执行,
将如下代码加到function suessCallback()方法里面即可实现轮播
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 suessCallback(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插件不能滑动的问题就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程