zoukankan      html  css  js  c++  java
  • 移动端H5开发常用技巧

    html 篇

    常用的meta属性设置

    <meta name="screen-orientation" content="portrait"> //Android 禁止屏幕旋转
    <meta name="full-screen" content="yes">             //全屏显示
    <meta name="browsermode" content="application">     //UC应用模式,使用了application这种应用模式后,页面讲默认全屏,禁止长按菜单,禁止收拾,标准排版,以及强制图片显示。
    <meta name="x5-orientation" content="portrait">     //QQ强制竖屏
    <meta name="x5-fullscreen" content="true">          //QQ强制全屏
    <meta name="x5-page-mode" content="app">            //QQ应用模式

    邮箱识别(Android)

    安卓上会对符合邮箱格式的字符串进行识别,我们可以通过如下的 meta 来管别邮箱的自动识别:

    <meta content="email=no" name="format-detection" />

    通过标签属性来开启长按邮箱地址弹出邮件发送的功能:

    <a mailto:dooyoe@gmail.com">dooyoe@gmail.com</a>

    0.5px细线

     1px 的边框。在高清屏下,移动端的 1px 会很粗。

    那么为什么会产生这个问题呢?因为,DPR(devicePixelRatio) 设备像素比,它是默认缩放为 100%的情况下,设备像素和 CSS 像素的比值。目前主流的屏幕 DPR=2(iPhone 8),或者 3(iPhone 8 Plus)。拿 2 倍屏来说,设备的物理像素要实现 1 像素,而 DPR=2,所以 css 像素只能是 0.5。

    最常用的方法:

    /* 底边框 */
    .b-border {
      position: relative;
    }
    .b-border:before {
      content: '';
      position: absolute;
      left: 0;
      bottom: 0;
       100%;
      height: 1px;
      background: #d9d9d9;
      -webkit-transform: scaleY(0.5);
      transform: scaleY(0.5);
      -webkit-transform-origin: 0 0;
      transform-origin: 0 0;
    }
    /* 上边框 */
    .t-border {
      position: relative;
    }
    .t-border:before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
       100%;
      height: 1px;
      background: #d9d9d9;
      -webkit-transform: scaleY(0.5);
      transform: scaleY(0.5);
      -webkit-transform-origin: 0 0;
      transform-origin: 0 0;
    }
    /* 右边框 */
    .r-border {
      position: relative;
    }
    .r-border:before {
      content: '';
      position: absolute;
      right: 0;
      bottom: 0;
       1px;
      height: 100%;
      background: #d9d9d9;
      -webkit-transform: scaleX(0.5);
      transform: scaleX(0.5);
      -webkit-transform-origin: 0 0;
      transform-origin: 0 0;
    }
    /* 左边框 */
    .l-border {
      position: relative;
    }
    .l-border:before {
      content: '';
      position: absolute;
      left: 0;
      bottom: 0;
       1px;
      height: 100%;
      background: #d9d9d9;
      -webkit-transform: scaleX(0.5);
      transform: scaleX(0.5);
      -webkit-transform-origin: 0 0;
      transform-origin: 0 0;
    }
    
    /* 四条边 */
    .setBorderAll {
      position: relative;
      &:after {
        content: ' ';
        position: absolute;
        top: 0;
        left: 0;
         200%;
        height: 200%;
        transform: scale(0.5);
        transform-origin: left top;
        box-sizing: border-box;
        border: 1px solid #e5e5e5;
        border-radius: 4px;
      }
    }

    禁止用户选择页面中的文字或者图片

    div {
      -webkit-touch-callout: none;
      -webkit-user-select: none;
      -khtml-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }

    清除输入框内阴影

    div {
      -webkit-appearance: none;
    }

    禁止保存或拷贝图像

    img {
      -webkit-touch-callout: none;
    }

    android系统中元素被点击时产生边框

    部分android系统点击一个链接,会出现一个边框或者半透明灰色遮罩, 不同生产商定义出来额效果不一样。去除代码如下

    a,button,input,textarea{
      -webkit-tap-highlight-color: rgba(0,0,0,0)
      -webkit-user-modify:read-write-plaintext-only; 
    }

    iOS 滑动不流畅

    ios 手机上下滑动页面会产生卡顿,手指离开页面,页面立即停止运动。整体表现就是滑动不流畅,没有滑动惯性。iOS 5.0 以及之后的版本,滑动有定义有两个值 auto 和 touch,默认值为 auto。

    「解决方案」

    1、在滚动容器上增加滚动 touch 方法

    .wrapper {
      -webkit-overflow-scrolling: touch;
    }

    2、设置外部overflow为hidden,设置内容元素overflow为auto,内部元素超出body即产生滚动,超出的部分body隐藏

    body {
      overflow-y: hidden;
    }
    .wrapper {
      overflow-y: auto;
    }

    js 篇

    移动端click屏幕产生200-300 ms的延迟响应

    移动设备上的web网页是有300ms延迟的,往往会造成按钮点击延迟甚至是点击失效。解决方案:

    • fastclick可以解决在手机上点击事件的300ms延迟
    • zepto的touch模块,tap事件也是为了解决在click的延迟问题

    触摸事件的响应顺序

    1. ontouchstart
    2. ontouchmove
    3. ontouchend
    4. onclick

    iOS 上拉边界下拉出现空白

    手指按住屏幕下拉,屏幕顶部会多出一块白色区域。手指按住屏幕上拉,底部多出一块白色区域。

    在 iOS 中,手指按住屏幕上下拖动,会触发 touchmove 事件。这个事件触发的对象是整个 webview 容器,容器自然会被拖动,剩下的部分会成空白。

    「解决方案」

    document.body.addEventListener(
      'touchmove',
      function(e) {
        if (e._isScroller) return
        // 阻止默认事件
        e.preventDefault()
      },
      {
        passive: false
      }
    )

    IOS 键盘弹起挡住原来的视图

    • 可以通过监听移动端软键盘弹起 Element.scrollIntoViewIfNeeded(Boolean)方法用来将不在浏览器窗口的可见区域内的元素滚动到浏览器窗口的可见区域。如果该元素已经在浏览器窗口的可见区域内,则不会发生滚动。
    • true,则元素将在其所在滚动区的可视区域中居中对齐。
    • false,则元素将与其所在滚动区的可视区域最近的边缘对齐。根据可见区域最靠近元素的哪个边缘,元素的顶部将与可见区域的顶部边缘对准,或者元素的底部边缘将与可见区域的底部边缘对准。
    window.addEventListener('resize', function() {
      if (
        document.activeElement.tagName === 'INPUT' ||
        document.activeElement.tagName === 'TEXTAREA'
      ) {
        window.setTimeout(function() {
          if ('scrollIntoView' in document.activeElement) {
            document.activeElement.scrollIntoView(false)
          } else {
            document.activeElement.scrollIntoViewIfNeeded(false)
          }
        }, 0)
      }
    })

    IOS 键盘收起时页面没用回落,底部会留白

    通过监听键盘回落时间滚动到原来的位置

    window.addEventListener('focusout', function() {
      window.scrollTo(0, 0)
    })
    
    //input输入框弹起软键盘的解决方案。
    var bfscrolltop = document.body.scrollTop
    $('input')
      .focus(function() {
        document.body.scrollTop = document.body.scrollHeight
        //console.log(document.body.scrollTop);
      })
      .blur(function() {
        document.body.scrollTop = bfscrolltop
        //console.log(document.body.scrollTop);
      })

     

    
    

     

  • 相关阅读:
    代码的测试 生产 开关 一键切换 开关
    iot表输出按主键列排序,heap表不是
    iot表输出按主键列排序,heap表不是
    iot 表主键存放所有数据,且按数据插入顺序排序
    iot 表主键存放所有数据,且按数据插入顺序排序
    iot表和heap表排序规则不同
    iot表和heap表排序规则不同
    Oracle 排序规则
    Oracle 排序规则
    perl 异步超时 打印错误
  • 原文地址:https://www.cnblogs.com/linjiangjin/p/14462444.html
Copyright © 2011-2022 走看看