Today I will show you the process of creating photo slider with AngularJS and CSS3. The slider itself is not very complicated, but it will have a unique 3D effect when we turn the slides. During of the creation our gallery – we will use AngularJS v1.2. This framework will help us create the HTML markup and the turning slides mechanism. We will have two buttons to switch slides back and forth, as well as a small panel with thumbnails to switch immediately to a desired slide.
Step 1. HTML
First at all we have to prepare a proper header (with including all necessary resources):
index.html
02 | <html ng-app="example366"> |
04 | <meta charset="utf-8" /> |
05 | <meta name="author" content="Script Tutorials" /> |
06 | <title>Photo Gallery with AngularJS and CSS3 | Script Tutorials</title> |
07 | <meta name="description" content="Photo Gallery with AngularJS and CSS3 - demo page"> |
08 | <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> |
09 | <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" /> |
11 | <link href="css/style.css" rel="stylesheet" type="text/css" /> |
13 | <script src="js/jquery-2.0.3.min.js"></script> |
17 | <script src="js/app.js"></script> |
This is rather ordinary header: different meta infos, styles and javascripts. Now – the slider itself:
01 | <body ng-controller="MainCtrl"> |
06 | <div class="container slider"> |
08 | <img ng-repeat="photo in photos" class="slide" ng-swipe-right="showPrev()" ng-swipe-left="showNext()" ng-show="isActive($index)" ng-src="{{photo.src}}" /> |
10 | <a class="arrow prev" href="#" ng-click="showPrev()"></a> |
11 | <a class="arrow next" href="#" ng-click="showNext()"></a> |
14 | <li ng-repeat="photo in photos" ng-class="{'active':isActive($index)}"> |
15 | <img src="{{photo.src}}" alt="{{photo.desc}}" title="{{photo.desc}}" ng-click="showPhoto($index);" /> |
In the beginning, we indicated the main controller (for the <body>): MainCtrl. Then, to enumerate all the given photos, we used the following technique: ng-repeat=”photo in photos”. This allowed us to create multiple images and thumbnails for our navigation bar. To bind the onclick event, we used ‘ng-click’ event attribute. AngularJS also allows us to bind swipe actions (for mobile devices): ‘ng-swipe-left’ and ‘ng-swipe-right’.
Step 2. CSS
All styles are divided into three sections: styles for the slider and its slides, styles for the navigation arrows and styles for the thumbnail navigation bar:
css/style.css
006 | outline: medium none; |
013 | background-image: url("../images/prev.png"); |
016 | transition: all 0.2s linear 0s; |
019 | background-image: url("../images/next.png"); |
022 | transition: all 0.2s linear 0s; |
036 | padding: 1em 0 0.8em; |
044 | border: 5px solid #AAAAAA; |
047 | display: inline-block; |
054 | border: 5px solid #FFFFFF; |
060 | border: 15px solid #FFFFFF; |
066 | -webkit-perspective: 1000px; |
067 | -moz-perspective: 1000px; |
068 | -ms-perspective: 1000px; |
069 | -o-perspective: 1000px; |
071 | -webkit-transform-style: preserve-3d; |
072 | -moz-transform-style: preserve-3d; |
073 | -ms-transform-style: preserve-3d; |
074 | -o-transform-style: preserve-3d; |
075 | transform-style: preserve-3d; |
084 | -webkit-transition:1s linear all; |
085 | -moz-transition:1s linear all; |
086 | -o-transition:1s linear all; |
087 | transition:1s linear all; |
088 | -webkit-transform: rotateX(50deg) rotateY(30deg); |
089 | -moz-transform: rotateX(50deg) rotateY(30deg); |
090 | -ms-transform: rotateX(50deg) rotateY(30deg); |
091 | -o-transform: rotateX(50deg) rotateY(30deg); |
092 | transform: rotateX(50deg) rotateY(30deg); |
093 | -webkit-transform-origin: right top 0; |
094 | -moz-transform-origin: right top 0; |
095 | -ms-transform-origin: right top 0; |
096 | -o-transform-origin: right top 0; |
097 | transform-origin: right top 0; |
099 | .slide.ng-hide-add.ng-hide-add-active { |
102 | .slide.ng-hide-remove { |
103 | -webkit-transition:1s linear all; |
104 | -moz-transition:1s linear all; |
105 | -o-transition:1s linear all; |
106 | transition:1s linear all; |
107 | display:block!important; |
110 | .slide, .slide.ng-hide-remove.ng-hide-remove-active { |
In order to achieve this beautiful 3D transition effects between slides – we used CSS3 transition effects with rotateX, rotateY and preserve-3d for the parent element
Step 3. JavaScript
This is the main AngularJS application controller:
js/app.js
02 | angular.module('example366', ['ngAnimate', 'ngTouch']) |
03 | .controller('MainCtrl', function ($scope) { |
16 | $scope.isActive = function (index) { |
17 | return $scope._Index === index; |
20 | $scope.showPrev = function () { |
21 | $scope._Index = ($scope._Index > 0) ? --$scope._Index : $scope.photos.length - 1; |
24 | $scope.showNext = function () { |
25 | $scope._Index = ($scope._Index < $scope.photos.length - 1) ? ++$scope._Index : 0; |
28 | $scope.showPhoto = function (index) { |
29 | $scope._Index = index; |
In the beginning we prepared a collection of photos. After we will bind it into the HTML code. After this collection – several functions to manage with an active image.
Step 4. Images
All used images were taken from the Marc Driesenga’s Photostream at Flickr
[sociallocker]
[/sociallocker]
Conclusion
That’s all for today, thanks for your patient attention, and if you really like what we have done today – share it with all your friends in your social networks using the form below.