-
Notifications
You must be signed in to change notification settings - Fork 0
/
garfield.js
113 lines (85 loc) · 2.89 KB
/
garfield.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
/* ========================================================================
* Garfield: garfield.js 0.1.2
* http://www.github.com/andrezimpel/garfield
* ========================================================================
* Copyright 2017 Andre Zimpel
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// GARFIELD CLASS DEFINITION
// ======================
var Garfield = function (element, options) {
this.options = options
this.$body = $(document.body)
this.$element = $(element)
this.loaded = false
}
Garfield.VERSION = '0.1.2'
Garfield.DEFAULTS = {
'offsets': {
top: 400,
bottom: 400
},
dataSrcSetAttribute: 'data-srcset',
dataSrcAttribute: 'data-src',
unloadedCssClass: 'garfield',
loadedCssClass: 'garfielded'
}
Garfield.prototype.prepare = function () {
var options = this.options
var $element = this.$element
var $observer = $element
var that = this
var $parent = $element.parent()[0]
// check if the parent is within the picture element - this would prevent scrollMonitor from detecting propperly
if ($parent.tagName == 'PICTURE') {
$observer = $parent
}
// install watcher
var elementWatcher = scrollMonitor.create($observer, this.options.offsets);
elementWatcher.enterViewport(function() {
if (that.loaded === false) {
that.loadImage()
}
});
}
Garfield.prototype.loadImage = function () {
var options = this.options;
var $element = this.$element
var src = $element.attr(options.dataSrcAttribute)
var srcSet = $element.attr(options.dataSrcSetAttribute)
$element.attr('src', src)
$element.attr('srcset', srcSet)
this.loaded = true
}
// GARFIELD PLUGIN DEFINITION
// =======================
function Plugin(option, data) {
return this.each(function () {
var $this = $(this)
var data = $this.data('garfield')
var options = $.extend({}, Garfield.DEFAULTS, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('garfield', (data = new Garfield(this, options)))
data.prepare()
})
}
var old = $.fn.garfield
$.fn.garfield = Plugin
$.fn.garfield.Constructor = Garfield
// GARFIELD NO CONFLICT
// =================
$.fn.garfield.noConflict = function () {
$.fn.garfield = old
return this
}
// GARFIELD DATA-API
// ==============
$(document).ready(function () {
// add support for data-src & data-srcset
$('[' + Garfield.DEFAULTS.dataSrcAttribute + '], [' + Garfield.DEFAULTS.dataSrcSetAttribute + ']').each(function(){
var $this = $(this)
Plugin.call($this, $this.data())
})
})
}(jQuery);