最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS+HTML5手機(jī)開(kāi)發(fā)之滾動(dòng)和慣性緩動(dòng)實(shí)現(xiàn)方法分析

 更新時(shí)間:2016年06月12日 11:34:02   作者:鄭文亮  
這篇文章主要介紹了JS+HTML5手機(jī)開(kāi)發(fā)之滾動(dòng)和慣性緩動(dòng)實(shí)現(xiàn)方法,涉及javascript結(jié)合HTML5特性控制頁(yè)面元素的運(yùn)動(dòng)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了JS+HTML5手機(jī)開(kāi)發(fā)之滾動(dòng)和慣性緩動(dòng)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

1. 滾動(dòng) 以下是三種實(shí)現(xiàn)方式:

1) 利用原生的css屬性 overflow: scroll div id= parent style = overflow:scroll; divid='content'內(nèi)容區(qū)域/div /div Notice: 在android 有bug, 滾動(dòng)完后會(huì)回退到最頂端的內(nèi)容區(qū)域,解決辦法是使用后兩種方式實(shí)現(xiàn)

2)js 編程實(shí)現(xiàn) 思路:對(duì)比手指在屏幕上移動(dòng)前后位置變化改變內(nèi)容元素content

1. 滾動(dòng)

以下是三種實(shí)現(xiàn)方式:

1) 利用原生的css屬性 overflow: scroll

<div id="parent" style="overflow:scroll;>
  <div id='content'>內(nèi)容區(qū)域</div>
</div>

Notice:

在android 有bug, 滾動(dòng)完后會(huì)回退到最頂端的內(nèi)容區(qū)域,解決辦法是使用后兩種方式實(shí)現(xiàn)

2)js 編程實(shí)現(xiàn)

思路:對(duì)比手指在屏幕上移動(dòng)前后位置變化改變內(nèi)容元素content的位置

第一步:設(shè)置parent的 overflow為hidden, 設(shè)置content的position為relative, top為0;

第二步:監(jiān)聽(tīng)touch事件

var parent = document.getElementById('parent');
parent.addEventListener('touchstart', function(e) {
  // do touchstart
});
parent.addEventListener('touchmove', function(e) {
  // do touchmove
});
parent.addEventListener('touchend', function(e) {
  // do touchend
});

第三步:實(shí)現(xiàn)滾動(dòng)代碼

/**
 * 這里只實(shí)現(xiàn)垂直滾動(dòng)
 */
var parent = document.getElementById('parent');
var content = document.getElementById('content')
var startY = 0; // 初始位置
var lastY = 0; // 上一次位置
parent.addEventListener('touchstart', function(e) {
  lastY = startY = e.touches[0].pageY;
});
parent.addEventListener('touchmove', function(e) {
  var nowY = e.touches[0].pageY;
  var moveY = nowY - lastY;
  var contentTop = content.style.top.replace('px', '');
  // 設(shè)置top值移動(dòng)content
  content.style.top = (parseInt(contentTop) + moveY) + 'px';
  lastY = nowY;
});
parent.addEventListener('touchend', function(e) {
  // do touchend
  var nowY = e.touches[0].pageY;
  var moveY = nowY - lastY;
  var contentTop = content.style.top.replace('px', '');
  // 設(shè)置top值移動(dòng)content
  content.style.top = (parseInt(contentTop) + moveY) + 'px';
  lastY = nowY;
});

第四步:優(yōu)化

上邊代碼在手機(jī)上運(yùn)行效果相對(duì)PC上要卡很多

優(yōu)化部分請(qǐng)參見(jiàn):

3) 使用iScroll4框架

var scroll = new iScroll('parent', {
hScrollbar: false,
vScrollbar: true,
checkDOMChanges : true
});

框架官網(wǎng):http://cubiq.org/iscroll-4

2.慣性緩動(dòng)

思路:取手指最后一段時(shí)間在屏幕上劃動(dòng)的平均速度v,讓v按一個(gè)遞減函數(shù)變化,直到不能移動(dòng)或v<=0

