前端学习笔记 - 移动端Web开发

本文详细介绍了移动端Web开发的基础知识,包括浏览器现状、手机屏幕尺寸、常见移动端屏幕尺寸和调试方法。深入讲解了视口、meta视口标签、二倍图、背景缩放、流式布局和flex弹性布局。此外,还探讨了移动端开发选择,如单独移动端页面和响应式布局,以及介绍了移动端技术解决方案,如normalize.css、CSS3盒子模型、媒体查询、Less和rem布局。文章还涵盖了Bootstrap前端框架、vw/vh移动端布局、移动端网页特效如触屏事件、轮播图和拖动元素,并讨论了移动端开发中常用的插件和框架。
摘要由CSDN通过智能技术生成

移动端基础

浏览器现状

PC端常见浏览器:360浏览器、谷歌浏览器、火狐浏览器、QQ浏览器、百度浏览器、搜狗浏览器、IE浏览器

移动端常见浏览器:UC浏览器,QQ浏览器,欧朋浏览器,百度手机浏览器,360安全浏览器,谷歌 浏览器,搜狗手机浏览器,猎豹浏览器,以及其他杂牌浏览器。

  • 国内的UC和QQ,百度等手机浏览器都是根据Webkit修改过来的内核,国内尚无自主研发的内核,就像国内的手机操作系统都是基于Android修改开发的一样

  • 兼容移动端主流浏览器,处理Webkit内核浏览器即可

手机屏幕现状

移动端设备屏幕尺寸非常多,碎片化严重。

  • Android设备有多种分辨率:480x800, 480x854, 540x960, 720x1280,1080x1920等,还有传说中的2K,4k屏。

  • 近年来iPhone的碎片化也加剧了,其设备的主要分辨率有:640x960, 640x1136, 750x1334, 1242x2208等。

  • 作为开发者无需关注这些分辨率,因为常用的尺寸单位是px

常见移动端屏幕尺寸

