I couldn’t find a carousel plugin for ionic framework which can create a lightbox that lets you swipe through images. So I made my own.
Template HTML1
<carousel images="images" show="show"></carousel>
Page Controller1
2
3
4
5
6
7
8
9
10
11function PageCtrl($s){
$s.show = false;
$s.images = [
'http://lorempixel.com/300/200/',
'http://lorempixel.com/300/200/food',
'http://lorempixel.com/300/200/nature',
'http://lorempixel.com/300/200/sports',
];
}
angular.module('controller',[])
.controller('PageCtrl', ['$scope', PageCtrl]);
To trigger carousel, you should bind a ng-click event somewhere to toggle show.
Directive HTML
I called it carousel.html .1
2
3
4
5
6
7
8
9
10
11
12
13
14<div class="carousel-container" ng-show="show">
<div class="carousel-close" ng-click="close()"><i class="ion-close"></i></div>
<div class="carousel">
<ul class="carousel-list" on-swipe-right="nextSlide()" on-swipe-left="prevSlide()">
<li class="carousel-image" ng-repeat="oneImage in images" style="{{imageStyle()}}">
<img ng-src="{{oneImage}}">
</li>
</ul>
<div class="carousel-arrow-left" ng-click="prevSlide();"><i class="ion-arrow-left-a"></i></div>
<div class="carousel-arrow-right" ng-click="nextSlide();"><i class="ion-arrow-right-a"></i></div>
</div>
</div>
SCSS1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68.carousel-container{
width: 100%;
position: fixed;
background-color: rgba(0,0,0,0.4);
z-index: 100;
height: 100%;
overflow-x:hidden;
overflow-y:hidden;
.carousel-close{
position: absolute;
right: 5px;
top: 5px;
color: white;
}
.carousel{
margin-top:33%;
position:relative;
min-height: 400px;
overflow: hidden;
margin-left: auto;
margin-right: auto;
ul{
list-style: none;
list-style-type: none;
padding-left: 0px;
position: absolute;
li {
float:left;
position: relative;
img {
width: 90%;
max-width: 90%;
margin-left: auto;
margin-right: auto;
display: block;
}
}
}
.carousel-arrow-left{
width: 20px;
height: 20px;
position: absolute;
top: 25%;
left: 10%;
position: absolute;
z-index: 102;
opacity: 0.5;
i {
font-size: 32px;
color: white;
}
}
.carousel-arrow-right{
width: 20px;
height: 20px;
top: 25%;
right: 10%;
position: absolute;
z-index: 102;
opacity: 0.5;
i {
font-size: 32px;
color: white;
}
}
}
}
Directive Js code
1 | function carousel() { |
In my code, I pulled a database row which contains images in the html.
I then had to bind ng-clicks to each image in the code.
1 | $s.content = databaseRow.text; |
When displaying content in the html, I used my dynamic directive to rebind the new images ng-click.
Code I added to carousel directive
1 | scope.$watch('images', function(images) { |
dynamic directive
1 | function dynamic ($c) { |
Hope this will help you out.