我正在努力找出最佳方法是优化此代码。我目前仅使用width @media标签,而JS似乎是我可以控制一系列非常特定的变量来调整此div元素的唯一可靠方法。
我目前意识到这是一团糟,但是我想获得反馈,以查看是否存在任何明显的问题。仍在学习,所以对我轻松一点。
谢谢。
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);
});
由于目标不是很明确,因此我只能进行一些小的代码编辑,以使其更易于阅读,并且可能会提高一些性能。
jQuery(document).ready(function($) {
// Define share variables.
var element = $('.hs-caption');
var elementWidth, elementHeight;
var screenWidth, screenHeigth;
var upcomingHeader = $('.fold-header').height() * 2.0;
var heightOffset;
var totalHeight;
var HEIGHT_RATIO = 1.25;
var paddingElements = $('.hs-info .btn-slide');
var displayElements = $('.hs-info p, .hs-sponsor');
var RESIZE_DELEY = 200;
var delayedHandler = null;
function onResize() {
updateSetting();
setToCenterOfParent(element, $(document.body), true, false);
fullscreen();
scalar();
};
function scalar() {
var innerHeight = window.innerHeight;
var totalElementHeight = totalHeight * HEIGHT_RATIO + upcomingHeader;
var isScreenSmaller = (innerHeight < totalElementHeight);
var padding = isScreenSmaller ? '10px 0px 0px 0px' : '0px';
var display = isScreenSmaller ? 'none' : 'block';
paddingElements.css('padding', padding);
displayElements.css('display', display);
}
function updateSetting(){
screenWidth = $(window).width();
screenWidth = $(window).height();
elementWidth = element.width();
elementHeight = element.height();
heightOffset = (screenHeight > 960) ? 220 : 200;
totalHeight = elementHeight + heightOffset;
}
function fullscreen() {
$('#hero').css({
width: screenWidth,
height: screenHeigth
});
}
function setToCenterOfParent(element, parent, ignoreWidth, ignoreHeight) {
var parentWidth = parent.width();
var parentHeight = parent.width();
if (!ignoreWidth) {
element.css('left', parentWidth/2 - elementWidth/2);
}
if (!ignoreHeight) {
element.css('top', heightOffset);
}
}
// Init
onResize();
$(window).resize(function() {
// Don't do this too often
if (delayedHandler !== null) {
clearTimeout(delayedHandler);
}
// Delayed the function to be executed, so it only updates when user stop
// resizing for a fixed amount of time.
delayedHandler = setTimeout(onResize, RESIZE_DELEY);
});
console.log("height:", elementHeight);
console.log("total height:", elementHeight + heightOffset);
console.log("screenHeight:", screenHeight);
console.log("heightOffset:", heightOffset);
console.log("upcomingHeader:", upcomingHeader);
});
这个想法是:
$('.hs-info .btn-slide')
和$('.hs-info p, .hs-sponsor')
,$('.hs-caption')
似乎也被广泛使用,将其拔出。setToCenterOfParent
其他元素,但是我将尝试输入jquery包装的元素,因此我可以在函数中直接对它们使用jquery。elementWidth/elementHeight
尚不清楚它是否会更改,因此我保留未编辑的代码。settimeout
来调整大小,因此jquery在用户调整窗口大小期间不会进行大量计算,并且0.2 sec的延迟应该足够快,以使用户不会感到延迟(或者您可以将RESIZE_DELEY调整为更小的数字)。jQuery(foo).bar
,以$(foo).bar
保持一致性。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句