web移动端开发技巧与注意事项

<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post" style="height: 1059.6px; overflow: hidden;">
								<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
								            <div id="content_views" class="markdown_views prism-atom-one-dark">
							<!-- flowchart 箭头图标 勿删 -->
							<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
							<p><strong>一、meta的使用</strong></p>
<p>1、<code>&lt;meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/&gt;</code><br>
强制让文档的宽度与设备的宽度保持1:1,并且文档最大的宽度比例是1.0,且不允许用户点击屏幕放大浏览。<br>
2、winphone系统a、input标签被点击时产生的半透明灰色背景怎么去掉:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">&lt;meta name="msapplication-tap-highlight" content="no"&gt;
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>3、忽略页面的数字为电话,忽略email识别 :</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">&lt;meta name="format-detection" content="telephone=no, email=no"/&gt;
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p><strong>二、针对适配等比缩放的方法:</strong></p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">@media only screen and (min-width: 1024px){
  body{zoom:3.2;}
}
@media only screen and (min-width: 768px) and (max-width: 1023px) {
  body{zoom:2.4;}
}
@media only screen and (min-width: 640px) and (max-width: 767px) {
  body{zoom:2;}
}
@media only screen and (min-width: 540px) and (max-width: 639px) {
  body{zoom:1.68;}
}
@media only screen and (min-width: 480px) and (max-width: 539px) {
  body{zoom:1.5;}
}
@media only screen and (min-width: 414px) and (max-width: 479px) {
  body{zoom:1.29;}
}
@media only screen and (min-width: 400px) and (max-width: 413px) {
  body{zoom:1.25;}
}
@media only screen and (min-width: 375px) and (max-width: 413px) {
  body{zoom:1.17;}
}
@media only screen and (min-width: 360px) and (max-width:374px) {
  body{zoom:1.125;}
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li></ul></pre>
<p>或如:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">@media all and (orientation : landscape) { 
  h2{color:red;}/*横屏时字体红色*/
} 

@media all and (orientation : portrait){ 
  h2{color:green;}/*竖屏时字体绿色*/
} 
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li></ul></pre>
<p><strong>三、布局</strong></p>
<p>1.布局使用百分比:<br>
  不同的手机有着不同的分辨率,这时再用我们pc端布局常用的px就不合适了。使用百分比布局要时时刻刻清楚其父元素,因为子元素的百分比高度是根据父元素的高度来确定的,当父元素的高度为不确定值时,或者说父元素的高度未定义时,子元素的高度百分比将没有用(没有参照物)。所以只有设置了父元素的高度,子元素的高度百分比才会有用。<br>
  <br>
2.em与rem:<br>
  <code>em</code>是根据相对单位,不是固定的,他会继承父级元素的字体大小,若没有父级则em的相对基准点为浏览器的字体大小,浏览器的字体默认为16px,因此若无父级元素,相对于浏览器大小:<code>Xem=X*16px;</code><br>
<code>rem</code>是css3新增属性,是完全相对于HTML根元素大小设定的,默认为10px,因此无论父级字体大小,<code>1rem=10px</code>。</p>
<p>3.栅格布局:<br>
  <code>box-sizing:border-box;</code>可以改变盒子模型的计算方式方便你设置宽进行自适应流式布局。<br>
  <br>
