search
HomeWeb Front-endHTML Tutorial移动端webapp自适应实践_html/css_WEB-ITnose

为什么要写这个

以前写过关于webapp自适应屏幕的文章(链接),不过写的大多数群众看不懂,所以来个图文并茂的版本。虽然只是一个简单的页面,不过在做的过程中也遇到了一些问题,也算是好事吧!

该示例github地址:https://github.com/iwangx/WebApp访问地址:https://csssprite.herokuapp.com/

准备

  • PSD:这个是最重要的东西,用于测量尺寸,以及切图,我是不太同意切图的工作交给ui,自己切比较好,psd的分辨率我做的是640px的宽度,当然这个psd是网上下的。
  • CSS雪碧图制作工具:最新版(v4.3)能够将多张图片拼接成一张图片并且生成代码的小程序(本人自主研发)。
  • Webstorm:前端开发利器。
  • 马克曼:前端尺寸颜色测量工具。
  • Photoshop:切图利器。
  • Sass:CSS预编译工具。

第一步、切图

我用的是photoshop切的图,切图如下:

当然可能看不太清楚,毕竟是白色的图。

第二步、搭建框架

大体结构就是这个,地址:https://github.com/iwangx/WebApp

一、CSS结构

CSS主要采用Sass作于预编译的工具,结构如下:

  • reset.scss文件主要是对于元素样式的重置;
  • app.scss文件是对单个页面样式;
  • size.scss文件里面包含了手机端自适应1-400尺寸的变量。

二、JS结构

js中最重要的是自适应的的代码,地址:移动端自适应,zepto这些都没有引入,因为只是个简单的示例。

第三步、html页面机构

页面结构如下图:

  • header:头部标题;
  • nav:头部的导航;
  • controller:中间列表;
  • footer:底部导航。

第四步、生成雪碧图

利用雪碧图生成工具:生成雪碧图,并且生成代码:

将代码copy到css目录中的app.scss目录下:

当然前提是要引入size这个里面方面变量的文件,要不然$_*就没法识别而报错了。

第五步、页面以及CSS编码

CSS代码:

@import "size";//所有图片变量@mixin sprite{background:url(../images/sprite.png) no-repeat ;background-size:$_138 $_163;}@mixin icon_right{height:$_59;width:$_59;background-position:0 -$_75 0 -$_5;}@mixin icon_left{height:$_59;width:$_59;background-position:0 0;}@mixin icon_tag{height:$_44;width:$_65;background-position:0 -$_1 0 -$_119;}@mixin icon_person{height:$_44;width:$_65;background-position:0 0 -$_66;}@mixin icon_book{height:$_44;width:$_65;background-position:0 -$_73 0 -$_71;}@mixin icon_more{height:$_44;width:$_65;background-position:0 -$_73 0 -$_119;}body{background: #fbfbfb}.sprite{@include sprite;}.header{position: fixed;background: #dd3131;height: $_90;line-height: $_90;;width: 100%;left: 0;top: 0;font-size: $_40;color: #fff;text-align: center;  button{position: absolute;top: $_16;border: $_2 solid #fff;border-radius: 50%;box-sizing: content-box}}.btn-left{@include icon_left;left: $_16;}.btn-right{@include icon_right;right: $_16;}.nav{display: -webkit-box;position: fixed;left: 0;top: $_90;color: #3d3d3d;font-size: $_30;width: 100%;border-bottom: $_1 solid #e7e5e6;  a{display: block;height: $_60;line-height: $_60;text-align: center;background: #fff;-webkit-box-flex: 1;border-right: $_1 solid #e7e5e6;box-sizing: border-box;}}.controller{padding: $_151 0 $_100 0;}.list{  li{border-bottom:$_1 solid #cfcfcf }  a{display: -webkit-box;padding: $_16;}  img{height: $_122;width: $_122;display: block}}.list-right{-webkit-box-flex: 1;padding-left: $_15;  h1{color: #555;font-size: $_24;}  p{color: #878787;font-size: $_18;margin-top: $_15;line-height: 1.5}}.footer{height: $_100;position: fixed;left: 0;bottom: 0;width: 100%;display: -webkit-box;background: #4a4a4a;text-align: center;  a{display: block;-webkit-box-flex: 1;box-sizing: border-box;padding-top: $_10;border-right: $_1 solid #fff;    &:last-child{border-right: none}  }  i{display: block;margin: 0 auto}  span{color: #fff;font-size: $_24;display: block;margin-top: $_5;}}.icon_book{@include icon_book}.icon_tag{@include icon_tag}.icon_person{@include icon_person}.icon_more{@include icon_more}

