Multi-item responsive carousel

前端 未结 2 956
无人共我
无人共我 2021-02-04 02:05

I\'m building a website that requires a carousel to be implemented. Because this website is built on AngularJS I wanted to go with Angulars Boostrap Carousel, h

2条回答
  •  滥情空心
    2021-02-04 02:44

    ui-bootstrap's carousel is not a good choice, it has other drawback like isolated scope on each slide. I'm using https://github.com/revolunet/angular-carousel which support multi item on each slide.

    Because this directive support ng-repeat. You easy change you collection and using nested ng-repeat to set different number of items in each slide.

    • {{ image }}

    As you have already defined 3 break points. We just need to reconstruct the imageCollection array when viewport size changed.

    $window.on('resize', function() {
        var width = $window.width();
        if(width > 900) {
           // desktop
           rebuildSlide(3);
        } else if(width <= 900 && width > 480) {
           // tablet
           rebuildSlide(2);
        } else {
           // phone
           rebuildSlide(1);
        }
        // don't forget manually trigger $digest()
        $scope.$digest();
    });
    
    function rebuildSlide(n) {
       var imageCollection = [],
           slide = [],
           index;
       // values is your actual data collection.
       for(index = 0; index < values.length; index++) {
           if(slide.length === n) {
               imageCollection.push(slide);
               slide = [];
           }
           slide.push(values[index]);
       }
       imageCollection.push(slide);
       $scope.imageCollection = imageCollection;
    }
    

提交回复
热议问题