我试图找出如何让 jQuery 脚本在单击事件启动时正常工作,该脚本计算容器的高度(此脚本出现在加载页面的开头并 100% 工作)但我是现在试图让它在 .click 事件中工作。
如果有人知道我应该如何实现这一点,我将不胜感激。
ps 我已经尝试过准备好文件;等待 DOM 加载但没有雪茄。
$(document).ready(function() {
$("#hidden4").click(function(event) {
event.preventDefault();
$("#hidden4-1").slideToggle();
var classDown = $('#hidden4 div.down').attr('class');
var classUp = $('#hidden4 div.up').attr('class');
if(classDown == 'down') {
$("#hidden4 div.down").attr("class","up");
}
else if(classUp == 'up') {
$("#hidden4 div.up").attr("class","down");
}
// Attain the absolute height of the container id container and assign to a usable variable
var height = $("#container").height();
alert (height);
// Use the previous variable, divide by 4 then round that up and multiply by 4 and assign to new variable
var newHeight = Math.ceil(height / 4) * 4;
// Create a new variable and add the string "center" to it
var finalHeight = "center ";
// Using the previous variable add to it using the first 2 variables subtracting to find the difference and add 2
finalHeight += (newHeight - height)+2;
// Using the previous variable add to it the string "px" for the css selector usage
finalHeight += "px";
// Update the CSS of the required element altering the background position with the final variable
$(".contentFooter").css('background-position', finalHeight);
});
});
先感谢您。