-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.lazyLoadGoogleMaps.js
133 lines (112 loc) · 2.89 KB
/
jquery.lazyLoadGoogleMaps.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
/*
lazyLoadGoogleMaps v1.0.2
A JavaScript plugin for lazy-loading Google Maps.
-
By Osvaldas Valutis, www.osvaldas.info
Available for use under the MIT License
*/
window.googleMapsScriptLoaded = function()
{
$( window ).trigger( 'googleMapsScriptLoaded' );
};
;( function( $, window, document, undefined )
{
'use strict';
var $window = $( window ),
$body = $( 'body' ),
windowHeight = $window.height(),
windowScrollTop = 0,
debounce = function( delay, fn )
{
var timer = null;
return function()
{
var context = this, args = arguments;
clearTimeout( timer );
timer = setTimeout( function(){ fn.apply( context, args ); }, delay );
};
},
throttle = function( delay, fn )
{
var last, deferTimer;
return function()
{
var context = this, args = arguments, now = +new Date;
if( last && now < last + delay )
{
clearTimeout( deferTimer );
deferTimer = setTimeout( function(){ last = now; fn.apply( context, args ); }, delay );
}
else
{
last = now;
fn.apply( context, args );
}
};
},
apiScriptLoaded = false,
apiScriptLoading = false,
$containers = $([]),
init = function( callback )
{
windowScrollTop = $window.scrollTop();
$containers.each( function()
{
var $this = $( this ),
thisOptions = $this.data( 'options' );
if( $this.offset().top - windowScrollTop > windowHeight * 1 )
return true;
if( !apiScriptLoaded && !apiScriptLoading )
{
var apiArgs =
{
callback: 'googleMapsScriptLoaded'
};
if( thisOptions.key ) apiArgs.key = thisOptions.key;
if( thisOptions.libraries ) apiArgs.libraries = thisOptions.libraries;
if( thisOptions.language ) apiArgs.language = thisOptions.language;
if( thisOptions.region ) apiArgs.region = thisOptions.region;
$body.append( '<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&' + $.param( apiArgs ) + '"></script>' );
apiScriptLoading = true;
}
if( !apiScriptLoaded ) return true;
var map = new google.maps.Map( this, { zoom: 15 });
if( thisOptions.callback !== false )
thisOptions.callback( this, map );
$containers = $containers.not( $this );
});
};
$window.on( 'googleMapsScriptLoaded',function()
{
apiScriptLoaded = true;
init();
})
.on( 'scroll', throttle( 500, init ) )
.on( 'resize', debounce( 1000, function()
{
windowHeight = $window.height();
init();
}));
$.fn.lazyLoadGoogleMaps = function( options )
{
options = $.extend(
{
key: false,
libraries: false,
language: false,
region: false,
callback: false,
},
options );
this.each( function()
{
var $this = $( this );
$this.data( 'options', options );
$containers = $containers.add( $this );
});
init();
this.debounce = debounce;
this.throttle = throttle;
return this;
};
})( jQuery, window, document );