forked from revolunet/angular-carousel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-panorama.js
107 lines (100 loc) · 4.11 KB
/
angular-panorama.js
1
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Angular Panorama - Mimic Windows Phone's Panorama UI control.
* @version v0.2.0 - 2014-03-18
* @link http://cnjsstong2.github.com/angular-panorama
* @author Tong Shen <tshen@farseerinc.com>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
angular.module('angular-panorama', ['ngTouch']);
angular.module('angular-panorama')
.directive('ngPanorama', ['$swipe', function ($swipe) {
return {
restrict: 'A',
scope: {
pages: "=ngPanorama",
i: "=ngPanoramaIndex",
bg: "=ngPanoramaBackgroundImage",
reset: "=ngPanoramaReset"
},
link: function (scope, el, attr) {
scope.$watch('bg', function (newValue) {
if (newValue) {
el.css('background-image', 'url(' + newValue + ')');
}
});
scope.$watch('i', function (newValue) {
setOffset(getOffsetByIndex(newValue));
});
scope.$watch('reset', function (newValue) {
ul.css(cruiseOff);
setOffset(0);
ul.css(cruiseOn);
});
el.addClass('ng-panorama-container');
var ul = el.find('ul');
ul.addClass('ng-panorama-slides');
if (!scope.i) {
scope.i = 0;
}
function getOffsetByIndex(index) {
var offset = 0;
var containerWidth = el.prop('offsetWidth');
for (var i = 0; i < index; i++) {
offset -= (scope.pages[i].width || 100) * containerWidth / 100;
}
if (index > 0) {
var fine = (100 - scope.pages[index].width) / 2 * containerWidth / 100;
offset += fine;
if (index == scope.pages.length - 1) {
offset += fine;
}
}
return parseInt(offset, 10);
}
function setOffset(offset) {
ul.css({
'-webkit-transform': 'translate3d(' + offset + 'px, 0, 0)',
'transform': 'translate3d(' + offset + 'px, 0, 0)'
});
}
var startCoords, startOffset;
var cruiseOn = {
'transition': 'all 0.2s ease',
'-webkit-transition': 'all 0.2s ease'
};
var cruiseOff = {
'transition': 'none',
'-webkit-transition': 'none'
};
$swipe.bind(el, {
start: function (coords) {
startCoords = coords;
startOffset = getOffsetByIndex(scope.i);
ul.css(cruiseOff);
},
move: function (coords) {
setOffset(startOffset + coords.x - startCoords.x);
},
end: function (coords) {
var targetIndex = scope.i;
var threshold = el.prop('offsetWidth') * 0.1;
var delta = coords.x - startCoords.x;
if (delta > threshold && targetIndex > 0) {
targetIndex--;
} else if (delta < -threshold && targetIndex < scope.pages.length - 1) {
targetIndex++;
}
scope.$apply(function () {
scope.i = targetIndex;
});
ul.css(cruiseOn);
setOffset(getOffsetByIndex(scope.i));
},
cancel: function (coords) {
ul.css(cruiseOn);
setOffset(getOffsetByIndex(scope.i));
}
});
}
};
}]);