jquery mobile中设定content高度的两种方法

本文介绍了两种解决jQuery Mobile中内容区域高度不占满剩余空间的方法:一是通过纯CSS实现内容区域占据可用空间;二是结合CSS与JavaScript动态计算并设置内容高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用jquerymobile开发时会遇到这样一个需求:If header and footer arefixed you would think that content covers rest of available space

默认情况下:Emptycontent div height is 0 and it will stretch vertically only to cover its inner(child) content.

 

解决方案有两种:

1. 纯CSS方案

we can forcecontent div to resize according to available space even before pageshow event.This can be done with this CSS rules

#content {
    padding: 0;
    position: absolute !important; 
    top : 45px !important;  
    right : 0; 
    bottom : 45px !important;  
    left : 0 !important;     
}

This CSS willremove classic content padding and absolutely position content div to cover allavailable space, giving only some space to a page header. Bottom rule can bechanged to 40px in case footer is also needed. Keyword !important is alsoneeded because we want to enforce new values over old ones.

 

2. CSS+Javascript方法

calculateavailable content height. It can be then used to dynamically set new contentheight. That combined with window resize event will give you bestresponsiveness.

css

#content {
    padding: 0;  
}

javascript

function getRealContentHeight() {
	var header = $.mobile.activePage.find("div[data-role='header']:visible");
	var footer = $.mobile.activePage.find("div[data-role='footer']:visible");
	var content = $.mobile.activePage.find("div[data-role='content']:visible:visible");
	var viewport_height = $(window).height();

	var content_height = viewport_height - header.outerHeight() - footer.outerHeight();
	if((content.outerHeight() - header.outerHeight() - footer.outerHeight()) <= viewport_height) {
		content_height -= (content.outerHeight() - content.height());
	} 
	return content_height;
}

参考:

Solution to jQuery Mobile page content empty space height problem

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值