-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsive.php
267 lines (249 loc) · 8.14 KB
/
responsive.php
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
257
258
259
260
261
262
263
264
265
266
267
<script>
// remap jQuery to $
(function($){
customApp.breakpointsInit = function(options)
{
//object for organizing code
var breakpointsInit = new Object();
//default options
var defaults = {
desktopMaxWidth: 1200,
tabletLandscapeMaxWidth: 1024,
tabletMaxWidth: 768,
phoneLandscapeMaxWidth: 480,
phoneMaxWidth: 320
}
//buid settings based on defaults and options
var settings = $.extend({}, defaults, options);
breakpointsInit.setup = function()
{
customApp.viewportWidth = $(window).width();
customApp.viewportHeight = $(window).height();
customApp.breakPoints = {
desktopWide: {
maxWidth: 1000000,
next: false
},
desktop: {
maxWidth: settings.desktopMaxWidth,
next: 'desktopWide'
},
tabletLandscape: {
maxWidth: settings.tabletLandscapeMaxWidth,
next: 'desktop'
},
tablet: {
maxWidth: settings.tabletMaxWidth,
next: 'tabletLandscape'
},
phoneLandscape: {
maxWidth: settings.phoneLandscapeMaxWidth,
next: 'tablet'
},
phone: {
maxWidth: settings.phoneMaxWidth,
next: 'phoneLandscape'
}
}
for (var breakpoint in customApp.breakPoints) {
if(customApp.viewportWidth <= customApp.breakPoints[breakpoint].maxWidth){
customApp.currentView = breakpoint;
customApp.loadedView = breakpoint;
}
}
breakpointsInit.resize()
}
breakpointsInit.checkBreakpointLoaded = function()
{
for (var breakpoint in customApp.breakPoints) {
if(customApp.viewportWidth <= customApp.breakPoints[breakpoint].maxWidth){
customApp.currentView = breakpoint;
}
}
if($('.ie8').length){
customApp.currentView = 'desktop';
}
}
breakpointsInit.resize = function()
{
$(window).resize(function() {
customApp.viewportWidth = $(window).width();
customApp.viewportHeight = $(window).height();
customApp.breakpointsInitObj.checkBreakpointLoaded();
if(customApp.currentView != customApp.loadedView) {
customApp.loadedView = customApp.currentView;
customApp.breakpointsViewChange();
}
});
}
breakpointsInit.setup();
customApp.breakpointsInitObj = breakpointsInit;
}
customApp.breakpointsInit();
customApp.breakpointsViewChange = function()
{
//use this function to perform any changes to the view based on breakpoint changes
//change responsive images
//customApp.breakpointImagesObj.emptySrc();
//customApp.breakpointImages();
}
customApp.breakpointImages = function(callback)
{
//object for organizing code
var breakpointImages = new Object();
breakpointImages.setup = function()
{
//load first priority images
$("img[responsiveImg='1']").each(function(){
var me = $(this);
breakpointImages.loadImages(me)
});
//run callback after first priority images are loaded
if(callback){
callback();
}
//load second priority images
$("img[responsiveImg='2']").each(function(){
var me = $(this);
breakpointImages.loadImages(me)
});
}
breakpointImages.loadImages = function($image)
{
var srcAttr = 'src-'+customApp.currentView;
var src = $image.attr(srcAttr);
var currentSrc = $image.attr('src');
//loop through all breakpoints to show the smallest image that is bigger than the breakpoint
var loopCount = 0;
var nextView = customApp.breakPoints[customApp.currentView].next;
while(!src && nextView != false){
//prevent an infinite loop
if(loopCount > 10){
break;
}
srcAttr = 'src-'+nextView;
src = $image.attr(srcAttr);
nextView = customApp.breakPoints[nextView].next;
loopCount++;
}
//set the source if it is different from current and if one was found
if(src && src != currentSrc){
$image.attr('src',src);
}
}
breakpointImages.emptySrc = function()
{
$("img[responsiveImg='1'], img[responsiveImg='2']").each(function(){
var me = $(this);
me.attr('src', '');
});
}
breakpointImages.setup();
customApp.breakpointImagesObj = breakpointImages;
}
customApp.setFullpageElement = function(options)
{
//object for organizing calculations
var sfe = new Object();
//default options
var defaults = {
contentElement: false, //the element which should fill the page
wrapperElement: false, //the wrapper element, note this should have overflow hidden
verticalPaddingSelectors: false, //comma separated list of selectors that should reduce the vertical height of the full page element
debug: false
}
//buid settings based on defaults and options
var settings = $.extend({}, defaults, options);
//remove old styles from element or wrapper
settings.wrapperElement.add(settings.contentElement).removeAttr('style');
//find the vertical padding that needs to reduce the maxHeight
sfe.verticalPadding = 0;
if(settings.verticalPaddingSelectors){
var verticalPaddingElements = $(settings.verticalPaddingSelectors);
verticalPaddingElements.each(function(){
var me = $(this);
var outerHeight = me.outerHeight();
sfe.verticalPadding = sfe.verticalPadding + outerHeight;
});
}
//set all variables
sfe.contentHeight = settings.contentElement.outerHeight();
sfe.contentWidth = settings.contentElement.outerWidth();
sfe.viewportWidth = $(window).width();
sfe.viewportHeight = $(window).height();
sfe.maxHeight = sfe.viewportHeight - sfe.verticalPadding;
sfe.maxWidth = sfe.viewportWidth;
sfe.scaleHeight = (sfe.contentHeight * sfe.maxWidth) / sfe.contentWidth; //scale the height while keeping the width at max width
sfe.scaleWidth = (sfe.contentWidth * sfe.maxHeight) / sfe.contentHeight; //scale the width while keeping the height at max height
//if the scale width is greater than the max width, then we need to crop the sides
sfe.scaleOnWidth = (sfe.scaleWidth > sfe.maxWidth);
if (sfe.scaleOnWidth) {
sfe.width = sfe.scaleWidth;
sfe.height = sfe.maxHeight;
sfe.crop = 'sides';
} else {
sfe.width = sfe.maxWidth;
sfe.height = sfe.scaleHeight;
sfe.crop = 'top';
}
sfe.marginLeft = (sfe.maxWidth - sfe.width) / 2;
sfe.marginTop = (sfe.maxHeight - sfe.height) / 2;
if(settings.debug){
console.log(' sfe.maxWidth '+sfe.maxWidth+' sfe.maxHeight '+sfe.maxHeight);
console.log(' sfe.scaleWidth '+sfe.scaleWidth+' sfe.scaleHeight '+sfe.scaleHeight);
console.log(sfe);
}
if(sfe.crop == 'top') {
//now resize the image relative to the ratio
settings.contentElement.attr('crop', 'top')
.css({
'margin-top': sfe.marginTop,
'height': sfe.height,
'width': sfe.width,
'min-width': '100%',
'max-width': '100%',
'min-height': 'none',
'max-height': 'none'
});
settings.wrapperElement.css('height', sfe.maxHeight);
} else {
settings.contentElement .attr('crop', 'sides')
.css({
'margin-left': sfe.marginLeft,
'height': sfe.height,
'width': sfe.width,
'min-width': 'none',
'max-width': 'none',
'min-height': '100%',
'max-height': '100%'
});
settings.wrapperElement.css('height', sfe.maxHeight);
}
}
})(window.jQuery);
</script>
<!-- start html for responsive img, IMPORTANT! img-desktopWide is the fallback -->
<!-- responsiveImg = 1 for high priority image -->
<img
src=''
alt=''
responsiveImg='1'
src-phone=''
src-phoneLandscape=''
src-tablet=''
src-tabletLandscape=''
src-desktop=''
src-desktopWide=''
/>
<!-- responsiveImg = 2 for low priority image -->
<img
src=''
alt=''
responsiveImg='2'
src-phone=''
src-phoneLandscape=''
src-tablet=''
src-tabletLandscape=''
src-desktop=''
src-desktopWide=''
/>