HTML代码:

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <meta name="viewport" content="initial-scale=0.5,minimum-scale=0.5, maximum-scale=0.5,user-scalable=no,minimal-ui">    <meta name="author" content="wangxing">    <meta name="screen-orientation" content="portrait">    <meta name="x5-orientation" content="portrait">    <meta name="format-detection" content="telephone=no">    <title>app自适应</title>    <link rel="stylesheet" href="/css/reset.css">    <link rel="stylesheet" href="/css/app.css">    <script src="/js/lib/resize.js"></script></head><body>    <header class="header">        <button class="sprite btn-left"></button>        <span>远程酒业</span>        <button class="sprite btn-right"></button>    </header>    <nav class="nav">        <a href="javascript:;">资讯</a>        <a href="javascript:;">产品</a>        <a href="javascript:;">实战</a>        <a href="javascript:;">讨论</a>    </nav>    <section class="controller">        <ul class="list">            <li>                <a href="#">                    <img src="/static/imghwm/default1.png"  data-src="src"  class="lazy" alt="图片">                    <div class="list-right">                        <h1 id="酒名字">酒名字</h1>                        <p>提升白酒等传统酒类生产机械化水平成为行业未来研究工作的重点,从而促进酿酒装备与技术的快速发展……</p>                    </div>                </a>            </li>            <li>                <a href="#">                    <img src="/static/imghwm/default1.png"  data-src="src"  class="lazy" alt="图片">                    <div class="list-right">                        <h1 id="酒名字">酒名字</h1>                        <p>提升白酒等传统酒类生产机械化水平成为行业未来研究工作的重点,从而促进酿酒装备与技术的快速发展……</p>                    </div>                </a>            </li>            <li>                <a href="#">                    <img src="/static/imghwm/default1.png"  data-src="src"  class="lazy" alt="图片">                    <div class="list-right">                        <h1 id="酒名字">酒名字</h1>                        <p>提升白酒等传统酒类生产机械化水平成为行业未来研究工作的重点,从而促进酿酒装备与技术的快速发展……</p>                    </div>                </a>            </li>            <li>                <a href="#">                    <img src="/static/imghwm/default1.png"  data-src="src"  class="lazy" alt="图片">                    <div class="list-right">                        <h1 id="酒名字">酒名字</h1>                        <p>提升白酒等传统酒类生产机械化水平成为行业未来研究工作的重点,从而促进酿酒装备与技术的快速发展……</p>                    </div>                </a>            </li>        </ul>    </section>    <footer class="footer">        <a href="#">            <i class="sprite icon_book"></i>            <span>资讯</span>        </a>        <a href="#">            <i class="sprite icon_tag"></i>            <span>产品</span>        </a>        <a href="#">            <i class="sprite icon_person"></i>            <span>实战</span>        </a>        <a href="#">            <i class="sprite icon_more"></i>            <span>讨论</span>        </a>    </footer></body></html>

用到图片的地方,主要有两个class,一个是雪碧图的class,一个就是他本事引用的class,二者结合。

大家可以看到css代码中的大小全都是$_*,也就是测量出来的大小,包括字号也是。

这样就构成了我们的webapp.

iphone5上是这样:

iphone6上是这样:

平板上是这样:

结语

希望这个博客对大家有帮助,尤其是新手,也希望大家多提意见。

作者:你猜不猜文章源自:http://www.cnblogs.com/wang4517/p/4528432.html

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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools