在此处查看轮播:Owl-carousel2
我遇到一个问题,每当将ng-repeat指令应用于轮播时,这些项目都是垂直堆叠而不是水平布局。
如果我不使用ng-repeat并使用静态项目,则它应能正常工作。
有没有我可以写的指令并适用于猫头鹰轮播以保持布局?
另外,引起轮播破裂的ng-repeat是什么?
是否以某种方式剥离了应用于旋转木马的猫头鹰旋转木马类?
注意*如果手动构建列表,则使用遍历并追加元素:
var div = document.createElement('div'); var anchor = document.createElement('a'); var img = document.createElement('img'); ..... carousel.appendChild(div);
然后调用owl.owlCarousel({..})。它可以正常工作,不确定是否是最好的解决方法,因为ng-repeat使一切变得容易。
我发现了一个hack,如果我将猫头鹰init封装在一个超时中,那么ng-repat可以工作。
setTimeout(function(){ ...call owl init now },1000);
<link rel="stylesheet" href="css/owl.carousel.css"/> <link rel="stylesheet" href="css/owl.theme.default.min.css"/> ..... <script src="/js/lib/owl.carousel.min.js"></script> <script> $(document).ready(function() { var owl = $('.owl-carousel'); owl.owlCarousel({ ..... }); owl.on('mousewheel', '.owl-stage', function(e) { if (e.deltaY > 0) { owl.trigger('next.owl'); } else { owl.trigger('prev.owl'); } e.preventDefault(); }); }) </script>
能够从另一篇文章的DTing修改指令,以使其与同一页面上的多个轮播一起使用。这是一个工作的plnkr
-编辑- 让另一个plnkr给出如何添加项目的示例。执行reinit()不起作用会导致猫头鹰传送带被破坏,这会丢失数据元素,并且永远无法再次初始化。
var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.items1 = [1,2,3,4,5]; $scope.items2 = [1,2,3,4,5,6,7,8,9,10]; }).directive("owlCarousel", function() { return { restrict: 'E', transclude: false, link: function (scope) { scope.initCarousel = function(element) { // provide any default options you want var defaultOptions = { }; var customOptions = scope.$eval($(element).attr('data-options')); // combine the two options objects for(var key in customOptions) { defaultOptions[key] = customOptions[key]; } // init carousel $(element).owlCarousel(defaultOptions); }; } }; }) .directive('owlCarouselItem', [function() { return { restrict: 'A', transclude: false, link: function(scope, element) { // wait for the last item in the ng-repeat then call init if(scope.$last) { scope.initCarousel(element.parent()); } } }; }]);
这是HTML
<!DOCTYPE html> <html ng-app="plunker"> <head> <meta charset="utf-8" /> <title>AngularJS Plunker</title> <script>document.write('<base href="' + document.location + '" />');</script> <link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.transitions.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js" /> <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></script> <script data-require="jquery@2.1.3" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script> <script src="app.js"></script> </head> <body ng-controller="MainCtrl"> <data-owl-carousel class="owl-carousel" data-options="{navigation: true, pagination: false, rewindNav : false}"> <div owl-carousel-item="" ng-repeat="item in ::items1" class="item"> <p>{{::item}}</p> </div> </data-owl-carousel> <data-owl-carousel class="owl-carousel" data-options="{navigation: false, pagination: true, rewindNav : false}"> <div owl-carousel-item="" ng-repeat="item in ::items2" class="item"> <p>{{::item}}</p> </div> </data-owl-carousel> </body> </html>