一、移动端开发要点
- 视图,指的是屏幕可以展示的地方
| 属性 | 说明 | | —- | —- | | width | 宽度设置的是viewport宽度,可以设置device-width的特殊值 | | initial-scale | 初始缩放比,大于0的数字,一般取值为1.0 | | maximum-scale | 最大缩放比,大于0的数字,一般取值为1.0 | | minimum-scale | 最小缩放比,大于0的数字,一般取值为1.0 | | user-scalable | 用户是否可以缩放,取值yes或no(1或0) |<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0, user-scalable=no"><meta http-equiv="X-UA-Compatible" content="ie=edge">
- css中的px不等于手机的物理像素值,通常是1px=2物理像素或者1px=3物理像素等等
1.1 二倍图以及多倍图(和背景图缩放方式)
- 一张50*50px的图片,在手机上展开,按照刚才的物理像素比会将图片放大倍数导致图片模糊
- 因此用倍图来解决这个问题
- 通常这样子做:
- 在手机上要想展示清楚的图片,就优先考虑图片在手机上会放大的因素,因此先选用在手机上能高清展示的多倍图,
- 然后手动通过调节css样式,将图片按照进行物理像素比进行缩小即可 ```html img{ /原始能在手机上高清展示的100100px的图片,进行手动按照物理像素比缩小*/ width:50px; heigh:50px; }
.box{ /背景图片缩放/ /可以写百分比,根据父盒子的长度或宽度取百分比值/ /如果只写一个,则默认是宽度,长度根据宽度等比例缩放/ /如果取值cover,则是等比例缩放至完全覆盖div盒子/ /如果取值contain,则是等比例缩放至当高度或宽度铺满div盒子为止/ background-size:50px 50px; }

<a name="JKEQT"></a>## 1.2 移动端开发选择<a name="Agvsg"></a>### (1)移动端css初始化normalize.css- normalize.css:保护了有价值的默认值,修复了浏览器的bug,是模块化的,拥有详细的文档;- 官网:http://necolas.github.io/normalize.css/<a name="rQAFY"></a>### (2)移动端特殊样式- css3盒子模型:- box-sizing:border-box;- -webkit-box-sizing:border-box;- 清除点击高亮效果:- -webkit-tap-highlight-color:transparent;- 取消移动端按钮、输入框默认的样式,以便自定义样式- -webkit-appearance:none;- 禁用长按页面时弹出菜单(尤其是图片和链接)- img,a{ -webkit-touch-callout:none; }<a name="BfdrX"></a>## 3.3 移动端常见的页面布局<a name="wI4vF"></a>### (1)技术选型——单独制作移动端页面(主流)- 说白了就是单独写几个页面,pc端和移动端单独制作,移动端通常在域名中加个“m”<a name="cSRMU"></a>#### 流式布局(百分比布局):- 通过盒子的宽度设置成百分比来进行伸缩,不受固定像素的限制,内容项两侧填充- max-width 最大宽度(max-height 最大高度)- min-width 最小宽度(min-height 最小高度)- 流式布局案例- index.html```html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no,maximum-scale=1.0,minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><!-- 引入我们的css初始化文件 --><link rel="stylesheet" href="css/normalize.css"><!-- 引入我们首页的css --><link rel="stylesheet" href="css/index.css"><title>Document</title></head><body><!-- 顶部 --><header class="app"><ul><li><img src="images/close.png" alt=""></li><li><img src="images/logo.png" alt=""></li><li>打开京东App,购物更轻松</li><li>立即打开</li></ul></header><!-- 搜索 --><div class="search-wrap"><div class="search-btn"></div><div class="search"><div class="jd-icon"></div><div class="sou"></div></div><div class="search-login">登陆</div></div><!-- 主体内容部分 --><div class="main-content"><!-- 滑动图 --><div class="slider"><img src="upload/banner.dpg" alt=""></div><!-- 小家电品牌日 --><div class="brand"><div><a href="#"><img src="upload/pic11.dpg" alt=""></a></div><div><a href="#"><img src="upload/pic22.dpg" alt=""></a></div><div><a href="#"><img src="upload/pic33.dpg" alt=""></a></div></div><!-- nav部分 --><nav class="clearfix"><a href=""><img src="upload/nav1.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav2.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav1.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav1.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav1.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav3.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav1.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav1.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav1.webp" alt=""><span>京东超市</span></a><a href=""><img src="upload/nav1.webp" alt=""><span>京东超市</span></a></nav><!-- 新闻模块 --><div class="news"><a href="#"><img src="upload/new1.dpg" alt=""></a><a href="#"><img src="upload/new2.dpg" alt=""></a><a href="#"><img src="upload/new3.dpg" alt=""></a></div></div></body></html>
- index.css ```css body { width: 100%; min-width: 320px; max-width: 640px; margin: 0 auto; font-size: 14px; font-family: -apple-system, Helvetica, sans-serif; color: #666; line-height: 1.5; }
/点击高亮我们需要清除清除 设置为transparent 完成透明/
- { -webkit-tap-highlight-color: transparent; }
/在移动端浏览器默认的外观在iOS上加上这个属性才能给按钮和输入框自定义样式/
input { -webkit-appearance: none; }
/禁用长按页面时的弹出菜单/
img, a { -webkit-touch-callout: none; }
a { color: #666; text-decoration: none; }
ul { margin: 0; padding: 0; list-style: none; }
img { vertical-align: middle; }
div { / css3 盒子模型 / box-sizing: border-box; }
.clearfix:after { content: “”; display: block; line-height: 0; visibility: hidden; height: 0; clear: both; }
.app { height: 45px; }
.app ul li { float: left; height: 45px; line-height: 45px; background-color: #333333; text-align: center; color: #fff; }
.app ul li:nth-child(1) { width: 8%; }
.app ul li:nth-child(1) img { width: 10px; }
.app ul li:nth-child(2) { width: 10%; }
.app ul li:nth-child(2) img { width: 30px; vertical-align: middle; }
.app ul li:nth-child(3) { width: 57%; }
.app ul li:nth-child(4) { width: 25%; background-color: #F63515; }
/ 搜索 /
.search-wrap { position: fixed; overflow: hidden; width: 100%; height: 44px; min-width: 320px; max-width: 640px; }
.search-btn { position: absolute; top: 0; left: 0; width: 40px; height: 44px; }
.search-btn::before { content: “”; display: block; width: 20px; height: 18px; background: url(../images/s-btn.png) no-repeat; background-size: 20px 18px; margin: 14px 0 0 15px; }
.search-login { position: absolute; right: 0; top: 0; width: 40px; height: 44px; color: #fff; line-height: 44px; }
.search { position: relative; height: 30px; background-color: #fff; margin: 0 50px; border-radius: 15px; margin-top: 7px; }
.jd-icon { width: 20px; height: 15px; position: absolute; top: 8px; left: 13px; background: url(../images/jd.png) no-repeat; background-size: 20px 15px; }
.jd-icon::after { content: “”; position: absolute; right: -8px; top: 0; display: block; width: 1px; height: 15px; background-color: #ccc; }
.sou { position: absolute; top: 8px; left: 50px; width: 18px; height: 15px; background: url(../images/jd-sprites.png) no-repeat -81px 0; background-size: 200px auto; }
.slider img { width: 100%; }
/ 品牌日 /
.brand { overflow: hidden; border-radius: 10px 10px 0 0; }
.brand div { float: left; width: 33.33%; }
.brand div img { width: 100%; }
/ nav /
nav { padding-top: 5px; }
nav a { float: left; width: 20%; text-align: center; }
nav a img { width: 40px; margin: 10px 0; }
nav a span { display: block; }
/ news /
.news { margin-top: 20px; }
.news img { width: 100%; }
.news a { float: left; box-sizing: border-box; }
.news a:nth-child(1) { width: 50%; }
/ .news a:nth-child(2), .news a:nth-child(3), { width: 25%; } /
/ n+2 就是从从2个往后面选 /
.news a:nth-child(n+2) { width: 25%; border-left: 1px solid #ccc; }
/ .news a:nth-child(3) { width: 25%; } /
- 初始化样式文件normalize.css```css/*! normalize.css v5.0.0 | MIT License | github.com/necolas/normalize.css *//*** 1. Change the default font family in all browsers (opinionated).* 2. Correct the line height in all browsers.* 3. Prevent adjustments of font size after orientation changes in* IE on Windows Phone and in iOS.*//* Document========================================================================== */html {font-family: sans-serif; /* 1 */line-height: 1.15; /* 2 */-ms-text-size-adjust: 100%; /* 3 */-webkit-text-size-adjust: 100%; /* 3 */}/* Sections========================================================================== *//*** Remove the margin in all browsers (opinionated).*/body {margin: 0;}/*** Add the correct display in IE 9-.*/article,aside,footer,header,nav,section {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========================================================================== *//*** Add the correct display in IE 9-.* 1. Add the correct display in IE.*/figcaption,figure,main { /* 1 */display: block;}/*** Add the correct margin in IE 8.*/figure {margin: 1em 40px;}/*** 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========================================================================== *//*** 1. Remove the gray background on active links in IE 10.* 2. Remove gaps in links underline in iOS 8+ and Safari 8+.*/a {background-color: transparent; /* 1 */-webkit-text-decoration-skip: objects; /* 2 */}/*** Remove the outline on focused links when they are also active or hovered* in all browsers (opinionated).*/a:active,a:hover {outline-width: 0;}/*** 1. Remove the bottom border in Firefox 39-.* 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 */}/*** Prevent the duplicate application of `bolder` by the next rule in Safari 6.*/b,strong {font-weight: inherit;}/*** 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 style in Android 4.3-.*/dfn {font-style: italic;}/*** Add the correct background and color in IE 9-.*/mark {background-color: #ff0;color: #000;}/*** 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========================================================================== *//*** Add the correct display in IE 9-.*/audio,video {display: inline-block;}/*** Add the correct display in iOS 4-7.*/audio:not([controls]) {display: none;height: 0;}/*** Remove the border on images inside links in IE 10-.*/img {border-style: none;}/*** Hide the overflow in IE.*/svg:not(:root) {overflow: hidden;}/* Forms========================================================================== *//*** 1. Change the font styles in all browsers (opinionated).* 2. Remove the margin in Firefox and Safari.*/button,input,optgroup,select,textarea {font-family: sans-serif; /* 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;}/*** 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`* controls in Android 4.* 2. Correct the inability to style clickable types in iOS and Safari.*/button,html [type="button"], /* 1 */[type="reset"],[type="submit"] {-webkit-appearance: button; /* 2 */}/*** 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;}/*** Change the border, margin, and padding in all browsers (opinionated).*/fieldset {border: 1px solid #c0c0c0;margin: 0 2px;padding: 0.35em 0.625em 0.75em;}/*** 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 */}/*** 1. Add the correct display in IE 9-.* 2. Add the correct vertical alignment in Chrome, Firefox, and Opera.*/progress {display: inline-block; /* 1 */vertical-align: baseline; /* 2 */}/*** Remove the default vertical scrollbar in IE.*/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 and cancel buttons in Chrome and Safari on macOS.*/[type="search"]::-webkit-search-cancel-button,[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 IE 9-.* 1. Add the correct display in Edge, IE, and Firefox.*/details, /* 1 */menu {display: block;}/** Add the correct display in all browsers.*/summary {display: list-item;}/* Scripting========================================================================== *//*** Add the correct display in IE 9-.*/canvas {display: inline-block;}/*** Add the correct display in IE.*/template {display: none;}/* Hidden========================================================================== *//*** Add the correct display in IE 10-.*/[hidden] {display: none;}
flex弹性布局(强烈推荐):
- 采用flex布局的元素,称之为flex容器,简称“容器”,它的所有的子元素自动成为容器的一员,称为flex项目,简称“项目”
- 子元素就可以纵向、横向排列了
- 当我们为父元素添加display=flex之后,子元素的float、clear、vertical-align将失效
- 常见的父项元素属性(设置在父元素中的属性)
- flex-direction:设置主轴方向(设置子元素按行排列还是列排列),取值为:row(默认从左到右排列);row-reverse(从右到左);column(从上到下);column-reverse(从下到上)
- justify-content:属性定义了项目在主轴上的对齐方式;使用该属性的前提是确定好主轴排列的方式;取值为:flex-start(默认从头开始排列,如果是主轴x轴则是从左到右);flex-end(从尾部开始排列);center(居中排列);space-around(平分剩余空间);space-between(先两边贴边,再;平分剩余空间)
- flex-wrap:决定子元素是否换行,取值为nowrap(默认值不换行);wrap(换行);当装不下子元素,则将子元素收缩
- align-items:设置侧轴上的元素排列方式(单行),取值为:flex-start(默认从头开始排列,如果是主轴x轴则是从左到右);flex-end(从尾部开始排列);center(挤在一起居中(垂直居中));stretch(拉伸默认值,前提是不设置盒子高度)
- flex-flow属性是flex-direction和flex-wrap属性的属性复合:flex-flow:row wrap
- 常见子项元素属性:
- flex属性:定义子项元素分配父元素剩余空间:.item{ flex: num(数字) }
- align-self属性:控制子项元素自己在侧轴上的排列方式,允许单个项目有与其他项目不一样的对齐方式,可以覆盖align-items属性,默认值是auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。取值跟align-items属性一样;
- order属性:定义子项目的排列顺序;数值越小排列越靠前,默认是零;
- flex弹性布局案例
- index.html ```html <!DOCTYPE html>
热门活动
获取更多福利
- index.css```cssbody {max-width: 540px;min-width: 320px;margin: 0 auto;font: normal 14px/1.5 Tahoma, "Lucida Grande", Verdana, "Microsoft Yahei", STXihei, hei;color: #000;background: #f2f2f2;overflow-x: hidden;-webkit-tap-highlight-color: transparent;}ul {list-style: none;margin: 0;padding: 0;}a {text-decoration: none;color: #222;}div {box-sizing: border-box;}/* 搜索模块 */.search-index {display: flex;/* 固定定位跟父级没有关系 它以屏幕为准 */position: fixed;top: 0;left: 50%;/* 固定的盒子应该有宽度 */-webkit-transform: translateX(-50%);transform: translateX(-50%);width: 100%;min-width: 320px;max-width: 540px;height: 44px;/* background-color: pink; */background-color: #F6F6F6;border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;}.search {position: relative;height: 26px;line-height: 24px;border: 1px solid #ccc;flex: 1;font-size: 12px;color: #666;margin: 7px 10px;padding-left: 25px;border-radius: 5px;box-shadow: 0 2px 4px rgba(0, 0, 0, .2);}.search::before {content: "";position: absolute;top: 5px;left: 5px;width: 15px;height: 15px;background: url(../images/sprite.png) no-repeat -59px -279px;background-size: 104px auto;}.user {width: 44px;height: 44px;/* background-color: purple; */font-size: 12px;text-align: center;color: #2eaae0;}.user::before {content: "";display: block;width: 23px;height: 23px;background: url(../images/sprite.png) no-repeat -59px -194px;background-size: 104px auto;margin: 4px auto -2px;}/* focus */.focus {padding-top: 44px;}.focus img {width: 100%;}/* local-nav */.local-nav {display: flex;height: 64px;margin: 3px 4px;background-color: #fff;border-radius: 8px;}.local-nav li {flex: 1;}.local-nav a {display: flex;flex-direction: column;/* 侧轴居中对齐 因为是单行 */align-items: center;font-size: 12px;}.local-nav li [class^="local-nav-icon"] {width: 32px;height: 32px;background-color: pink;margin-top: 8px;background: url(../images/localnav_bg.png) no-repeat 0 0;background-size: 32px auto;}.local-nav li .local-nav-icon-icon2 {background-position: 0 -32px;}.local-nav li .local-nav-icon-icon3 {background-position: 0 -64px;}.local-nav li .local-nav-icon-icon4 {background-position: 0 -96px;}.local-nav li .local-nav-icon-icon5 {background-position: 0 -128px;}/* nav */nav {overflow: hidden;border-radius: 8px;margin: 0 4px 3px;}.nav-common {display: flex;height: 88px;background-color: pink;}.nav-common:nth-child(2) {margin: 3px 0;}.nav-items {/* 不冲突的 */flex: 1;display: flex;flex-direction: column;}.nav-items a {flex: 1;text-align: center;line-height: 44px;color: #fff;font-size: 14px;/* 文字阴影 */text-shadow: 1px 1px rgba(0, 0, 0, .2);}.nav-items a:nth-child(1) {border-bottom: 1px solid #fff;}.nav-items:nth-child(1) a {border: 0;background: url(../images/hotel.png) no-repeat bottom center;background-size: 121px auto;}/* -n+2就是选择前面两个元素 */.nav-items:nth-child(-n+2) {border-right: 1px solid #fff;}.nav-common:nth-child(1) {background: -webkit-linear-gradient(left, #FA5A55, #FA994D);}.nav-common:nth-child(2) {background: -webkit-linear-gradient(left, #4B90ED, #53BCED);}.nav-common:nth-child(3) {background: -webkit-linear-gradient(left, #34C2A9, #6CD559);}/* subnav-entry */.subnav-entry {display: flex;border-radius: 8px;background-color: #fff;margin: 0 4px;flex-wrap: wrap;padding: 5px 0;}.subnav-entry li {/* 里面的子盒子可以写 % 相对于父级来说的 */flex: 20%;}.subnav-entry a {display: flex;flex-direction: column;align-items: center;}.subnav-entry-icon {width: 28px;height: 28px;background-color: pink;margin-top: 4px;background: url(../images/subnav-bg.png) no-repeat;background-size: 28px auto;}/* sales-box */.sales-box {border-top: 1px solid #bbb;background-color: #fff;margin: 4px;}.sales-hd {position: relative;height: 44px;border-bottom: 1px solid #ccc;}.sales-hd h2 {position: relative;text-indent: -999px;overflow: hidden;}.sales-hd h2::after {position: absolute;top: 5px;left: 8px;content: "";width: 79px;height: 15px;background: url(../images/hot.png) no-repeat 0 -20px;background-size: 79px auto;}.more {position: absolute;right: 5px;top: 0px;background: -webkit-linear-gradient(left, #FF506C, #FF6BC6);border-radius: 15px;padding: 3px 20px 3px 10px;color: #fff;}.more::after {content: "";position: absolute;top: 9px;right: 9px;width: 7px;height: 7px;border-top: 2px solid #fff;border-right: 2px solid #fff;transform: rotate(45deg);}.row {display: flex;}.row a {flex: 1;border-bottom: 1px solid #eee;}.row a:nth-child(1) {border-right: 1px solid #eee;}.row a img {width: 100%;}
less+rem+媒体查询布局:
- 什么是rem?rem跟px一样是个单位,相对于html元素字体大小来说的,1rem=html中的字体像素大小,之前学过的em则是相对于父元素的字体大小来说的
- 优点:可以通过修改html元素文字大小来改变页面中的元素大小,可以整体控制;
- 什么是媒体查询?媒体查询(media query)是css3的语法
- 特点:使用@media查询,可以针对不同的媒体类型定义不同的样式;@media可以针对不同屏幕尺寸设置不同的样式;当你重置浏览器大小的过程中,页面也会根据浏览器的宽度和高度重新渲染页面;目前针对很多苹果手机、adroit手机,平板等设备都用到多媒体查询;
语法:@media mediatype and|not|only (media feature){ css code; } 其中mediatype有all(所有设备)、print(打印机和打印预览)、scree(电脑屏幕、平板电脑、智能手机等等);关键字and(且)、not(非)、only(仅仅只);media feature(媒体特性width输出设备中页面可见区域的宽度)、min-width(输出设备中页面最小可见区域宽度)、max-width(输出设备中页面最大可见区域宽度)
@media mediatype and|not|only (media feature1) and|not|only(media feature2){css code;}
理解:个人感觉有点像if判断,满足该条件就进入执行里面的样式代码,小于最大可见宽度则满足,大于最小可见宽度则满足
引入页源:
- 当样式比较频繁时,可以针对不同媒体使用不同stylesheets(样式表)
- 原理:就是直接在link标签中判断设备的尺寸,然后引入不同的css样式文件
- 语法:
<link rel="stylesheet" media="mediatype and|not|only(media feature)" href="引入的样式文件路径">
less的认识
- 它是一门css扩展语言,也成为css预处理器,加入了程序式语言的特性,引入了变量、Mixin(混入)、运算及函数等功能,大大简化了css的编写,并且降低了css的维护成本;
- 官网:http://lesscss.cn/
- 常见的css预处理器:sass、less、stylus
- less是一门css预处理器语言,它扩展了css的动态特性;
less的使用:
- 把它编写在后缀名为less的文件中;
less变量:@变量名:值; 变量命名规范,不能包含特殊字符,不能以数字开头大小写敏感
//变量书写格式@变量名:值;@color:pink;//变量引用body{background-color:@color;}
less编译:将less语句编译成css代码,通过vscode插件——easy less实现;安装好该插件之后,重启vscode即可,之后只要将.less文件保存即可自动编译生成.css文件;
- less嵌套:后代选择器直接在嵌套写即可;伪类选择器在前面加个“&”即可
- less运算:直接在less文件中做加减乘除运算即可,两个数有单位且不同则以第一个为准,只有一个数有单位则以有单位的为准,注意,除法运算需要加括号,相关运算函数
- escape(@string); // 通过 URL-encoding 编码字符串
e(@string); // 对字符串转义
%(@string, values…); // 格式化字符串
unit(@dimension, [@unit: “”]); // 移除或替换属性值的单位
color(@string); // 将字符串解析为颜色值
data-uri([mimetype,] url); // 将资源内嵌到css中,可能回退到url()
ceil(@number); // 向上取整
floor(@number); // 向下取整
percentage(@number); // 将数字转换为百分比,例如 0.5 -> 50%
round(number, [places: 0]); // 四舍五入取整
sqrt(number); // 计算数字的平方根
abs(number); // 数字的绝对值
sin(number); // sin函数
asin(number); // arcsin函数
cos(number); // cos函数
acos(number); // arccos函数
tan(number); // tan函数
atan(number); // arctan函数
pi(); // 返回PI
pow(@base, @exponent); // 返回@base的@exponent次方
mod(number, number); // 第一个参数对第二个参数取余
convert(number, units); // 在数字之间转换
unit(number, units); // 不转换的情况下替换数字的单位
color(string); // 将字符串或者转义后的值转换成颜色
rgb(@r, @g, @b); // 转换为颜色值
rgba(@r, @g, @b, @a); // 转换为颜色值
argb(@color); // 创建 #AARRGGBB 格式的颜色值
hsl(@hue, @saturation, @lightness); // 创建颜色值
hsla(@hue, @saturation, @lightness, @alpha); // 创建颜色值
hsv(@hue, @saturation, @value); // 创建颜色值
hsva(@hue, @saturation, @value, @alpha); // 创建颜色值
hue(@color); // 从颜色值中提取 hue 值(色相)
saturation(@color); // 从颜色值中提取 saturation 值(饱和度)
lightness(@color); // 从颜色值中提取 ‘lightness’ 值(亮度)
hsvhue(@color); // 从颜色中提取 hue 值,以HSV色彩空间表示(色相)
hsvsaturation(@color); // 从颜色中提取 saturation 值,以HSV色彩空间表示(饱和度)
hsvvalue(@color); // 从颜色中提取 value 值,以HSV色彩空间表示(色调)
red(@color); // 从颜色值中提取 ‘red’ 值(红色)
green(@color); // 从颜色值中提取 ‘green’ 值(绿色)
blue(@color); // 从颜色值中提取 ‘blue’ 值(蓝色)
alpha(@color); // 从颜色值中提取 ‘alpha’ 值(透明度)
luma(@color); // 从颜色值中提取 ‘luma’ 值(亮度的百分比表示法)
saturate(@color, 10%); // 饱和度增加 10%
desaturate(@color, 10%); // 饱和度降低 10%
lighten(@color, 10%); // 亮度增加 10%
darken(@color, 10%); // 亮度降低 10%
fadein(@color, 10%); // 透明度增加 10%
fadeout(@color, 10%); // 透明度降低 10%
fade(@color, 50%); // 设定透明度为 50%
spin(@color, 10); // 色相值增加 10
mix(@color1, @color2, [@weight: 50%]); // 混合两种颜色
greyscale(@color); // 完全移除饱和度,输出灰色
contrast(@color1, [@darkcolor: black], [@lightcolor: white], [@threshold: 43%]); // 如果 @color1 的 luma 值 > 43% 输出 @darkcolor,否则输出 @lightcolor
multiply(@color1, @color2);
screen(@color1, @color2);
overlay(@color1, @color2);
softlight(@color1, @color2);
hardlight(@color1, @color2);
difference(@color1, @color2);
exclusion(@color1, @color2);
average(@color1, @color2);
negation(@color1, @color2);
iscolor(@colorOrAnything); // 判断一个值是否是颜色
isnumber(@numberOrAnything); // 判断一个值是否是数字(可含单位)
isstring(@stringOrAnything); // 判断一个值是否是字符串
iskeyword(@keywordOrAnything); // 判断一个值是否是关键字
isurl(@urlOrAnything); // 判断一个值是否是url
ispixel(@pixelOrAnything); // 判断一个值是否是以px为单位的数值
ispercentage(@percentageOrAnything); // 判断一个值是否是百分数
isem(@emOrAnything); // 判断一个值是否是以em为单位的数值
isunit(@numberOrAnything, “rem”); // 判断一个值是否是指定单位的数值
含*号的函数只在1.4.0 beta以上版本中可用
- rem适配方案:
- 原理:
- 让一些不能等比自适应的元素,达到当设备尺寸发生改变时,等比适配当前设备;
- 使用媒体查询根据不同的设备按比例设置html的字体大小,然后页面元素使用rem单位做尺寸;当html字体大小变化时元素尺寸也发生变化,从而达到等比缩放的适配效果
- 总结:按照设计稿与设备宽度的比例,动态计算并设置html根标签的font-size大小;(媒体查询);css中,设计稿元素的宽、高、相对位置等取值按照同比例换算为rem为单位的值;
- 方案一:
- less
- 媒体查询
- rem
- 实现:①假设设计稿是750px;假设我们把整个屏幕分为15等分(划分标准不一定可以是20份也可以是10等分);②每一份作为html字体大小,这里就是50px;③那么在320ppx的设备时,字体带线啊哦为320/15就是21.33px;④用我们页面元素的大小除以不同的html字体大小会发现他们是等比例缩放的;
- 最终公式:页面元素的rem值 = 页面元素值(px) / (屏幕宽度 / 划分的份数); 其中:屏幕宽度 / 划分的份数 = html的font-size
- 苏宁首页案例:
- common.less文件 ```less // 设置常见的屏幕尺寸 修改里面的html文字大小 a { text-decoration: none; } // 一定要写到最上面 html { font-size: 50px; } // 我们此次定义的划分的份数 为 15 @no: 15; // 320 @media screen and (min-width: 320px) { html { font-size: 320px / @no; } } // 360 @media screen and (min-width: 360px) { html { font-size: 360px / @no; } } // 375 iphone 678 @media screen and (min-width: 375px) { html { font-size: 375px / @no; } }
- 原理:
// 384 @media screen and (min-width: 384px) { html { font-size: 384px / @no; } }
// 400 @media screen and (min-width: 400px) { html { font-size: 400px / @no; } } // 414 @media screen and (min-width: 414px) { html { font-size: 414px / @no; } } // 424 @media screen and (min-width: 424px) { html { font-size: 424px / @no; } }
// 480 @media screen and (min-width: 480px) { html { font-size: 480px / @no; } }
// 540 @media screen and (min-width: 540px) { html { font-size: 540px / @no; } } // 720 @media screen and (min-width: 720px) { html { font-size: 720px / @no; } }
// 750 @media screen and (min-width: 750px) { html { font-size: 750px / @no; } }
- index.css```css// 首页的样式less文件@import "common";// @import 导入的意思 可以把一个样式文件导入到另外一个样式文件里面// link 是把一个 样式文件引入到 html页面里面body {min-width: 320px;width: 15rem;margin: 0 auto;line-height: 1.5;font-family: Arial,Helvetica;background: #F2F2F2;}// 页面元素rem计算公式: 页面元素的px / html 字体大小 50// search-content@baseFont: 50;.search-content {display: flex;position: fixed;top: 0;left: 50%;transform: translateX(-50%);width: 15rem;height: 88rem / @baseFont;background-color:#FFC001;.classify {width: 44rem / @baseFont;height: 70rem / @baseFont;margin: 11rem / @baseFont 25rem / @baseFont 7rem / @baseFont 24rem / @baseFont;background: url(../images/classify.png) no-repeat;// 背景缩放background-size: 44rem / @baseFont 70rem / @baseFont;}.search {flex: 1;input {outline: none;width: 100%;border: 0;height: 66rem / @baseFont;border-radius: 33rem / @baseFont;background-color:#FFF2CC;margin-top: 12rem / @baseFont;font-size: 25rem / @baseFont;padding-left: 55rem / @baseFont;color: #757575;}}.login {width: 75rem / @baseFont;height: 70rem / @baseFont;line-height: 70rem / @baseFont;margin: 10rem / @baseFont;font-size: 25rem / @baseFont;text-align: center;color: #fff;}}// banner.banner {width: 750rem / @baseFont;height: 368rem / @baseFont;img {width: 100%;height: 100%;}}// ad.ad {display: flex;a {flex: 1;img {width: 100%;}}}// navnav {width: 750rem / @baseFont;a {float: left;width: 150rem / @baseFont;height: 140rem / @baseFont;text-align: center;img {display: block;width: 82rem / @baseFont;height: 82rem / @baseFont;margin: 10rem / @baseFont auto 0;}span {font-size: 25rem / @baseFont;color: #333;}}}
- index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /><meta http-equiv="X-UA-Compatible" content="ie=edge"><link rel="stylesheet" href="css/normalize.css"><link rel="stylesheet" href="css/index.css"><title>Document</title></head><body><!-- 顶部搜索框 --><div class="search-content"><a href="#" class="classify"></a><div class="search"><form action=""><input type="search" value="厨卫保暖季 哒哒哒"></form></div><a href="#" class="login">登录</a></div><!-- banner部分 --><div class="banner"><img src="upload/banner.gif" alt=""></div><!-- 广告部分 --><div class="ad"><a href="#"><img src="upload/ad1.gif" alt=""></a><a href="#"><img src="upload/ad2.gif" alt=""></a><a href="#"><img src="upload/ad3.gif" alt=""></a></div><!-- nav模块 --><nav><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a><a href="#"><img src="upload/nav1.png" alt=""><span>爆款手机</span></a></nav></body></html>
- 方案二(推荐):- flexble.js- rem- 网址:https://github.com/amfe/lib-flexible
(2)技术选型——响应式页面兼容移动端(其次)
- 响应式页面是根据浏览器的宽度进行实时样式改变,这种方式要花费大量精力编写浏览器兼容的样式代码
原理:使用媒体查询针对不同的宽度的设备进行布局和样式的设置,从而适配不同的设备; | 设备划分 | 尺寸区间 | | —- | —- | | 超小屏幕(手机) | width<768px | | 小屏设备(平板) | 768px<=width<992px | | 中等屏幕(桌面显示器) | 992px<=width<1200px | | 宽屏设备(大桌名设备显示器) | width>=1200px |
媒体查询布局:
- 准备一个父级作为布局容器,来配合自己元素实现变化效果。原理就是在不同的屏幕下,通过媒体查询的方式来改变这个布局容器的大小,再改变子元素的排列方式和大小,从而实现不同屏幕下,看到不同的页面布局和样式变化;
- 超小屏幕:设置布局容器宽度为100%;
- 小屏幕:设置布局容器宽度为750px;
- 中等屏幕:设置布局容器宽度为970px;
- 大屏幕:设置布局容器宽度为1170px;
- bootstarp:来自于推特;是目前最受欢迎的前端框架,它基于html、css和javascript的,简介灵活的特性使得web开发更加快捷
- 中文官网:http://www.bootcss.com
- 官网:http://getbootstrap.com
- 推荐网址:http://bootstrap.css88.com
栅格系统:
其用于通过一系列的行(row)和列(column)的组合来创建页面布局 | | 超小屏幕 | 小屏幕 | 中等屏幕 | 大屏幕 | | —- | —- | —- | —- | —- | | .container的最大宽度 | 设置布局容器宽度为100% | 设置布局容器宽度为750px | 设置布局容器宽度为970px | 设置布局容器宽度为1170px | | 类前缀 | .col-xs- | .col-sm- | .col-md- | .col-lg | | 列数(column) | 12列 | | | |
