-
Notifications
You must be signed in to change notification settings - Fork 5
/
duplicity.js
99 lines (97 loc) · 2.74 KB
/
duplicity.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
// establish vars so they are global
var duplicity = {
phone: false,
tablet: false,
desktop: false
};
// checks if screen size change has changed enough to qualify a new view
function detectScreenSizeChange(forceUpdate){
// set default value
if (forceUpdate === undefined){
forceUpdate = false;
}
// get old screen size
var oldDevice;
if (duplicity.phone){
oldDevice = "phone";
}
else if (duplicity.tablet){
oldDevice = "tablet";
}
else if (duplicity.desktop){
oldDevice = "desktop";
}
else {
// first time defining device, so no previous definition
oldDevice = null;
}
// get new screen size
var screenSize = $(window).width();
var newDevice;
if (screenSize < 640){
newDevice = "phone";
}
else if (screenSize >= 640 && screenSize <= 1024){
newDevice = "tablet";
}
else {
newDevice = "desktop";
}
// if they are not the same call the affect function to change document to respond to new device
if ((oldDevice != newDevice) || forceUpdate){
affectScreenSizeChange(oldDevice, newDevice);
}
// if there was a legitamate change of device (and this wasn't being called for the first time), alert custom user function if exists
try{
if (oldDevice != null && oldDevice != newDevice){
screenSizeChanged(oldDevice, newDevice);
}
}
catch(err){
// do nothing
}
}
// changes document to repond to change in screen size
function affectScreenSizeChange(from, to){
// reset document class
$("body").removeClass("phone tablet desktop");
// reset HTML elements
$("[data-display]").css("display", "none");
// reset JS vars
duplicity.phone = false;
duplicity.tablet = false;
duplicity.desktop = false;
// change document
if (to == "phone"){
// js vars
duplicity.phone = true;
// add css class
$("body").addClass("phone");
// show html elements
$("[data-display~='phone']").css("display", "");
}
else if (to == "tablet"){
// js vars
duplicity.tablet = true;
// add css class
$("body").addClass("tablet");
// show html elements
$("[data-display~='tablet']").css("display", "");
}
else {
// js vars
duplicity.desktop = true;
// add css class
$("body").addClass("desktop");
// show html elements
$("[data-display~='desktop']").css("display", "");
}
}
// call function
detectScreenSizeChange();
// on full load, call function
$(document).ready(function(){
detectScreenSizeChange(true);
});
// call function whenever user resizes
$(window).resize(detectScreenSizeChange);