码迷,mamicode.com
首页 > 移动开发 > 详细

Viewport及判断移动端上下滑动

时间:2016-10-29 19:01:09      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:并且   触摸事件   port   nim   ble   listener   它的   移动端   整数   

devicePixelRatio window对象

有一个devicePixelRatio属性,

它的官方的定义为:设备物理像素和设备独立像素的比例,也就是

devicePixelRatio = 物理像素 / 独立像素。

获取设备独立像素(屏幕宽度)

document.documentElement.clientWidth

window.screen.width

$(window).width()

<script>
var x=document.documentElement.clientWidth;
var winx=window.screen.width;(屏幕总宽度,并且无论怎样变幻窗口大小,其值不变)
var wh=$(window).width();
alert(x+‘ ‘+winx+‘ ‘+wh);
</script>

二、meta viewport

<meta name=“viewport” content=“width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no”>

width 设置视口宽度,正整数或 width-device

initial-scale 设置页面的初始缩放值

minimum-scale 设置页面最小缩放值

maximum-scale 设置页面最小缩放值

user-scalable 用户缩放,值为“no”或“yes” height 很少使用

三、JS基本触摸事件

> touchstart 开始

> touchmove 滑动

> touchend 结束

document.addEventListener(‘touchstart‘,function (ev){ console.log(ev); }, false);

例子:

判断手机端上下滑动

var startY=0,endy=0,flag=false;
document.addEventListener(‘touchstart‘,function(e){
  startY=e.targetTouches[0].pageY;
    //console.log(e);
},false);
document.addEventListener(‘touchmove‘,function(e){
  endy=e.targetTouches[0].pageY;
  if (startY-endy>100||startY-endy<-100) {
    flag=true;
  }

},false);
document.addEventListener(‘touchend‘,function(e){
  if (flag) {
    if (startY-endy>100) {
      alert(‘上滑‘);
    }
  if(startY-endy<-100){
    alert(‘下滑‘);
    }
  }
},false);

 

Viewport及判断移动端上下滑动

标签:并且   触摸事件   port   nim   ble   listener   它的   移动端   整数   

原文地址:http://www.cnblogs.com/SunShineM/p/6011331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!