Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add srcset 'w' support using a data- attribute #661

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions jquery.colorbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,16 @@
return this.rel;
},
href: function() {
// using this.href would give the absolute url, when the href may have been inteded as a selector (e.g. '#container')
// Using this.href would always give the absolute url, but the href may be
// intended as an in-page selector (e.g. '#container')
return $(this).attr('href');
},
sizes: function() {
return this.getAttribute('data-cb-sizes');
},
srcset: function() {
return this.getAttribute('data-cb-srcset');
},
title: function() {
return this.title;
}
Expand Down Expand Up @@ -140,7 +147,11 @@
div = "div",
requests = 0,
previousCSS = {},
init;
init,

// Any browser that supports 'sizes' will also support 'w' syntax.
srcsetWSupported = "sizes" in new Image();


// ****************
// HELPER FUNCTIONS
Expand Down Expand Up @@ -823,12 +834,20 @@
var img,
i = $related[this],
settings = new Settings(i, $.data(i, colorbox)),
src = settings.get('href');
src = settings.get('href'),
sizes = settings.get('sizes'),
srcset = settings.get('srcset');

if (src && isImage(settings, src)) {
src = retinaUrl(settings, src);
img = document.createElement('img');
img.src = src;

if (srcset && srcsetWSupported) {
img.sizes = sizes;
img.srcset = srcset;
} else {
img.src = src;
}
}
});
}
Expand Down Expand Up @@ -890,27 +909,27 @@

function load () {
var href, setResize, prep = publicMethod.prep, $inline, request = ++requests;

active = true;

photo = false;

trigger(event_purge);
trigger(event_load);
settings.get('onLoad');

settings.h = settings.get('height') ?
setSize(settings.get('height'), 'y') - loadedHeight - interfaceHeight :
settings.get('innerHeight') && setSize(settings.get('innerHeight'), 'y');

settings.w = settings.get('width') ?
setSize(settings.get('width'), 'x') - loadedWidth - interfaceWidth :
settings.get('innerWidth') && setSize(settings.get('innerWidth'), 'x');

// Sets the minimum dimensions for use in image scaling
settings.mw = settings.w;
settings.mh = settings.h;

// Re-evaluate the minimum width and height based on maxWidth and maxHeight values.
// If the width or height exceed the maxWidth or maxHeight, use the maximum values instead.
if (settings.get('maxWidth')) {
Expand All @@ -921,9 +940,11 @@
settings.mh = setSize(settings.get('maxHeight'), 'y') - loadedHeight - interfaceHeight;
settings.mh = settings.h && settings.h < settings.mh ? settings.h : settings.mh;
}

href = settings.get('href');

sizes = settings.get('sizes');
srcset = settings.get('srcset');

loadingTimer = setTimeout(function () {
$loadingOverlay.show();
}, 100);
Expand Down Expand Up @@ -1010,7 +1031,14 @@
}, 1);
});

photo.src = href;
if (srcset && srcsetWSupported) {
if (sizes) {
photo.sizes = sizes;
}
photo.srcset = srcset;
} else {
photo.src = href;
}

} else if (href) {
$loadingBay.load(href, settings.get('data'), function (data, status) {
Expand Down