我爱编程

移动端H5常见问题及解决方案

2018-05-28  本文已影响415人  木子知日_

HTML篇

<video x-webkit-airplay="true" webkit-playsinline="true" playsinline="true"></video>

CSS篇

* { -webkit-tap-highlight-color: rgba(0,0,0,0); }
a,img{-webkit-touch-callout:none;}
body{-webkit-user-select: none}
body{-webkit-text-size-adjust: none}
input{
  -webkit-appearance:none;
  outline:none;
}}
::-webkit-input-placeholder { color: #999; }
input:focus::-webkit-input-placeholder{ color:#999; }
p{
  line-height: normal;
  padding: 10px 0;
}
var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'
window.addEventListener(resizeEvt, function(){}, false)

//竖屏时样式
@media all and (orientation:portrait){   }
//横屏时样式
@media all and (orientation:landscape){   }

js篇

var time = "2000-01-01 00:00:00"
time = time.replace(/\-/g, "/");//将时间格式的'-'转成'/'形式,兼容iOS
document.body.addEventListener('ontouchstart',function(){})

混合篇

body.modal-open {
    position: fixed;
}

如果只设置这个样式,会导致蒙版弹出时滚动位置丢失,还需要用js来保存滚动条的位置

if(showModal){
  let  scrollTop = document.body.scrollTop || document.documentElement.scrollTop
  document.body.classList.add('modal-open')
  document.body.style.top = -scrollTop +'px'
}else{
  document.body.classList.remove('modal-open')
  window.scrollTo(0,this.scrollTop)
}
window.addEventListener('resize', function() {
    if(
        document.activeElement.tagName === 'INPUT' ||
        document.activeElement.tagName === 'TEXTAREA'
      ) {
        window.setTimeout(function() {
          if('scrollIntoView' in document.activeElement) {
            document.activeElement.scrollIntoView();
          } else {
            document.activeElement.scrollIntoViewIfNeeded();
          }
        }, 0);
      }
});
   持续更新中......
上一篇 下一篇

猜你喜欢

热点阅读