-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartBg.js
256 lines (218 loc) · 10.8 KB
/
smartBg.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
(function ($) {
var that = this;
this.smartPrivate = {
containerBg: {},
parentContainer: {},
containerClass: 'smartBg-container',
arrowClassAll: 'arrowBackgroundGame',
options: {
imgBg: '',
showArrow: false,
keepVisible: true
},
init: function (parent) {
this.parentContainer = parent;
this.parentContainer.append($('<div class="subgame-backgroundImagesubgame-backgroundImage"></div>'));
this.containerBg = $('.subgame-backgroundImagesubgame-backgroundImage');
this.containerBg.addClass(this.containerClass);
var background = new Image();
background.onload = function () {
var bgWidth = this.width;
var bgHeight = this.height;
that.smartPrivate.buildBackground(bgWidth, bgHeight);
$(window).resize(function () {
$('.' + that.smartPrivate.arrowClassAll).remove();
that.smartPrivate.buildBackground(bgWidth, bgHeight);
});
};
background.src = this.options.imgBg;
},
buildBackground: function (bgWidth, bgHeight) {
$('.' + that.smartPrivate.arrowClassAll).unbind('click');
if (this.options.showArrow) {
that.smartPrivate.loadBackground(bgWidth, bgHeight);
} else {
//loadBackgroundsNoMarging(bgWidth, bgHeight, imageUrl);
}
//add this in the controller
//$scope.$on("$destroy", function (event) {
// jQuery(window).unbind("resize");
//});
},
loadBackground: function (imgWidth, imgHeight) {
var parentElement = that.smartPrivate.parentContainer;
var parentHeight = parentElement.height();
var parentWidth = parentElement.width();
var bgRatio = imgWidth / imgHeight;
var imageUrl = that.smartPrivate.options.imgBg;
that.smartPrivate.containerBg.height(imgHeight);
that.smartPrivate.containerBg.width(imgWidth);
that.smartPrivate.containerBg.css(
'background-image',
'url(' + imageUrl + ')'
);
that.smartPrivate.containerBg.fadeIn('slow');
that.smartPrivate.containerBg.attr('data-position', 'center');
if (parentElement[0].offsetHeight < parentElement[0].scrollHeight || parentElement[0].offsetWidth < parentElement[0].scrollWidth) {
if (bgRatio >= 1.35) {
that.smartPrivate.setLandscape(imgWidth, imgHeight, parentHeight, parentWidth);
} else {
that.smartPrivate.setPortrait(imgWidth, imgHeight, parentHeight, parentWidth);
}
} else {
that.smartPrivate.setPortrait(imgWidth, imgHeight, parentHeight, parentWidth);
}
if (that.smartPrivate.options.keepVisible) {
// Show arrows when is clicked the background
$('.' + that.smartPrivate.arrowClassAll).css('opacity', '0');
that.smartPrivate.parentContainer.on('click', that.smartPrivate.attachBackgroundEvent);
$('.' + that.smartPrivate.arrowClassAll).hover(function () {
$('.' + that.smartPrivate.arrowClassAll).css('opacity', '1');
}, function () {
var timer = undefined;
timer = setTimeout(function () {
// Remove opacity style for prevents problems with :hover
$('.' + that.smartPrivate.arrowClassAll).css('opacity', '0');
clearTimeout(timer);
}, 2000);
});
}
},
attachBackgroundEvent: function (event) {
// Get the element clicked
var targetElement = $(event.originalEvent.target);
if (targetElement.hasClass(that.smartPrivate.containerClass)) {
var timer = undefined;
$('.' + that.smartPrivate.arrowClassAll).css('opacity', '1');
timer = setTimeout(function () {
// Remove opacity style for prevents problems with :hover
$('.' + that.smartPrivate.arrowClassAll).css('opacity', '0');
clearTimeout(timer);
}, 3000);
} else {
var timer = undefined;
timer = setTimeout(function () {
// Remove opacity style for prevents problems with :hover
$('.' + that.smartPrivate.arrowClassAll).css('opacity', '0');
clearTimeout(timer);
}, 3000);
}
},
setPortrait: function(imgWidth, imgHeight, parentHeight, parentWidth) {
var ratio = imgWidth / parentWidth;
var newHeightImage = imgHeight / ratio;
if (newHeightImage >= parentHeight) {//in mobile view this image has to be bigger than the container
that.smartPrivate.containerBg.height(newHeightImage);
that.smartPrivate.containerBg.width(parentWidth);
that.smartPrivate.containerBg.css(
'background-size',
parentWidth + 'px ' + newHeightImage + 'px'
);
//center image, half of image minues half of container
var moveToCenter = (newHeightImage / 2) - (parentHeight / 2);
that.smartPrivate.containerBg.css('top', '-' + moveToCenter + 'px');
that.smartPrivate.containerBg.css('left', '0px');
if ($('.arrowBackgroundGame-up').length == 0 && $('.arrowBackgroundGame-down').length == 0) {
that.smartPrivate.parentContainer.append(
'<span class="glyphicon glyphicon-chevron-up arrowBackgroundGame arrowBackgroundGame-up" data-type="up"></span>' +
'<span class="glyphicon glyphicon-chevron-down arrowBackgroundGame arrowBackgroundGame-down" data-type="down"></span>'
);
}
that.smartPrivate.attachEvent(parentHeight, parentWidth, moveToCenter);
} else {
that.smartPrivate.setLandscape(imgWidth, imgHeight, parentHeight, parentWidth);
}
},
setLandscape: function (imgWidth, imgHeight, parentHeight, parentWidth) {
var ratio = imgHeight / parentHeight;
var newWidthImage = imgWidth / ratio;
if (newWidthImage >= parentWidth) {//in mobile view this image has to be bigger than the container
that.smartPrivate.containerBg.height(parentHeight);
that.smartPrivate.containerBg.width(newWidthImage);
that.smartPrivate.containerBg.css(
'background-size',
newWidthImage + 'px ' + parentHeight + 'px');
//center image, half of image minues half of container
var moveToCenter = (newWidthImage / 2) - (parentWidth / 2);
that.smartPrivate.containerBg.css('left', '-' + moveToCenter + 'px');
that.smartPrivate.containerBg.css('top', '0px');
//after redimension the image to fit the contanier we can check if the width are bigger than container. this happens with image 4400/3500
if (that.smartPrivate.containerBg.width() < parentWidth) {
that.smartPrivate.containerBg.addClass('overContent-centerAll');
} else {
if ($('.arrowBackgroundGame-left').length == 0 && $('.arrowBackgroundGame-right').length == 0) {
that.smartPrivate.parentContainer.append(
'<span class="glyphicon glyphicon-chevron-left arrowBackgroundGame arrowBackgroundGame-left" data-type="left"></span>' +
'<span class="glyphicon glyphicon-chevron-right arrowBackgroundGame arrowBackgroundGame-right" data-type="right"></span>'
);
}
}
that.smartPrivate.attachEvent(parentHeight, parentWidth, moveToCenter);
} else {
that.smartPrivate.setPortrait(imgWidth, imgHeight, parentHeight, parentWidth);
}
},
attachEvent: function (parentHeight, parentWidth, center) {
$('.' + that.smartPrivate.arrowClassAll).on('click', function () {
var imageBg = {
top: parseInt(that.smartPrivate.containerBg.css('top'), 10),
left: parseInt(that.smartPrivate.containerBg.css('left'), 10),
height: that.smartPrivate.containerBg.height(),
width: that.smartPrivate.containerBg.width(),
position: that.smartPrivate.containerBg.attr('data-position')
};
switch ($(this).attr('data-type')) {
case 'up':
if (imageBg.position == 'center') {
that.smartPrivate.containerBg.css('top', '0%');
that.smartPrivate.containerBg.attr('data-position', 'top');
}
if (imageBg.position == 'bottom') {
that.smartPrivate.containerBg.css('top', '-' + center + 'px');
that.smartPrivate.containerBg.css('bottom', '');
that.smartPrivate.containerBg.attr('data-position', 'center');
}
break;
case 'down':
if (imageBg.position == 'center') {
that.smartPrivate.containerBg.css('top', '');
that.smartPrivate.containerBg.css('bottom', '0%');
that.smartPrivate.containerBg.attr('data-position', 'bottom');
}
if (imageBg.position == 'top') {
that.smartPrivate.containerBg.css('top', '-' + center + 'px');
that.smartPrivate.containerBg.attr('data-position', 'center');
}
break;
case 'left':
if (imageBg.position == 'center') {
that.smartPrivate.containerBg.css('left', '0%');
that.smartPrivate.containerBg.attr('data-position', 'left');
}
if (imageBg.position == 'right') {
that.smartPrivate.containerBg.css('left', '-' + center + 'px');
that.smartPrivate.containerBg.attr('right', '');
that.smartPrivate.containerBg.attr('data-position', 'center');
}
break;
case 'right':
if (imageBg.position == 'center') {
that.smartPrivate.containerBg.css('left', '');
that.smartPrivate.containerBg.css('right', '0%');
that.smartPrivate.containerBg.attr('data-position', 'right');
}
if (imageBg.position == 'left') {
that.smartPrivate.containerBg.css('left', '-' + center + 'px');
that.smartPrivate.containerBg.attr('data-position', 'center');
}
break;
}
});
}
}
$.fn.smartBg = function (options) {
that.smartPrivate.options = $.extend(that.smartPrivate.options, options);
that.smartPrivate.init(this);
return this;
};
}(jQuery));