每天一个JS小demo之移动端横向滑屏和“橡皮筋”特效

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width,user-scalable=no" />
<meta charset="UTF-8">
<script type="text/javascript">
(function(){
setSize();
window.onresize = setSize;
function setSize(){
var html = document.documentElement;
var htmlW = html.clientWidth;
html.style.fontSize = htmlW / 16 + "px";
}
})();
</script>
<title>Document</title>
<style type="text/css">
body {
margin: 0;
background: #ccc;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
.navWrap {
position: relative;
border-bottom: 1px solid #000;
background: #f1f1f1;
height: 2.4rem;
overflow: hidden;
}
#navs {
position: absolute;
left: 0;
top: 0;
white-space: nowrap;
}
#navs li {
display: inline-block;
font: .8rem/2.4rem "宋体";
padding: 0 1rem;
}
#navs li:nth-of-type(1) {
background: #fff;
}
#navs li:nth-last-of-type(1) {
background: #fff;
}
#navs a {
text-decoration: none;
color: #000;
}
</style>
<script type="text/javascript" src="js/startmove.js"></script>
</head>
<body>
<nav class="navWrap">
<ul id="navs">
<li class="active"><a href="javascript:;" target="_blank">首页</a></li><li><a href="javascript:;" target="_blank">MV</a></li><li><a href="javascript:;" target="_blank">悦单</a></li><li><a href="javascript:;" target="_blank">V榜</a></li><li><a href="javascript:;" target="_blank">音乐</a></li><li><a href="javascript:;" target="_blank">商城</a></li><li><a href="javascript:;" target="_blank">节目</a></li><li><a href="javascript:;" target="_blank">饭团</a></li><li><a href="javascript:;" target="_blank">咨询</a></li><li><a href="javascript:;" target="_blank">商城</a></li><li><a href="javascript:;" target="_blank">节目</a></li><li><a href="javascript:;" target="_blank">饭团</a></li><li><a href="javascript:;" target="_blank">咨询</a></li><li><a href="javascript:;" target="_blank">我的家</a></li><li><a href="javascript:;" target="_blank">APP下载</a></li><li><a href="javascript:;" target="_blank">热门应用</a></li>
</ul>
</nav>
<script type="text/javascript">
document.addEventListener('touchstart', function(e) {
e.preventDefault();
});
(function(){
var navWrap = document.querySelector('.navWrap');
var navs = document.querySelector('#navs');
var start,startEl,lastTime,lastDis,lastSpeed;
var min = 0;
var max = 0;
var TC = .25;//拉力系数
/*
lastTime 上一次时间
lastDis 上一次位置
lastSpeed 上一次速度
*/
navWrap.addEventListener('touchstart', function(e) {
var touch = e.changedTouches[0];
start = touch.pageX;
startEl = css(navs,"left");
lastTime = Date.now();
lastDis = touch.pageX;
min = navWrap.clientWidth - navs.offsetWidth;
});
navWrap.addEventListener('touchmove', function(e) {
var touch = e.changedTouches[0];
var now = touch.pageX;
var x = now - start + startEl;
var nowTime = Date.now();
if(x > max){
x *= TC;
} else if(x < min) {
// 位移的幅度为什么没有减小?
x = (x - min)*TC + min;
}
css(navs,"left",x);
lastSpeed = (touch.pageX - lastDis)/(nowTime - lastTime);
//得到上一次和这一次的滑动速度
lastDis = touch.pageX;
lastTime = nowTime;
});
navWrap.addEventListener('touchend', function(e) {
//console.log(lastSpeed);//最后一次滑动的速度
/* 速越快,缓冲的距离越大 */
if(Date.now() - lastTime > 100){
//如果最后一次的move时间和我抬起的时候,相差比较大,说明我在抬起之前,有一定时间没有移动过,那松手之后,也应该保持不动
lastSpeed = 0;
}
lastSpeed = Math.abs(lastSpeed) < .1?0:lastSpeed;
var translate = lastSpeed * 200;//这是要缓冲出去距离
translate = Math.abs(translate) > 1000?1000*(translate/Math.abs(translate)):translate;
var target = translate + css(navs,"left");
var type = "easeOutStrong";
if(target > max){
target = 0;
type = "backOut";
} else if(target < min){
target = min;
type = "backOut";
}
var time = Math.abs((target -css(navs,"left")) * 1.3);
time = (time>0&&time<200)?200:time;
console.log(time);
startMove({
el:navs,
target: {
left: target
},
time: time,
type: type
});
});
})();
</script>
</body>
</html>

