1. 滾動
以下是三種實(shí)現(xiàn)方式:
1) 利用原生的css屬性 overflow: scroll
<div id="parent" style="overflow:scroll;>
<div id='content'>內(nèi)容區(qū)域</div>
</div>
<div id='content'>內(nèi)容區(qū)域</div>
</div>
Notice:
在android 有bug, 滾動完后會回退到最頂端的內(nèi)容區(qū)域,解決辦法是使用后兩種方式實(shí)現(xiàn)
2)js 編程實(shí)現(xiàn)
思路:對比手指在屏幕上移動前后位置變化改變內(nèi)容元素content的位置
第一步:設(shè)置parent的 overflow為hidden, 設(shè)置content的position為relative, top為0;
第二步:監(jiān)聽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
});
parent.addEventListener('touchstart', function(e) {
// do touchstart
});
parent.addEventListener('touchmove', function(e) {
// do touchmove
});
parent.addEventListener('touchend', function(e) {
// do touchend
});
第三步:實(shí)現(xiàn)滾動代碼
/**
* 這里只實(shí)現(xiàn)垂直滾動
*/
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值移動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值移動content
content.style.top = (parseInt(contentTop) + moveY) + 'px';
lastY = nowY;
});
* 這里只實(shí)現(xiàn)垂直滾動
*/
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值移動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值移動content
content.style.top = (parseInt(contentTop) + moveY) + 'px';
lastY = nowY;
});
第四步:優(yōu)化
上邊代碼在手機(jī)上運(yùn)行效果相對PC上要卡很多
優(yōu)化部分請參見:
3) 使用iScroll4框架
var scroll = new iScroll('parent', {
hScrollbar: false,
vScrollbar: true,
checkDOMChanges : true
});
框架官網(wǎng):http://cubiq.org/iscroll-4
2.慣性緩動
思路:取手指最后一段時間在屏幕上劃動的平均速度v,讓v按一個遞減函數(shù)變化,直到不能移動或v<=0
/**
* 這里只實(shí)現(xiàn)垂直滾動
*/
var parent = document.getElementById('parent');
var content = document.getElementById('content')
var startY = 0; // 初始位置
var lastY = 0; // 上一次位置
/**
* 用于緩動的變量
*/
var lastMoveTime = 0;
var lastMoveStart = 0;
var stopInertiaMove = false; // 是否停止緩動
parent.addEventListener('touchstart', function(e) {
lastY = startY = e.touches[0].pageY;
/**
* 緩動代碼
*/
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值移動content
content.style.top = (parseInt(contentTop) + moveY) + 'px';
lastY = nowY;
/**
* 緩動代碼
*/
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值移動content
content.style.top = contentY + 'px';
lastY = nowY;
/**
* 緩動代碼
*/
var nowTime = e.timeStamp || Date.now();
var v = (nowY - lastMoveStart) / (nowTime - lastMoveTime); //最后一段時間手指劃動速度
stopInertiaMove = false;
(function(v, startTime, contentY) {
var dir = v > 0 ? -1 : 1; //加速度方向
var deceleration = dir*0.0006;
var duration = v / deceleration; // 速度消減至0所需時間
var dist = v * duration / 2; //最終移動多少
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);
});
* 這里只實(shí)現(xiàn)垂直滾動
*/
var parent = document.getElementById('parent');
var content = document.getElementById('content')
var startY = 0; // 初始位置
var lastY = 0; // 上一次位置
/**
* 用于緩動的變量
*/
var lastMoveTime = 0;
var lastMoveStart = 0;
var stopInertiaMove = false; // 是否停止緩動
parent.addEventListener('touchstart', function(e) {
lastY = startY = e.touches[0].pageY;
/**
* 緩動代碼
*/
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值移動content
content.style.top = (parseInt(contentTop) + moveY) + 'px';
lastY = nowY;
/**
* 緩動代碼
*/
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值移動content
content.style.top = contentY + 'px';
lastY = nowY;
/**
* 緩動代碼
*/
var nowTime = e.timeStamp || Date.now();
var v = (nowY - lastMoveStart) / (nowTime - lastMoveTime); //最后一段時間手指劃動速度
stopInertiaMove = false;
(function(v, startTime, contentY) {
var dir = v > 0 ? -1 : 1; //加速度方向
var deceleration = dir*0.0006;
var duration = v / deceleration; // 速度消減至0所需時間
var dist = v * duration / 2; //最終移動多少
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);
});
本文來自zzm_justin的博客,原文地址:http://blog.csdn.net/zzm_justin/article/details/8476373