/
jquery-addToHome.js
70 lines (63 loc) · 1.96 KB
/
jquery-addToHome.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
(function ($) {
$.fn.showAddToHome = function (oOptions) {
// This is the easiest way to have default options.
var iWidth = $(window).width(),
iTriangleDiameter = oOptions ? oOptions.triangleDiameter || 20 : 20,
iHalfDiameter = iTriangleDiameter / 2,
oSettings = $.extend(true, {
arrowStyles: {
borderLeft: iHalfDiameter + 'px solid transparent',
borderRight: iHalfDiameter + 'px solid transparent',
borderTop: iHalfDiameter + 'px solid black',
bottom: '0',
height: 0,
position: 'fixed',
width: 0
},
elStyles: {
// These are the defaults.
backgroundColor: 'black',
borderRadius: '10px',
bottom: iHalfDiameter + 'px',
color: 'white',
position: 'fixed',
width: '200px',
zIndex: 1000
},
pStyles: {
margin: 0,
padding: '0.5em',
textAlign: 'center'
},
html: '<p>Add To Home</p>'
}, oOptions),
el, elArrow, elBody, iMargin;
if (window.navigator.standalone === false) {
elBody = $('body');
el = $(document.createElement('div')).appendTo(elBody);
el.html(oSettings.html);
el.css(oSettings.elStyles);
el.find('p').css(oSettings.pStyles);
elArrow = $(document.createElement('div')).appendTo(elBody).css(
oSettings.arrowStyles);
// center the message
iMargin = (iWidth - el.width()) / 2;
el.css({
marginLeft: iMargin + 'px',
marginRight: iMargin + 'px'
});
// center the arrow
iMargin = (iWidth - iTriangleDiameter) / 2;
elArrow.css({
marginLeft: iMargin + 'px',
marginRight: iMargin + 'px'
});
function dismissalCallback(e) {
el.remove();
elArrow.remove();
$(this).off('click', dismissalCallback).off('touch', dismissalCallback);
}
$(document).on('click', dismissalCallback).on('touch', dismissalCallback);
}
}
}(jQuery));