设备 尺寸(英寸) 开发尺寸(px) 物理像素比
iphone3G 3.5 320*480 1.0
iphone4/4s 3.5 320*480 2.0
iphone5/5s/5c 4.0 320*568 2.0
HTC One M8 4.5 360*640 3.0
iphone 6 4.7 375*667 2.0
Nexus 4 4.7 384*640 2.0
Nexus 5x 5.2 411*731 2.3
iphone6 Plus 5.5 414*736 3.0
Samsung Galaxy Note4 5.7 480*853 3.0
Sony Xperia Z Ultra 6.4 540*960 2.0
Nexus 7('12) 7.0 600*960 1.3
iPad Mini 7.9 768*1024 1.0

作为前端开发,不建议纠结dp,dpi,pt,ppi等单位。

移动端调试方法

  • Chrome DevTools(谷歌浏览器)的模拟手机调试

    • f12→左上角小图标→可以选择型号,也可以edit添加

    • image-20211014172322974

    • image-20211014172447703

    • image-20211014172555608

  • 搭建本地web服务器,手机和服务器一个局域网内,通过手机访问服务器

  • 使用外网服务器,直接IP或域名访问

视口

视口(viewport)就是浏览器显示页面内容的屏幕区域。 视口可以分为布局视口、视觉视口和理想视口

  • 布局视口 layout viewport:一般移动设备的浏览器都默认设置了一个布局视口,用于解决早期的PC端页面在手机上显示的问题

    • iOS, Android基本都将这个视口分辨率设置为 980px,所以PC上的网页大多都能在手机上呈现,只不过元 素看上去很小,一般默认可以通过手动缩放网页

image-20211014172938463

image-20211014173322294

  • 视觉视口 visual viewport:是用户正在看到的网站的区域

    • 可以通过缩放去操作视觉视口,但不会影响布局视口,布局视口仍保持原来的宽度

image-20211014173026820

  • 理想视口 ideal viewport

    • 使网站在移动端有最理想的浏览和阅读宽度而设定

    • 理想视口,对设备来讲,是最理想的视口尺寸

    • 需要手动添写meta视口标签通知浏览器操作

      meta视口标签的主要目的:布局视口的宽度应该与理想视口的宽度一致,简单理解就是设备有多宽,布局的视口就多宽

meta视口标签

<meta name="viewport" content="width=device-width, user-scalable=no,
initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
属性 解释说明
width 宽度设置的是viewport宽度,可以设置device-width特殊值
initial-scale 初始缩放比,大于0的数字(页面一打开按照几倍打开)
maximum-scale 最大缩放比,大于0的数字
minimum-scale 最小缩放比,大于0的数字
user-scalable 用户是否可以缩放,yes或no(1或0)

标准的viewport设置

  • 视口宽度和设备保持一致 width=device-width

  • 视口的默认缩放比例1.0 initial-scale=1.0

  • 不允许用户自行缩放 user-scalable=no

  • 最大允许的缩放比例1.0 maximum-scale=1.0

  • 最小允许的缩放比例1.0 minimum-scale=1.0

二倍图

物理像素&物理像素比

物理像素点指的是屏幕显示的最小颗粒,是物理真实存在的。

  • 厂商在出厂时就设置好了,比如苹果6\7\8 是 750* 1334

  • 开发时候的1px 不是一定等于1个物理像素的

  • PC端页面,1个px 等于1个物理像素的,但是移动端就不尽相同

  • PC端 和 早前的手机屏幕 / 普通手机屏幕: 1CSS像素 = 1 物理像素的

  • 一个px的能显示的物理像素点的个数,称为物理像素比或屏幕像素比

    • iphone8的物理像素是750,1px开发像素等于2个物理像素(375宽的盒子就可以占满了)

  • Retina(视网膜屏幕)是一种显示技术,可以将把更多的物理像素点压缩至一块屏幕里,从 而达到更高的分辨率,并提高屏幕显示的细腻程度

image-20211014175133308

多倍图

  • 对于一张 50px * 50px 的图片,在手机 Retina 屏中打开,按照刚才的物理像素比会放大倍数,这样会造成图片模糊

  • 在标准的viewport设置中,使用倍图来提高图片质量,解决在高清设备中的模糊问题

  • 通常使用二倍图, 因为iPhone 6\7\8 的影响,但是现在还存在3倍图4倍图的情况,这个看实际开发公司需求

  • 背景图片 注意缩放问题

        /* 在 iphone8 下面,需要一个50*50px的图片,直接放进去会被放大两倍,100*100就会模糊,所以要放一个100*100像素的图片,然后手动把这个图片的大小缩小为50*50px */
        /* 准备的图片比实际需要的大小大两倍,就是2倍图 */
        img {
            /*原始图片100*100px*/
            width: 50px;
            height: 50px;
        }
        /*或*/
        .box {
            /*原始图片100*100px*/
            background-size: 50px 50px;
        }

背景缩放 background-size

background-size 属性规定背景图像的尺寸

background-size: 背景图片宽度 背景图片高度;

  • 单位: 长度|百分比|cover|contain;

  • cover把背景图像扩展至足够大,以使背景图像完全覆盖背景区域。

  • contain把图像图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域

  • 只写一个参数,算作宽度,高度会等比例缩放

移动端开发选择

  • 移动端主流方案

    • 单独制作移动端页面(主流)

      • 京东商城手机版、淘宝触屏版、苏宁易购手机版等

    • 响应式页面兼容移动端(其次)

      • 三星手机官网等

单独移动端页面(主流)

通常情况下,网址域名前面加 m(mobile) 可以打开移动端。通过判断设备,如果是移动设备打开,则跳到移动端页面

image-20211014192235745

响应式兼容PC移动端

三星电子官网: www.samsung.com/cn/ ,通过判断屏幕宽度来改变样式,以适应不同终端。

  • 缺点:制作麻烦, 需要花很大精力去调兼容性问题

移动端技术解决方案

移动端浏览器

  • 移动端浏览器基本以 webkit 内核为主,因此就考虑webkit兼容性问题。

  • 可以放心使用 H5 标签和 CSS3 样式。

  • 同时浏览器的私有前缀只需要考虑添加 webkit 即可

image-20211014192510648

CSS初始化 normalize.css

移动端 CSS 初始化推荐使用 normalize.css/

Normalize.css:

/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
​
​
/* Document
   ========================================================================== */
​
​
/**
 * 1. Correct the line height in all browsers.
 * 2. Prevent adjustments of font size after orientation changes in iOS.
 */
​
html {
    line-height: 1.15;
    /* 1 */
    -webkit-text-size-adjust: 100%;
    /* 2 */
}
​
​
/* Sections
   ========================================================================== */
​
​
/**
 * Remove the margin in all browsers.
 */
​
body {
    margin: 0;
}
​
​
/**
 * Render the `main` element consistently in IE.
 */
​
main {
    display: block;
}
​
​
/**
 * Correct the font size and margin on `h1` elements within `section` and
 * `article` contexts in Chrome, Firefox, and Safari.
 */
​
h1 {
    font-size: 2em;
    margin: 0.67em 0;
}
​
​
/* Grouping content
   ========================================================================== */
​
​
/**
 * 1. Add the correct box sizing in Firefox.
 * 2. Show the overflow in Edge and IE.
 */
​
hr {
    box-sizing: content-box;
    /* 1 */
    height: 0;
    /* 1 */
    overflow: visible;
    /* 2 */
}
​
​
/**
 * 1. Correct the inheritance and scaling of font size in all browsers.
 * 2. Correct the odd `em` font sizing in all browsers.
 */
​
pre {
    font-family: monospace, monospace;
    /* 1 */
    font-size: 1em;
    /* 2 */
}
​
​
/* Text-level semantics
   ========================================================================== */
​
​
/**
 * Remove the gray background on active links in IE 10.
 */
​
a {
    background-color: transparent;
}
​
​
/**
 * 1. Remove the bottom border in Chrome 57-
 * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
 */
​
abbr[title] {
    border-bottom: none;
    /* 1 */
    text-decoration: underline;
    /* 2 */
    text-decoration: underline dotted;
    /* 2 */
}
​
​
/**
 * Add the correct font weight in Chrome, Edge, and Safari.
 */
​
b,
strong {
    font-weight: bolder;
}
​
​
/**
 * 1. Correct the inheritance and scaling of font size in all browsers.
 * 2. Correct the odd `em` font sizing in all browsers.
 */
​
code,
kbd,
samp {
    font-family: monospace, monospace;
    /* 1 */
    font-size: 1em;
    /* 2 */
}
​
​
/**
 * Add the correct font size in all browsers.
 */
​
small {
    font-size: 80%;
}
​
​
/**
 * Prevent `sub` and `sup` elements from affecting the line height in
 * all browsers.
 */
​
sub,
sup {
    font-size: 75%;
    line-height: 0;
    position: relative;
    vertical-align: baseline;
}
​
sub {
    bottom: -0.25em;
}
​
sup {
    top: -0.5em;
}
​
​
/* Embedded content
   ========================================================================== */
​
​
/**
 * Remove the border on images inside links in IE 10.
 */
​
img {
    border-style: none;
}
​
​
/* Forms
   ========================================================================== */
​
​
/**
 * 1. Change the font styles in all browsers.
 * 2. Remove the margin in Firefox and Safari.
 */
​
button,
input,
optgroup,
select,
textarea {
    font-family: inherit;
    /* 1 */
    font-size: 100%;
    /* 1 */
    line-height: 1.15;
    /* 1 */
    margin: 0;
    /* 2 */
}
​
​
/**
 * Show the overflow in IE.
 * 1. Show the overflow in Edge.
 */
​
button,
input {
    /* 1 */
    overflow: visible;
}
​
​
/**
 * Remove the inheritance of text transform in Edge, Firefox, and IE.
 * 1. Remove the inheritance of text transform in Firefox.
 */
​
button,
select {
    /* 1 */
    text-transform: none;
}
​
​
/**
 * Correct the inability to style clickable types in iOS and Safari.
 */
​
button,
[type="button"],
[type="reset"],
[type="submit"] {
    -webkit-appearance: button;
}
​
​
/**
 * Remove the inner border and padding in Firefox.
 */
​
button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {
    border-style: none;
    padding: 0;
}
​
​
/**
 * Restore the focus styles unset by the previous rule.
 */
​
button:-moz-focusring,
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring {
    outline: 1px dotted ButtonText;
}
​
​
/**
 * Correct the padding in Firefox.
 */
​
fieldset {
    padding: 0.35em 0.75em 0.625em;
}
​
​
/**
 * 1. Correct the text wrapping in Edge and IE.
 * 2. Correct the color inheritance from `fieldset` elements in IE.
 * 3. Remove the padding so developers are not caught out when they zero out
 *    `fieldset` elements in all browsers.
 */
​
legend {
    box-sizing: border-box;
    /* 1 */
    color: inherit;
    /* 2 */
    display: table;
    /* 1 */
    max-width: 100%;
    /* 1 */
    padding: 0;
    /* 3 */
    white-space: normal;
    /* 1 */
}
​
​
/**
 * Add the correct vertical alignment in Chrome, Firefox, and Opera.
 */
​
progress {
    vertical-align: baseline;
}
​
​
/**
 * Remove the default vertical scrollbar in IE 10+.
 */
​
textarea {
    overflow: auto;
}
​
​
/**
 * 1. Add the correct box sizing in IE 10.
 * 2. Remove the padding in IE 10.
 */
​
[type="checkbox"],
[type="radio"] {
    box-sizing: border-box;
    /* 1 */
    padding: 0;
    /* 2 */
}
​
​
/**
 * Correct the cursor style of increment and decrement buttons in Chrome.
 */
​
[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
    height: auto;
}
​
​
/**
 * 1. Correct the odd appearance in Chrome and Safari.
 * 2. Correct the outline style in Safari.
 */
​
[type="search"] {
    -webkit-appearance: textfield;
    /* 1 */
    outline-offset: -2px;
    /* 2 */
}
​
​
/**
 * Remove the inner padding in Chrome and Safari on macOS.
 */
​
[type="search"]::-webkit-search-decoration {
    -webkit-appearance: none;
}
​
​
/**
 * 1. Correct the inability to style clickable types in iOS and Safari.
 * 2. Change font properties to `inherit` in Safari.
 */
​
::-webkit-file-upload-button {
    -webkit-appearance: button;
    /* 1 */
    font: inherit;
    /* 2 */
}
​
​
/* Interactive
   ========================================================================== */
​
​
/*
 * Add the correct display in Edge, IE 10+, and Firefox.
 */
​
details {
    display: block;
}
​
​
/*
 * Add the correct display in all browsers.
 */
​
summary {
    display: list-item;
}
​
​
/* Misc
   ========================================================================== */
​
​
/**
 * Add the correct display in IE 10+.
 */
​
template {
    display: none;
}
​
​
/**
 * Add the correct display in IE 10.
 */
​
[hidden] {
    display: none;
}

CSS3 盒子模型 box-sizing

CSS3盒子模型: 盒子的宽度 = CSS中设置的宽度width 里面包含了 border 和 padding,也就是说,CSS3中的盒子模型, padding 和 border 不会撑大盒子了

/*CSS3盒子模型*/
box-sizing: border-box;
/*传统盒子模型*/
box-sizing: content-box;
  • 移动端可以全部CSS3 盒子模型

  • PC端如果完全需要兼容,我们就用传统模式,如果不考虑兼容性,我们就选择 CSS3 盒子模型

特殊样式

    <style>
        div {
            /*CSS3盒子模型*/
            box-sizing: border-box;
            -webkit-box-sizing: border-box;
        }
        
        img,
        a {
            /*需要清除点击高亮,设置为transparent透明*/
            -webkit-tap-highlight-color: transparent;
            /*禁用长按页面时的弹出菜单*/
            -webkit-touch-callout: none;
        }
        
        input {
            /*在移动端浏览器默认的外观在iOS上加上这个属性才能给按钮和输入框自定义样式*/
            -webkit-appearance: none;
        }
    </style>
</head>
​
<body>
    <div></div>
    <a href="#">链接</a>
    <input type="button" value="按钮">
</body>

移动端常见布局

移动端技术选型

  • 单独制作移动端页面(主流)

    • 流式布局(百分比布局)

    • flex 弹性布局(强烈推荐)

    • less+rem+媒体查询布局

    • 混合布局

  • 响应式页面兼容移动端(其次)

    • 媒体查询

    • bootstrap

流式布局(百分比布局)

流式布局,就是百分比布局,也称非固定像素布局

  • 通过盒子的宽度设置成百分比来根据屏幕的宽度来进行伸缩,不受固定像素的限制,内容向两侧填充

  • 主要是看宽度,不看高度

  • 流式布局方式是移动web开发使用的比较常见的布局方式

  • max-width 最大宽度 (max-height 最大高度)

  • min-width 最小宽度 (min-height 最小高度)

京东移动端首页

  • 访问地址:m.jd.com

  1. 技术选型

  • 方案:单独制作移动页面方案

  • 技术:采取流式布局

  1. 搭建相关文件夹结构

image-20211014195706893

  1. 设置视口标签以及引入初始化样式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茗0309

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值