Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This might help someone else, here is a solution for applying custom dimensions to colorbox when it is opened, I think this works regardless of implementation. Drop this into the js on your in your theme.
(function ($, Drupal, window, document, undefined) {
//Configure colorbox call back to resize with custom dimensions
$.colorbox.settings.onLoad = function() {
colorboxResize();
}
//Customize colorbox dimensions
var colorboxResize = function(resize) {
var width = "90%";
var height = "90%";
if($(window).width() > 960) { width = "860" }
if($(window).height() > 700) { height = "630" }
$.colorbox.settings.height = height;
$.colorbox.settings.width = width;
//if window is resized while lightbox open
if(resize) {
$.colorbox.resize({
'height': height,
'width': width
});
}
}
//In case of window being resized
$(window).resize(function() {
colorboxResize(true);
});
})(jQuery, Drupal, this, this.document);
Comments
Comment #1
hockey2112 CreditAttribution: hockey2112 commentedWorked great for me! Thanks!
Comment #2
rimu CreditAttribution: rimu commentedFantastic, thank you!
Comment #3
rakesh.gectcrAmazing work...!
Thanks !
Comment #4
candelas CreditAttribution: candelas commentedIf you need to resize a video that is in a iframe, add this code after $.colorbox.settings.width = width;
Comment #5
candelas CreditAttribution: candelas commentedI had videos and images with picture, so at the end I made this
Comment #6
susoson CreditAttribution: susoson commentedWorked great with bootstrap class img-responsive on the img tag. Thanks!
Comment #7
tzt20 CreditAttribution: tzt20 commentedThanks for sharing this great solution @philpro, worked like a charm and easy to customize of course :)
Comment #8
dariogcode CreditAttribution: dariogcode commentedHi,
Thanks for this solution. I also noticed that videos inside a colorbox cannot be clickable, it appear that none of the link inside the colorbox works. Anyone having the same issue?
Comment #9
xaxI have a popup-on-load on the homepage of a site, popup renders beautifully in a colorbox, but is not responsive. I tried added the js above (first the top bit, then the version at #5), they do make a difference: first the colorbox popup opens in the old fashioned way, far to big and thus mostly offscreen, then when I press reload page (mobile safari iOS8) it does resize the colorbox, to 90% height and 90% width. However hxw of the screen, not of the image in the popup! So my landscape image is now stretched being recognition to a portrait one..
Comment #10
narkoff CreditAttribution: narkoff commentedThanks, @philpro. The code works when colorbox modal is already open and then resizing window. However, if there is a gallery of multiple pictures, clicking to the next picture causes the colorbox popup to resize to the original larger value. Only resizing the window will cause the colorbox to fit again in the smaller browser window.
Along the same lines, if the browser window starts small, the colorbox popup will only fit when resizing the browser.
Clearly the code only triggers colorbox resize when the browser window is resized. How can I keep the colorbox window at the smaller/resized value when clicking through multiple pictures/objects?
Comment #11
von_stirliz CreditAttribution: von_stirliz commentedGuys,
where shall I add the code? I am not very experienced user, so a help would be appreciated!
Thanks,
Cheers.
Comment #12
wataruo CreditAttribution: wataruo as a volunteer commented@von_stirliz, it's just a piece of js. Make a js file and name as you like. And refer the js file as other js files.
Thanks @philpro, it's a good tip!
Comment #13
tr-drupal CreditAttribution: tr-drupal commentedDidn't try it yet, but thanks a lot from me as well!
Wouldn't it make sense to add this to the module's js by default?
Comment #14
NIKS_Artreaktor CreditAttribution: NIKS_Artreaktor commentedFor Video it is ok.
Because we can set width/height and make video w100%/ h100% ...
But for Images it is trouble. In this code we have to set certain width/height. And it is bad.
If it gallery and you click next/previous then colorbox will understand what w/h inside and adapted container for image.
Somebody know how to resize colorbox by imitation next/previous buttons click?
To resize colorbox without certain width/height.
(for example here setting certain W/H
var width = "90%";
var height = "90%";
if($(window).width() > 960) { width = "860" }
if($(window).height() > 700) { height = "630" }
)
And I was surprised that colorbox not responsive. So many people using it....
Thanks.
Comment #15
Ralf Eisler CreditAttribution: Ralf Eisler commentedThis is working great to keep node content responsive in Colorbox Node module.
Thanks.
Comment #16
rupesh_jagtap CreditAttribution: rupesh_jagtap commentedGreat work philpro, it's worked well for me to play Youtube Video inside colorbox. Why don't you add this as a patch?
Comment #17
Rob T CreditAttribution: Rob T commentedThanks for this.
Comment #18
wellme CreditAttribution: wellme at Zyxware Technologies commentedComment #19
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedSo is this something that can be added to the js for the colorbox module or do I need to create a js file in my theme and add that file to the .info file?
Comment #20
Sridar CreditAttribution: Sridar commentedWorked like a charm! Thanks for the patch.
Comment #21
phongtt CreditAttribution: phongtt commentedit's work, thank.
Comment #22
Nathan Tsai CreditAttribution: Nathan Tsai as a volunteer commentedUntested! Please comment if it actually works or not :)
Comment #23
mmncs CreditAttribution: mmncs commentedI am using picture module with colorbox and I tried the js in #22 which didn't seem to work.
I use a picture mapping which maps to the original image and if the original image is larger than the screen it overflows.
Comment #24
HongPong CreditAttribution: HongPong as a volunteer and at kor group commentedBy setting a maxWidth attribute on colorbox I had much better results with mobile devices, a value like "95%" works well. It wasn't immediately obvious that maxWidth was necessary.
Comment #25
auxiliaryjoel CreditAttribution: auxiliaryjoel commented@HongPong could you elaborate on the maxWidth attribution you mentioned in #24. I'm having trouble with getting the thumbnails on page to be responsive columns and also the expanded/open image not sizing down to fit screen when browser is scaled smaller.
Comment #26
HongPong CreditAttribution: HongPong as a volunteer and at kor group commented@auxiliaryjoel the maxwidth attribute described here: http://www.jacklmoore.com/colorbox/ is normally false so you have to set it to be active. i think this is a result of the fact colorbox may not be a 'mobile first' solution that automatically works easily with mobile devices. I might be wrong, this was a while ago, but i think that was my issue.
Comment #27
Neslee Canil PintoCan anybody apply a patch for this.
Comment #28
Neslee Canil PintoComment #29
batkorThanks!
Worked for me