search
HomeWeb Front-endHTML Tutorial手机html页面的垂直布局的基本框架_html/css_WEB-ITnose

手机页面的一种典型布局方式为:头(header)、主体(main)和底(footer),头和底放一些常用的操作,主体部分显示内容,如下图:

要达到的效果是header固定在头部,footer固定在底部,主体部分可以滚动,实现的思路有如下几种:

1、固定位置(fixed)
利用CSS的position:fixed是最直接的方式,分别将header和footer固定在窗口的顶和底。虽然这种方式最简单直接,但是存在两个问题:1、中间的主体部分必须为header和footer预留出空间,否则内容就会被盖住。如果header和footer的高度已知且固定,可以通过给主体设置margin解决。如果不是已知的就麻烦了,必须要借助js进行计算才行,因此这种方式的的灵活性不够;2、在iOS环境下,如果页面上有input空间,输入时调用软键盘会导致fixed失效,footer显示的位置就乱了(android下没有问题)。所以,fixed方式虽然看似简单,用起来问题不少。

2、绝对位置(absolute)
采用绝对定位就是把header,main和footer都用absolute放在固定的位置上,让main支持自动滚动,这样内容多的时候滚动只发生在main里面,header和footer的位置固定。这种方式的局限是必须确定header和footer的高度,如果不确定需要借助js,灵活性不够。

3、弹性位置(flex)
弹性布局可以根据容器的情况和设置的规则自动调整子元素的大小,非常适合解决垂直布局的问题。但是,也有不少坑。第一,弹性布局有一新(display:flex)一旧(display:box)两个版本,当然想用新版本,可是android版的微信不支持,好在ios和主流的浏览器也都支持老版本,所以就先用老版本吧(只是做了简单的兼容测试)。第二,当main区域需要滚屏的时候,androi版的微信会导致footer里的button不能响应点击事件,直到滚屏到底,才行,似乎是main把footer覆盖了(footer一直可见),设置了z-index也不管用。

看代码:

<div class='flex-frame' ng-controller="myCtrl2">    <header class='flex-bar'>        <button class='btn btn-lg btn-default' ng-click="click($event,'buttonA')">buttonA</button>    </header>    <div class='flex-main'>        <div class='flex-main-wrap'>            <ul class='list-group'>                <li class='list-group-item' ng-repeat="d in data" ng-bind="d"></li>            </ul>            <input type='text' class='form-control input-lg'>        </div>    </div>    <footer class='flex-bar'>        <button class='btn btn-lg btn-default' ng-click="click($event,'button1')">button1</button>        <button class='btn btn-lg btn-default' ng-click="click($event,'button2')">button2</button>    </footer></div>
.flex-frame{height:100%;width:100%;background:#eff;display:-webkit-box;-webkit-box-orient:vertical;}.flex-bar{display:-webkit-box;padding:4px;background:#eee;}.flex-bar>button{display:block;-webkit-box-flex:1.0;margin-left:4px;}.flex-bar>button:first-child{margin-left:0;}.flex-main{position:relative;background:#ccc;-webkit-box-flex:1.0;overflow-y:hidden;padding-bottom:80px;}.flex-main-wrap{position:absolute;top:0;bottom:0;left:0;right:0;overflow-y:auto;}
var data = [], i = 0;while (i<30) {     data.push('row:' + i);     i++;}$scope.data = data;$scope.click = function(event, name) {     alert('click ' + name);}

看例子

PS:
1、本来想用main标签,发现微信的浏览器不认识main标签,就直接用div了。
2、微信里如果main区域需要滚屏,footer似乎就会被盖住,无法响应点击事件,不知道确切的原因是什么,找到下面这段话不知道是不是原因。
When the computed value for the overflow property is ‘visible’, ‘scroll’ or ‘auto’, the content may overflow the container. If the computed value for direction is normal, the content will overflow over the right or bottom side. If the computed value for direction is reverse, the content will overflow over the left or top side.
为了解决这个问题,加了flex-main-wrap进行控制。

参考:
https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Flexible_Box_Layout
http://www.w3.org/TR/2009/WD-css3-flexbox-20090723/

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.