下面代码只是实现了上下滑动惯性,没有写水平滑动惯性。(临时代码笔记,可能会在以后的过程中不断更新优化代码)

/**
* 惯性原理:
* 产生的速度 = 移动距离 / 移动时间
* 距离 = 松开的坐标 - 上次的坐标 (距离差)
* 时间 = 松开的时间 - 按下的时间 (时间差)
* */ var dargFun = {
dargDom:null, //惯性滑动的DOM区域
startX:0, //开始偏移的X
startY:0, //开始偏移的Y
clientX:0,
clientY:0,
translateX:0, //保存的X偏移
translateY:0, //保存的Y偏移
maxWidth:0, //滑动的最大宽度
maxHeight:0, //滑动的最大高度
startTime:0, //记录初始按下时间
init:function(config){
this.dargDom = document.querySelector(config.dargDom);
this.maxWidth = this.dargDom.offsetWidth;
this.maxHeight = this.dargDom.offsetHeight; this.dargDom.addEventListener('touchstart',(event)=>{
event.stopPropagation(); //停止事件传播
this.clientX = event.changedTouches[0].clientX;
this.clientY = event.changedTouches[0].clientY;
this.dargDom.style.WebkitTransition = this.dargDom.style.transition = '';
this.startX = this.translateX;
this.startY = this.translateY;
this.startTime = Date.now();
},false); this.dargDom.addEventListener('touchmove',(event)=>{
if(document.documentElement.scrollTop >= this.dargDom.scrollHeight - this.dargDom.clientHeight){ }else{
return;
} event.stopPropagation(); //停止事件传播
this.translateX = event.changedTouches[0].clientX - this.clientX + this.startX;
this.translateY = event.changedTouches[0].clientY - this.clientY + this.startY;
if(this.translateY > 0 ){ //拖动系数. 拉力的感觉
this.translateY *= 0.4;
}else if( this.translateY < -(this.dargDom.scrollHeight - this.dargDom.clientHeight)){
this.translateY = (event.changedTouches[0].clientY - this.clientY) * 0.4 + this.startY;
}
this.animate(this.translateY);
},false); this.dargDom.addEventListener('touchend',(event)=>{
event.stopPropagation(); //停止事件传播
var distanceY = event.changedTouches[0].clientY - this.clientY,
timeDis = Date.now() - this.startTime, //时间差
speed = (distanceY / timeDis) * 100; // 惯性
this.translateY += speed; this.translateY = 0;
// 添加贝塞尔曲线
this.dargDom.style.WebkitTransition = this.dargDom.style.transition = 'transform 500ms cubic-bezier(0.1, 0.57, 0.1, 1)';
this.animate(this.translateY); },false); },
animate:function(y){
this.dargDom.style.WebkitTransform = this.dargDom.style.transform = 'translateY('+y+'px)';
}
} export default dargFun;

注:当滑动到页面底部的时候才触发touchmove事件。

调用方式:

dragFun.init({
dargDom:'#contractContanier'
});

参考地址:


HTML5移动端拖动惯性的更多相关文章

  1. HTML5移动端图片左右切换动画

    插件描述:HTML5移动端图片左右切换动画 小海今天要给大家分享一款很不错的图片左右切换焦点图动画,并且支持移动端触摸滑动.功能上,这款HTML5图片播放器支持鼠标滑动.手机端触摸滑动以及自动播放.外 ...

  2. html5 移动端单页面布局

    序     移动端的web网页使用的是响应式设计,但一般我们看到的网站页面都是跳转刷新得到的,比如通过点击一个menu后进入到另一个页面 今天来说下是移动端的单页面的布局.单页面就是一切操作和布局都是 ...

  3. 第九十一节,html5+css3pc端固定布局,完成首页

    html5+css3pc端固定布局,完成首页 此时我们的首页就完成了 首页效果 其他页面我就不做了,原理相同,做其他页面时将头尾css分离调用即可 大纲算法 我们看看大纲算法比较清晰,说明符合规则 h ...

  4. 第九十节,html5+css3pc端固定布局,底部区域,说明区域,版权区域

    html5+css3pc端固定布局,底部区域,说明区域,版权区域 完成: HTML代码: <!--尾部--> <footer class="wei-bu"> ...

  5. 第八十九节,html5+css3pc端固定布局,热门旅游取,标题介绍区,旅游项目区

    html5+css3pc端固定布局,热门旅游取,标题介绍区,旅游项目区 样式: html代码: <!--主体--> <section class="zhu-ti" ...

  6. 第八十八节,html5+css3pc端固定布局,搜索区,插入大图,搜索框

    html5+css3pc端固定布局,搜索区,插入大图,搜索框 设置一个div作为搜索区域 1.宽度为百分之百 2.最小宽度为1263,因为要考虑到手机,等小屏幕缩小后宽度会自适应,导致破坏布局,将最小 ...

  7. 第八十七节,html5+css3pc端固定布局,大纲算法,section和div,结构分析

    html5+css3pc端固定布局,大纲算法,section和div,结构分析 一.大纲算法 在HTML5中有一个很重要的概念,叫做HTML5 大纲算法(HTML5Outliner),它的用途是为用户 ...

  8. 第八十六节,html5+css3pc端固定布局,网站结构,CSS选择器,完成导航

    html5+css3pc端固定布局,网站结构,CSS选择器,完成导航 页面采用1280的最低宽度设计,去掉滚动条为1263像素. 项目是PC端的固定布局,会采用像素(px)单位. 网站结构语义 在没有 ...

  9. html5移动端页面分辨率设置及相应字体大小设置的靠谱使用方式

    对于html5移动端网页编写CSS网上有很多介绍的文章,但在实际使用过程中还是会纠结. 网上的资料太多,且大多都是技术介绍型,特别是针对android上,网上写的各种麻烦,各种复杂,各种不接地气儿.. ...

随机推荐

  1. 依赖注入[7]: .NET Core DI框架[服务注册]

    包含服务注册信息的IServiceCollection对象最终被用来创建作为DI容器的IServiceProvider对象.服务注册就是创建出现相应的ServiceDescriptor对象并将其添加到 ...

  2. Egret 生成 自带EUI 的微信小游戏 踩坑!

    1. 首先,再次被网上一大堆屎一样的资料搞得浪费了我一天时间.各种坑. 2. 本文先讲一种正确的方式,然后再列举坑. 去www.egret.com下载最新的引擎,我的最新版本是5.2.2. 然后就会被 ...

  3. OSGi类加载流程

    思路 OSGi每个模块都有自己独立的classpath.如何实现这一点呢?是因为OSGi采取了不同的类加载机制: OSGi为每个bundle提供一个类加载器,该加载器能够看到bundle Jar文件内 ...

  4. Xtrabackup实现Mysql的InnoDB引擎热备份

    前面Zabbix使用的数据库是mysql,数据库备份不用多说,必须滴,由于使用的是innodb引擎,既然做,那就使用第三方强大的Xtrabackup工具来热备吧,Xtrabackup的说明,参见htt ...

  5. PHP workerMan tcp与webSocket 透传互通

    <?php $work_path = dirname(__FILE__); chdir($work_path); use \Workerman\Worker; use \Workerman\Li ...

  6. 浅谈Java中的锁:Synchronized、重入锁、读写锁

    Java开发必须要掌握的知识点就包括如何使用锁在多线程的环境下控制对资源的访问限制 ◆ Synchronized ◆ 首先我们来看一段简单的代码: 12345678910111213141516171 ...

  7. [Swift]LeetCode98. 验证二叉搜索树 | Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. [Swift]LeetCode264.丑数 II | Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  9. [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  10. [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...