-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS1.js
More file actions
175 lines (151 loc) · 5.96 KB
/
S1.js
File metadata and controls
175 lines (151 loc) · 5.96 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(function ($) {
$.fn.sliderVideo = function (options) {
options = $.extend({
scrollerSelector: '.slider-video-scroll',
itemSelector: '.slider-video-item',
aspectSelect: '.slider-video-aspect',
navLeftSelector: '.slider-video-left',
navRightSelecto: '.slider-video-right',
scrollTime: 500,
animationTime: 300,
visibleItems: 2,
itemsGap: 10,
aspectRation: 0.625
}, options);
var $slider = $(this);
var $scroller = $slider.find(options.scrollerSelector);
var $aspect = $slider.find(options.aspectSelect);
var $items = $slider.find(options.itemSelector);
var $navLeft = $slider.find(options.navLeftSelector);
var $navRight = $slider.find(options.navRightSelecto);
$items.bind('click', onItemClick);
$navLeft.bind('click', onNavLeftClick);
$navRight.bind('click', onNavRightClick);
$slider.bind('scrollToIndex', function (event, index) {
scrollToIndex(index);
});
initializeSize();
initializeOrigin();
initializeNavigation();
function initializeSize() {
$aspect.height($aspect.width() * options.aspectRation);
$scroller.width((50 * $items.length) + '%');
var itemWidth = $aspect.width() / options.visibleItems;
var itemHeight = itemWidth * options.aspectRation;
var positionTop = ($aspect.height() - itemHeight) / 2;
$items.each(function (i) {
$(this).css({
top: positionTop,
left: i * (itemWidth + options.itemsGap),
width: itemWidth - options.itemsGap + 'px',
height: itemHeight + 'px'
})
});
}
function initializeOrigin() {
var aspectScrollLeft = $aspect.scrollLeft()
$items.each(function (i) {
var $item = $(this);
if ($item.position().left > aspectScrollLeft) {
$item.css({
'transform-origin': '100% 50%'
});
} else {
$item.css({
'transform-origin': '0 50%'
});
}
})
}
function initializeNavigation() {
var visibleItems = getVisibleItems();
$navLeft.toggle(visibleItems.first().prev().length > 0);
$navRight.toggle(visibleItems.last().next().length > 0);
}
function onItemClick() {
var $item = $(this);
if ($item.is(".active")) {
$item.removeClass('active');
toggleVideo($item, false);
initializeSize();
$item.addClass('active-out');
setTimeout(function () {
$item.removeClass('active-out');
}, options.animationTime)
}
else {
$item.css({
width: $aspect.width(),
height: $aspect.height(),
left: $aspect.scrollLeft(),
top: 0
});
$item.addClass('active');
setTimeout(function () {
toggleVideo($item, true);
}, 300)
}
}
function onNavRightClick() {
scrollTo(1);
}
function onNavLeftClick() {
scrollTo(-1);
}
function toggleVideo($item, status) {
$item.find('.slider-video-pic').toggle(!status);
$item.find('.slider-video-play').toggle(status);
var frame = $item.find('iframe')[0];
frame.contentWindow.postMessage('{"event":"command","func":"' + (status ? 'playVideo' : 'pauseVideo') + '","args":""}', '*')
}
function scrollTo(direction) {
if ($items.is('.active')) {
var $active = $('.slider-video-item').filter('.active');
var $next = direction > 0 ? $active.next() : $active.prev();
if ($next.length) {
scrollToIndex($next.index());
$active.trigger('click');
setTimeout(function () {
$next.trigger('click');
initializeNavigation();
}, options.scrollTime)
}
} else {
var visibleItems = getVisibleItems();
if (direction > 0) {
scrollToIndex(visibleItems.last().next().index());
} else {
scrollToIndex(visibleItems.first().prev().index());
}
}
}
function scrollToIndex(index) {
var $item = $items.eq(index);
if ($item.length) {
var itemWidth = $aspect.width() / options.visibleItems;
var itemLeft = $item.position().left;
var targetPosition = null;
if (itemLeft > $aspect.scrollLeft() + $aspect.width()) {
targetPosition = itemLeft - itemWidth - options.itemsGap;
} else if (itemLeft < $aspect.scrollLeft()) {
targetPosition = itemLeft;
}
if (targetPosition != null) {
$aspect.animate({ scrollLeft: targetPosition + 'px' }, options.scrollTime, function () {
initializeOrigin();
initializeNavigation();
});
}
}
}
function getVisibleItems() {
return $items.filter(function (i) {
var posLeft = $(this).position().left;
return posLeft >= $aspect.scrollLeft() && posLeft < $aspect.scrollLeft() + $aspect.width();
});
}
};
})(jQuery);
$(function() {
$('.slider-video').sliderVideo();
})