//动画形式公式
var Tween = {
linear: function (t, b, c, d){
return c*t/d + b;
},
easeIn: function(t, b, c, d){
return c*(t/=d)*t + b;
},
easeOut: function(t, b, c, d){
return -c *(t/=d)*(t-2) + b;
},
easeBoth: function(t, b, c, d){
if ((t/=d/2) < 1) {
return c/2*t*t + b;
}
return -c/2 * ((--t)*(t-2) - 1) + b;
},
easeInStrong: function(t, b, c, d){
return c*(t/=d)*t*t*t + b;
},
easeOutStrong: function(t, b, c, d){
return -c * ((t=t/d-1)*t*t*t - 1) + b;
},
easeBothStrong: function(t, b, c, d){
if ((t/=d/2) < 1) {
return c/2*t*t*t*t + b;
}
return -c/2 * ((t-=2)*t*t*t - 2) + b;
},
elasticIn: function(t, b, c, d, a, p){
if (t === 0) {
return b;
}
if ( (t /= d) == 1 ) {
return b+c;
}
if (!p) {
p=d*0.3;
}
if (!a || a < Math.abs(c)) {
a = c;
var s = p/4;
} else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
},
elasticOut: function(t, b, c, d, a, p){
if (t === 0) {
return b;
}
if ( (t /= d) == 1 ) {
return b+c;
}
if (!p) {
p=d*0.3;
}
if (!a || a < Math.abs(c)) {
a = c;
var s = p / 4;
} else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
},
elasticBoth: function(t, b, c, d, a, p){
if (t === 0) {
return b;
}
if ( (t /= d/2) == 2 ) {
return b+c;
}
if (!p) {
p = d*(0.3*1.5);
}
if ( !a || a < Math.abs(c) ) {
a = c;
var s = p/4;
}
else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
if (t < 1) {
return - 0.5*(a*Math.pow(2,10*(t-=1)) *
Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
}
return a*Math.pow(2,-10*(t-=1)) *
Math.sin( (t*d-s)*(2*Math.PI)/p )*0.5 + c + b;
},
backIn: function(t, b, c, d, s){
if (typeof s == 'undefined') {
s = 1.70158;
}
return c*(t/=d)*t*((s+1)*t - s) + b;
},
backOut: function(t, b, c, d, s){
if (typeof s == 'undefined') {
s = 2.70158; //回缩的距离
}
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
},
backBoth: function(t, b, c, d, s){
if (typeof s == 'undefined') {
s = 1.70158;
}
if ((t /= d/2 ) < 1) {
return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
}
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
},
bounceIn: function(t, b, c, d){
return c - Tween['bounceOut'](d-t, 0, c, d) + b;
},
bounceOut: function(t, b, c, d){
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
}
return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
},
bounceBoth: function(t, b, c, d){
if (t < d/2) {
return Tween['bounceIn'](t*2, 0, c, d) * 0.5 + b;
}
return Tween['bounceOut'](t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
}
}
// 获取或者设置样式
function css(el,attr,val){
if(arguments.length == 2){
if(el.currentStyle){
val = el.currentStyle[attr];
} else {
val = getComputedStyle(el)[attr];
}
return parseFloat(val);
} else {
if(attr == "opacity"){
el.style.opacity = val;
el.style.filter = "alpha(opacity= "+val*100+")";
} else if(attr == "zIndex"){
el.style[attr] = Math.round(val);
}else {
el.style[attr] = val + "px";
}
}
}
//执行动画
function startMove(init){
clearInterval(init.el.timer);
var t = 0;
var b = {};
var c = {};
var d = init.time/20;
for( var s in init.target){
b[s] = css(init.el,s);
c[s] = init.target[s] - b[s];
}
init.el.timer = setInterval(function(){
t++;
if(t > d){
clearInterval(init.el.timer);
init.callBack&&init.callBack();
} else {
for(var s in init.target){
var val = Tween[init.type](t,b[s],c[s],d);
css(init.el,s,val);
}
}
},20);
}

posted @ 2017-06-29 21:10  catEatBird  阅读(1776)  评论(0编辑  收藏  举报