forked from mrjoelkemp/assets-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picturefill.js
37 lines (31 loc) · 985 Bytes
/
picturefill.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
define(['jquery'], function($) {
'use strict';
var mm = window.matchMedia || window.msMatchMedia,
mkimg = function() {
var $this = $(this), $source = $this.find('div[data-src]');
if (!$source.length) {
$this.find('img').remove();
return;
}
if (mm) {
$source = $source.first().add($source.filter(function() {
var m = $(this).data('media');
return m && mm(m).matches;
})).last();
}
// using attr here because .data() doesn't add the attribute
// and we want to select based on a Selectors API compliant selector
$this.attr('data-rendered', 'rendered');
$('<img>', {
alt: $this.data('alt'),
src: $source.data('src'),
'class': $source.data('class'),
title: $source.data('title'),
'data-pin-nopin': 'pin'
}).appendTo($this);
};
return ($.fn.picturefill = function() {
this.find('div[data-picture]:not([data-rendered])').each(mkimg);
return this;
});
});