js原生实现移动端手指滑动轮播图效果的示例


Posted in Javascript onJanuary 02, 2018

如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <title>Document</title>
</head>
<style>
  html{height:100%;}
  body{width: 100%;height:100%;margin:0;overflow: hidden;}
  .wrap{position: relative;overflow: hidden;}
  .box{position: absolute;list-style: none;left:0;top:0;padding:0;margin:0;}
  .box li{float:left;}
  .box{
    position: relative;
    height: 2000px;
    width: 100%;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: red;
  }
  .box1{
    height: 2000px;
  }
  .box2{
    background: yellow;
  }
  .box3{
    background: yellowgreen;
  }
  .box4{
    background: orange;
  }
  .box5{
    background: cyan;
  }
</style>
<body>
<div class="wrap">
  <ul class="box">
    <li><div class="box1 box2">11111</div></li>
    <li><div class="box1 box3">2222</div></li>
    <li><div class="box1 box4">3333</div></li>
    <li><div class="box1 box5">4444</div></li>
  </ul>
</div>
<script>
  var aLi = document.querySelectorAll(".box li");
  var box = document.querySelector('.box');
  var wrap = document.querySelector('.wrap');
  var aLiWidth = box.offsetWidth;
  console.log('aLiWidth: ' + aLiWidth)
  wrap.style.height = aLi[0].offsetHeight + 'px';
  // 设置盒子的宽度
  box.style.width = aLi.length*100 + '%';
  for(var i=0;i<aLi.length;i++){
    aLi[i].style.width = 1/aLi.length * 100 + '%';
  };
  // 初始化手指坐标点
  var startPoint = 0;
  var startEle = 0;
  //手指按下
  wrap.addEventListener("touchstart",function(e){
    startPoint = e.changedTouches[0].pageX;
    startEle = box.offsetLeft;
  });
  //手指滑动
  wrap.addEventListener("touchmove",function(e){
    var currPoint = e.changedTouches[0].pageX;
    var disX = currPoint - startPoint;
    var left = startEle + disX;
    box.style.left = left + 'px';
  });
  //当手指抬起的时候,判断图片滚动离左右的距离,当
  wrap.addEventListener("touchend",function(e){
    var left = box.offsetLeft;
// 判断正在滚动的图片距离左右图片的远近,以及是否为最后一张或者第一张
    var currNum = Math.round(-left/aLiWidth);
    currNum = currNum>=(aLi.length-1) ? aLi.length-1 : currNum;
    currNum = currNum<=0 ? 0 : currNum;
    box.style.left = -currNum*wrap.offsetWidth + 'px';
  })

</script>

</body>
</html>

以上这篇js原生实现移动端手指滑动轮播图效果的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
ASP.NET jQuery 实例13 原创jQuery文本框字符限制插件-TextArea Counter
Feb 03 Javascript
js 跳出页面的frameset框架示例介绍
Dec 23 Javascript
JavaScript知识点总结(六)之JavaScript判断变量数据类型
May 31 Javascript
prototype.js常用函数详解
Jun 18 Javascript
JS 实现计算器详解及实例代码(一)
Jan 08 Javascript
移动端利用H5实现压缩图片上传功能
Mar 29 Javascript
详解vue2父组件传递props异步数据到子组件的问题
Jun 29 Javascript
js图片轮播插件的封装
Jul 21 Javascript
vue基于mint-ui的城市选择3级联动的示例
Oct 25 Javascript
vue仿element实现分页器效果
Sep 13 Javascript
vscode 开发Vue项目的方法步骤
Nov 25 Javascript
addEventListener()和removeEventListener()追加事件和删除追加事件
Dec 04 Javascript
vue父组件向子组件(props)传递数据的方法
Jan 02 #Javascript
基于wordpress的ajax写法详解
Jan 02 #Javascript
基于Vue的SPA动态修改页面title的方法(推荐)
Jan 02 #Javascript
jq.ajax+php+mysql实现关键字模糊查询(示例讲解)
Jan 02 #Javascript
使用async、enterproxy控制并发数量的方法详解
Jan 02 #Javascript
图片懒加载imgLazyLoading.js使用详解
Sep 15 #Javascript
基于jquery.page.js实现分页效果
Jan 01 #jQuery
You might like
一个简单计数器的源代码
2006/10/09 PHP
PHP经典的给图片加水印程序
2006/12/06 PHP
PHP编译安装中遇到的两个错误和解决方法
2014/08/20 PHP
C#静态方法与非静态方法实例分析
2014/09/22 PHP
thinkphp中空模板与空模块的用法实例
2014/11/26 PHP
PHP callback函数使用方法和注意事项
2015/01/23 PHP
Yii框架防止sql注入,xss攻击与csrf攻击的方法
2016/10/18 PHP
javascript RadioButtonList获取选中值
2009/04/09 Javascript
jQuery validate 中文API 附validate.js中文api手册
2010/07/31 Javascript
jQuery lazyload 的重复加载错误以及修复方法
2010/11/19 Javascript
当json键为数字时的取值方法解析
2013/11/15 Javascript
使用jQuery和PHP实现类似360功能开关效果
2014/02/12 Javascript
js中匿名函数的创建与调用方法分析
2014/12/19 Javascript
javascript版2048小游戏
2015/03/18 Javascript
AngularJS ng-bind-template 指令详解
2016/07/30 Javascript
JavaScript数组的5种迭代方法
2017/09/29 Javascript
vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法
2017/11/28 Javascript
JS实现获取自定义属性data值的方法示例
2018/12/19 Javascript
详解vue更改头像功能实现
2019/04/28 Javascript
vue实现微信分享链接添加动态参数的方法
2019/04/29 Javascript
微信小程序监听用户登录事件的实现方法
2019/11/11 Javascript
[01:21:07]EG vs Liquid 2018国际邀请赛淘汰赛BO3 第一场 8.25
2018/08/29 DOTA
Python写入CSV文件的方法
2015/07/08 Python
Python的面向对象编程方式学习笔记
2016/07/12 Python
Python中装饰器兼容加括号和不加括号的写法详解
2017/07/05 Python
python之virtualenv的简单使用方法(必看篇)
2017/11/25 Python
Python中用psycopg2模块操作PostgreSQL方法
2017/11/28 Python
Python爬虫设置代理IP(图文)
2018/12/23 Python
用Python+OpenCV对比图像质量的几种方法
2019/07/15 Python
selenium+PhantomJS爬取豆瓣读书
2019/08/26 Python
美国知名日用品连锁超市:Dollar General(多来店)
2017/01/14 全球购物
英语生日邀请函
2014/01/23 职场文书
质量提升方案
2014/06/16 职场文书
张思德观后感
2015/06/09 职场文书
广告策划的实习心得体会总结!
2019/07/22 职场文书
普希金诗歌赏析(6首)
2019/08/22 职场文书