本文實例講述了js控制元素顯示在屏幕固定位置及監聽屏幕高度變化的方法。分享給大家供大家參考。具體如下:
//控制logo的顯示位置 Begin
window.addEventListener("resize", function () {
// 得到屏幕尺寸 (內部/外部寬度,內部/外部高度)
changeLogoPosition();
}, false);
changeLogoPosition();
function changeLogoPosition() {
var contentHeight = $("#main_content_div").css("height");
var logoHeight = $("#third_party_logo").css("height");
contentHeight = parseInt(contentHeight.replace('px', ''));
logoHeight = parseInt(logoHeight.replace('px', ''));
//alert('屏幕高度:'+document.body.scrollHeight+' 內容高度:'+contentHeight+' logo高度:'+logoHeight);
if (document.body.scrollHeight - contentHeight > logoHeight) {
document.getElementById('third_party_logo').style.position = 'absolute';
} else {
document.getElementById('third_party_logo').style.position = '';
}
}
//控制logo的顯示位置 End
希望本文所述對大家的javascript程序設計有所幫助。