Skip to content

Instantly share code, notes, and snippets.

@jrruiz
Last active May 18, 2016 11:24
Show Gist options
  • Save jrruiz/8ad12fb49926ca944416a24fda0245c3 to your computer and use it in GitHub Desktop.
Save jrruiz/8ad12fb49926ca944416a24fda0245c3 to your computer and use it in GitHub Desktop.
Force full width for elements inside an element without full width
function setFullWidth(elements) {
var $window = $(window);
if (!Array.isArray(elements)) {
elements = [elements];
}
elements = elements.map(function(element) {
element.position = element.position || 'relative';
return element;
});
adjustFullWidth();
$window.on('resize', enqueueResize);
function enqueueResize() {
if (adjustFullWidth.timeoutId) {
clearTimeout(adjustFullWidth.timeoutId);
}
adjustFullWidth.timeoutId = setTimeout(adjustFullWidth, 100);
}
function adjustFullWidth() {
var clientWidth = document.documentElement.clientWidth;
elements.forEach(function(element) {
var $element = $(element.selector),
$referenceElement = $(element.referenceElement),
referenceLeftOffset = $referenceElement.offset().left * -1,
referenceLeftPadding = $referenceElement.css('padding-left');
if (!$element.length || !$referenceElement.length) {
return;
}
referenceLeftPadding = Number(referenceLeftPadding.replace(/\D+/g, ''));
if (referenceLeftPadding > 0) {
referenceLeftOffset -= referenceLeftPadding;
}
$element.css({
left: referenceLeftOffset.toString() + 'px',
position: element.position,
width: clientWidth.toString() + 'px'
});
});
}
}
// usage
var fullWidthElements = [];
fullWidthElements.push({ selector: '#newsletter_block_home', referenceElement: '#main > .row' });
fullWidthElements.push({ selector: '.blockproductscategory', referenceElement: '#main' });
fullWidthElements.push({ selector: '.adtm_sub', referenceElement: '#main', position: 'absolute' });
setFullWidth(fullWidthElements);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment