I'm struggling to figure out what the best way would be to optimize this code. I'm currently only using width @media tags, and JS seems to be the only reliable way I can control a series of very specific variables to adjust this div element.
It's currently a mess I realize, but I wanted to get feedback to see if there are any glaring issues. Still learning, so go easy on me.
Thanks.
jQuery(document).ready(function($){
var elementHeight = $('.hs-caption').height();
var screenHeight = jQuery(window).height();
var upcomingHeader = $('.fold-header').height() * 2.0;
if (screenHeight > 960) {
var heightOffset = 220;
} else {
var heightOffset = 200;
}
var totalHeight = elementHeight + heightOffset;
function fullscreen(){
jQuery('#hero').css({
width: jQuery(window).width(),
height: jQuery(window).height()
});
}
function setToCenterOfParent(element, parent, ignoreWidth, ignoreHeight){
parentWidth = $(parent).width();
parentHeight = $(parent).height();
elementWidth = $(element).width();
elementHeight = $(element).height();
if(!ignoreWidth)
$(element).css('left', parentWidth/2 - elementWidth/2);
if(!ignoreHeight)
$(element).css('top', heightOffset);
}
function scalar(){
if (window.innerHeight < (totalHeight * 1.25) + upcomingHeader) {
console.log("screenHeight is less than elementHeight");
$('.hs-info p').css({
display: 'none'
}),
$('.hs-info .btn-slide').css({
padding: '10px 0px 0px 0px'
}),
$('.hs-sponsor').css({
display: 'none'
})
} else {
console.log("screenHeight is NOT less than elementHeight");
$('.hs-info .btn-slide').css({
padding: '0px'
}),
$('.hs-sponsor').css({
display: 'block'
}),
$(".hs-info p").css({
display: 'block'
})
}
}
setToCenterOfParent( $('.hs-caption'), document.body, true, false);
fullscreen();
scalar();
jQuery(window).resize(function() {
scalar();
setToCenterOfParent( $('.hs-caption'), document.body, true, false);
fullscreen();
});
console.log("height:", elementHeight);
console.log("total height:", elementHeight + heightOffset);
console.log("screenHeight:", screenHeight);
console.log("heightOffset:", heightOffset);
console.log("upcomingHeader:", upcomingHeader);
});
As the target is not very clear, I can only do some small code editing to make it more easy to read, and maybe a little performance improvement.
The idea is:
$('.hs-info .btn-slide')
and$('.hs-info p, .hs-sponsor')
, also$('.hs-caption')
seems to be widely used, pull it out.setToCenterOfParent
to other elements, but I'll try to input the jquery wrapped element, so I can directly use jquery on them in the function.elementWidth/elementHeight
here is not clear whether it'll change or not, so I remain the code not edited.settimeout
to resize, so jquery won't do lots of computation during the user resizing his window, and 0.2 sec's delay should be fast enough so user won't feel a delay(or you can adjust RESIZE_DELEY to a even smaller number).jQuery(foo).bar
to$(foo).bar
for consistency.I believe this should be the most we can do, if there's no example like jsfiddle, to demonstrate what this code is for.