4、wap页面有<code>img</code>标签,记得加上<code>display:block;</code>属性来解决img的边缘空白间隙的1px像素。如果图片要适应不同的手机要设置<code>width:100%;</code>而且不能添加高度。</p>
<p>5、有关<code>Flexbox</code>弹性盒子布局一些属性<br>
(1)不定宽高的水平垂直居中</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.xxx{ 
  position:absolute; 
  top:50%; 
  left:50%; 
  z-index:3; 
  -webkit-transform:translate(-50%,-50%); 
  border-radius:6px; 
  background:#fff; 
} 
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre>
<p>(2)[<code>flexbox</code>版]不定宽高的水平垂直居中</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.xx{ 
 justify-content:center;//子元素水平居中, 
 align-items:center;//子元素垂直居中; 
 display:-webkit-flex; 
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li></ul></pre>
<p><strong>四、文本的处理</strong></p>
<p>1、关闭iOS键盘首字母自动大写<code>&lt;input type="text" autocapitalize="off" /&gt;</code></p>
<p>2、//单行文本溢出</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.xx{
  overflow:hidden;
  white-space:nowrap;
  text-overflow:ellipsis;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li></ul></pre>
<p>3、//多行文本溢出</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.xx{
  display:-webkit-box !importmort;
  overflow:hidden;
  text-overflow:ellipsis;
  word-break:break-all;
  -webkit-box-orient:vertical;
  -webkit-line-clamp:2;(数字2表示隐藏两行)
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre>
<p>4、</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">html {
 -webkit-text-size-adjust: 100%;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
<p><strong>五、图片、媒体的处理</strong><br>
1、//使用流体图片</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">img{
  width:100%;
  height:auto;
  width:auto\9;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li></ul></pre>
<p>2、<code>audio</code>元素和<code>video</code>元素在ios和andriod中无法自动播放<br>
  应对方案:触屏即播</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">$('html').one('touchstart',function(){
  audio.play()
})
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
<p>3、如何禁止保存或拷贝图像<br>
  通常当你在手机或者<code>pad</code>上长按图像 <code>img</code> ,会弹出选项 存储图像 或者 拷贝图像,如果你不想让用户这么操作,那么你可以通过以下方法来禁止:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">img {
  -webkit-touch-callout: none;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
<p>PS:需要注意的是,该方法只在 iOS 上有效。</p>
<p><strong>六、阴影的处理</strong><br>
1、 移动端如何清除输入框内阴影<br>
  在iOS上,输入框默认有内部阴影,但无法使用 <code>box-shadow</code> 来清除,如果不需要阴影,可以这样关闭:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">input,textarea {
  border: 0;
  -webkit-appearance: none;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre>
<p><strong>七、字体的处理</strong><br>
对于网站字体设置<br>
1、移动端项目:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">font-family:Tahoma,Arial,Roboto,"Droid Sans","Helvetica Neue","Droid Sans Fallback","Heiti SC",sans-self; 
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>2、移动和pc端项目:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">font-family:Tahoma,Arial,Roboto,"Droid Sans","Helvetica Neue","Droid Sans Fallback","Heiti SC","Hiragino Sans GB",Simsun,sans-self;
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>3、字体大小尽量使用<code>pt</code>或者<code>em</code>,<code>rem</code>,代替<code>px</code>。</p>
<p>4、设置input里面placeholder字体的大小<code>::-webkit-input-placeholder{ font-size:10pt}</code></p>
<p>5、解决字体在移动端比例缩小后出现锯齿的问题:<code>-webkit-font-smoothing: antialiased;</code></p>
<p><strong>八、圆角设置</strong></p>
<p>放一个图片或者一个按钮,设置圆角会比较美观,设置圆角的值可以用百分比,也可以用em等单位。</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">element{
  border: 1px solid #ccc;
  -moz-border-radius: 百分比;
  -webkit-border-radius: 百分比;
  border-radius: 百分比;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre>
