Search code examples
angularjsinitswiper.jslocked

Swiper inits locked


I'm trying to get multiple SWIPERS (idangero.us/swiper/) on the same page, I'm setting up a store and I want to display differents swipes for each category, I generated easily, the issue here is that the second swiper inits locked and for some reason after changin zoom it gets unlocked and I can swipe over it: Video with the issue: https://streamable.com/s/a88zy/jutbzh

The most curious thing is that I tried to replicate, but in the fiddle it works as expected: https://jsfiddle.net/g4b0_88/L9ph48ev/

Here the code where I'm generating swipers for each category:

$scope.loadSwiper = function(idcat){
$scope.swiperSliders[idcat-1] = new Swiper('.swiper'+$scope.store.categories[idcat-1].idcategory, {
direction: 'horizontal',
loop: false,
nextButton: '.button-next'+$scope.store.categories[idcat-1].idcategory,
prevButton: '.button-prev'+$scope.store.categories[idcat-1].idcategory,
slidesPerView: 'auto',
spaceBetween: 0,
freeMode: true
});
$scope.swiperSliders[idcat-1].unlockSwipes();
};

And here the HTML where I'm loading categories and products per category:

<div ng-repeat="category in store.categories" ng-repeat-end-watch="categoriesWatcher">
            <h1 class="category">{{category.name}}</h1>
                <div class="swiper-container swiper{{category.idcategory}}">
                    <div class="swiper-wrapper featured-carousel">
                        <div class="swiper-slide product-block" ng-repeat="product in store.featured_products[category.idcategory-1]">
                            <div id="note{{product.idproduct}}" class="add_product_note note{{product.idproduct}}" ng-show="product_in_cart.data[product.idproduct]"><i class="fa fa-pencil-square-o" ng-click="addProductNote($ev, product.idproduct)" aria-hidden="true"></i></div>
                            <div class="product-img">
                                <img class="center-block" ng-src="<?php echo site_url('/assets/product-img/'); ?>{{product.image}}"/>
                            </div>
                            <div class="product-name">
                                <h4>{{product.name}}</h4>
                            </div>
                            <div class="product-desc">
                                <h4>{{product.description}}</h4>
                            </div>
                            <div class="product-price">
                                <p>{{product.sell_price | currency}}</p>
                            </div>
                            <div class="product-add" ng-show="product.type == 2">
                                <ngcart-addtocart id="{{product.idproduct}}" name="{{product.name}}" price="{{product.sell_price}}" quantity="1" img="<?php echo site_url('assets/product-img/');?>{{product.image}}" ptype="{{product.type}}" note="">Agregar</ngcart-addtocart>
                            </div>
                            <div class="product-add" ng-show="product.type == 1">
                                <ngcart-addtocart id="{{product.idproduct}}" name="{{product.name}}" price="{{product.sell_price}}" quantity="0.1" img="<?php echo site_url('assets/product-img/');?>{{product.image}}" ptype="{{product.type}}" note="">Agregar</ngcart-addtocart>
                            </div>
                        </div>
                    </div>
                    <div class="swiper-button-prev button-prev{{category.idcategory}}"></div>
                    <div class="swiper-button-next button-next{{category.idcategory}}"></div>
                </div>
            </div>

As I said, the Swipers loads correctly but locked for some reason and gone unlocked when zooming-in or zooming-out.


Solution

  • I found the solution, instead of using $scope.swiperSliders[idcat-1].unlockSwipes(); I put the following code block:

    $timeout(function(){
     $scope.swiperSliders[idcat-1].onResize();
     $scope.swiperSliders[idcat-1].update(true); 
    }, 100);