/**
 * 這里只實(shí)現(xiàn)垂直滾動(dòng)
 */
var parent = document.getElementById('parent');
var content = document.getElementById('content')
var startY = 0; // 初始位置
var lastY = 0; // 上一次位置
/**
 * 用于緩動(dòng)的變量
 */
var lastMoveTime = 0;
var lastMoveStart = 0;
var stopInertiaMove = false; // 是否停止緩動(dòng)
parent.addEventListener('touchstart', function(e) {
  lastY = startY = e.touches[0].pageY;
  /**
   * 緩動(dòng)代碼
   */
  lastMoveStart = lastY;
  lastMoveTime = e.timeStamp || Date.now();
  stopInertiaMove = true;
});
parent.addEventListener('touchmove', function(e) {
  var nowY = e.touches[0].pageY;
  var moveY = nowY - lastY;
  var contentTop = content.style.top.replace('px', '');
  // 設(shè)置top值移動(dòng)content
  content.style.top = (parseInt(contentTop) + moveY) + 'px';
  lastY = nowY;
  /**
   * 緩動(dòng)代碼
   */
  var nowTime = e.timeStamp || Date.now();
  stopInertiaMove = true;
  if(nowTime - lastMoveTime > 300) {
    lastMoveTime = nowTime;
    lastMoveStart = nowY;
  }
});
parent.addEventListener('touchend', function(e) {
  // do touchend
  var nowY = e.touches[0].pageY;
  var moveY = nowY - lastY;
  var contentTop = content.style.top.replace('px', '');
  var contentY = (parseInt(contentTop) + moveY);
  // 設(shè)置top值移動(dòng)content
  content.style.top = contentY + 'px';
  lastY = nowY;
  /**
   * 緩動(dòng)代碼
   */
  var nowTime = e.timeStamp || Date.now();
  var v = (nowY - lastMoveStart) / (nowTime - lastMoveTime); //最后一段時(shí)間手指劃動(dòng)速度
  stopInertiaMove = false;
  (function(v, startTime, contentY) {
    var dir = v > 0 ? -1 : 1; //加速度方向
    var deceleration = dir*0.0006;
    var duration = v / deceleration; // 速度消減至0所需時(shí)間
    var dist = v * duration / 2; //最終移動(dòng)多少
    function inertiaMove() {
      if(stopInertiaMove) return;
      var nowTime = e.timeStamp || Date.now();
      var t = nowTime-startTime;
      var nowV = v + t*deceleration;
      // 速度方向變化表示速度達(dá)到0了
      if(dir*nowV < 0) {
        return;
      }
      var moveY = (v + nowV)/2 * t;
      content.style.top = (contentY + moveY) + "px";
      setTimeout(inertiaMove, 10);
    }
    inertiaMove();
  })(v, nowTime, contentY);
});

PS:這里再為大家推薦幾款代碼格式化、美化工具,相信大家在以后的開(kāi)發(fā)過(guò)程中會(huì)用得到:

在線JavaScript代碼美化、格式化工具:
http://tools.jb51.net/code/js

JavaScript壓縮/格式化/加密工具:
http://tools.jb51.net/code/jscompress

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jQuery常用插件及用法總結(jié)》、《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)

希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

苍山县| 华亭县| 丰镇市| 甘德县| 黄石市| 峨山| 洛扎县| 忻城县| 九龙城区| 四会市| 阿克陶县| 孟州市| 灵山县| 买车| 高雄县| 通河县| 临江市| 海宁市| 枣强县| 伽师县| 通州区| 甘肃省| 平泉县| 庆城县| 鄢陵县| 昔阳县| 江川县| 志丹县| 周口市| 南通市| 广元市| 苏尼特右旗| 嘉义市| 辉县市| 竹北市| 卢湾区| 南郑县| 花莲县| 尚义县| 金华市| 金沙县|