<p><strong>九、边距凹陷</strong></p>
<p>1、像素边框(例子:移动端列表的下边框)</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.list-iteam:after{
  position: absolute;
  left: 0px;
  right: 0px;
  content: '';
  height: 1px;
  transform: scaleY(0.5);
  -moz-transform: scaleY(0.5);
  -webkit-transform:scaleY(0.5);
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre>
<p>2、与在pc端开发一样,开发过程中需要的一个很需要注意的问题的边距塌陷,典型的问题是<code>margin-top</code>的嵌套,对子元素设置<code>margin-top</code>值,子元素相对于父元素的位置没有变,而父元素跟着子元素一起向下移动响应的距离。解决方案:  <br>
(1) 给父元素<code>div1</code>设置一个<code>padding</code>值</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.div1{
  height: 500px;
  width: 100%;
  background: #ccc;
  overflow: hidden;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre>
<p><strong>十、禁止内容</strong></p>
<p>1、//禁止文本缩放:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">html {
    -webkit-text-size-adjust: 100%;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
<p>2、移动端禁止选中内容<br>
如果你不想用户可以选中页面中的内容,那么你可以在css中禁掉:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.user-select-none {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}    
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li></ul></pre>
<p>兼容<code>IE6-9</code>的写法:<code>onselectstart="return false;" unselectable="on"</code></p>
<p><strong>十一、滚动效果</strong></p>
<p><strong>十二、快速回弹</strong><br>
快速回弹滚动</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.xxx {
   overflow: auto;
   -webkit-overflow-scrolling: touch;
 }
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre>
<p>PS:<code>iScroll</code>用过之后感觉不是很好,有一些诡异的bug,这里推荐另外一个 <code>iDangero Swiper</code>,这个插件集成了滑屏滚动的强大功能(支持3D),而且还有回弹滚动的内置滚动条,官方地址: <a href="http://www.idangero.us/sliders/swiper/index.php" rel="nofollow" target="_blank">http://www.idangero.us/sliders/swiper/index.php</a></p>
<p><strong>十三、白色背景颜色搭配</strong></p>
<p><strong>十四、常用的移动端开发框架以及工具</strong></p>
<p>框架<br>
1 . 移动端基础框架<br>
  <code>zepto.js</code> 语法与jquery几乎一样,会jquery基本会zepto。<br>
  <code>iscroll.js</code> 解决页面不支持弹性滚动,不支持fixed引起的问题~ 实现下拉刷新,滑屏,缩放等功能。<br>
  <code>underscore.js</code> 该库提供了一整套函数式编程的实用功能,但是没有扩展任何JavaScript内置对象。<br>
  <code>fastclick</code> 加快移动端点击响应时间 。<br>
  <code>animate.css</code> CSS3动画效果库 。<br>
  <code>Normalize.css Normalize.css</code>是一种现代的、CSS reset为HTML5准备的优质替代方案 。<br>
2 . 滑屏框架<br>
  适合上下滑屏、左右滑屏等滑屏切换页面的效果 。</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">slip.js 
iSlider.js 
fullpage.js 
swiper 
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre>
<p>3 .瀑布流框架</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">masonry
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>工具推荐 :<br>
  <code>caniuse</code> 各浏览器支持html5属性查询 。<br>
  <code>paletton</code> 调色搭配。<br>
  <br>
<strong>十五、动画的处理</strong><br>
开启硬件加速<br>
解决页面闪白<br>
保证动画流畅</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.css {
  -webkit-transform: translate3d(0, 0, 0);
  -moz-transform: translate3d(0, 0, 0);
  -ms-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre>
<p>设计高性能<code>CSS3</code>动画的几个要素<br>
尽可能地使用合成属性<code>transform</code>和<code>opacity</code>来设计<code>CSS3</code>动画,<br>
不使用<code>position</code>的<code>left</code>和<code>top</code>来定位<br>
利用<code>translate3D</code>开启<code>GPU</code>加速</p>
<p><strong>十六、消除闪烁</strong><br>
消除<code>transition</code>闪屏</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.css{
   -webkit-transform-style: preserve-3d;
   -webkit-backface-visibility: hidden;
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre>
<p><strong>十七、移动端取消<code>touch</code>高亮效果</strong><br>
  在做移动端页面时,会发现所有<code>a</code>标签在触发点击时或者所有设置了伪类 :<code>active</code> 的元素,默认都会在激活状态时,显示高亮框,如果不想要这个高亮,那么你可以通过css以下方法来禁止:</p>
<pre class="prettyprint"><code class="has-numbering" "mdcp.signin(event)">.xxx {
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
<div class="hljs-button signin" data-title="登录后复制"></div></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>

            </div>
						<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" rel="stylesheet">
                      </div>

猜你喜欢

转载自blog.csdn.net/weixin_44392027/article/details/88959019