移动端— Touch事件轮播图
虽然 以前也写过手机端页面 。当时用的jquery moblie 框架。啥也不懂 就知道复制粘贴出效果 不敢改内部样式。现在呢 了解手机端原理 一些基本的概念 视口 缩放 后 。再去想以前写的页面 套框架 显然得心应手了不少。
手机移动端轮播 原生js
代码:
let banner = document.querySelector('.jd_banner')
let imgbox = banner.querySelector('ul:first-of-type')//第一个ul
let first = imgbox.querySelector('li:first-of-type')
let last = imgbox.querySelector('li:last-of-type')
//插入
// 最后的位置 //复制一个
imgbox.appendChild(first.cloneNode(true))
//开始的位置
imgbox.insertBefore(last.cloneNode(true), imgbox.firstChild)
let lilist = imgbox.children
let bannerWidth = banner.offsetWidth;
imgbox.style.width = lilist.length * bannerWidth + 'px'
for (let i = 0; i < lilist.length; i++) {
lilist[i].style.width = imgbox.offsetWidth / lilist.length + 'px'
}
imgbox.style.left = -bannerWidth + 'px'
let index = 1;
//屏幕大小改变
window.onresize = function () {
bannerWidth = banner.offsetWidth;
imgbox.style.width = lilist.length * bannerWidth + 'px'
for (let i = 0; i < lilist.length; i++) {
lilist[i].style.width = imgbox.offsetWidth / lilist.length + 'px'
}
imgbox.style.left = (-index * bannerWidth) + 'px'
}
let bannerIndex = document.querySelector('.jd_bannerIndex')
function active(i) {
for (let j = 0; j < bannerIndex.children.length; j++) {
bannerIndex.children[j].className = ''
}
bannerIndex.children[i].className = 'active'
}
function banntime(){
index++
imgbox.style.transition = 'left 0.5s ease-in-out'
imgbox.style.left = (-index * bannerWidth) + 'px'
if (index == lilist.length - 1) {
active(0)
index = 1
setTimeout(function () {
imgbox.style.transition = 'none'
active(index - 1)
imgbox.style.left = (-index * bannerWidth) + 'px'
}, 500)
}
active(index - 1)
}
let bannerInt = setInterval(banntime, 2000)
let startX, moveX, distanceX;
imgbox.addEventListener('touchstart', function (e) {
startX = e.targetTouches[0].clientX
clearInterval(bannerInt)
})
imgbox.addEventListener('touchmove', function (e) {
moveX = e.targetTouches[0].clientX
distanceX = moveX - startX
imgbox.style.left = (-index * bannerWidth + distanceX) + 'px'
})
imgbox.addEventListener('touchend', function (e) {
imgbox.style.transition = 'left 0.5s ease-in-out'
let drX = distanceX % bannerWidth
if (Math.abs(drX) > bannerWidth / 2) {
if (drX > 0) {
index--
} else {
index++
}
if (index == 0) {
index = 8
imgbox.style.transition = 'none'
}
if (index == lilist.length - 1) {
index = 1
imgbox.style.transition = 'none'
}
}
active(index - 1)
imgbox.style.left = (-index * bannerWidth) + 'px'
bannerInt=setInterval(banntime,2000)
})
效果:

总结 :
不管什么框架库,还是基本很重要 了解原理 才能得心应手
移动端— Touch事件轮播图的更多相关文章
- js 基础篇(点击事件轮播图的实现)
轮播图在以后的应用中还是比较常见的,不需要多少行代码就能实现.但是在只掌握了js基础知识的情况下,怎么来用较少的而且逻辑又简单的方法来实现呢?下面来分析下几种不同的做法: 1.利用位移的方法来实现 首 ...
- Day050--jQuery表单事件 轮播图 插件库 ajax
表单控件的事件 change()表单元素发生改变时触发事件 select()文本元素发生改变时触发事件 submit()表单元素发生改变时触发事件 .focus() 获取焦点 .blur() 释放焦点 ...
- 自实现PC端jQuery版轮播图
最近其他项目不是很忙,被安排给公司的官网项目做一个新的页面(之前没接触公司官网项目),其中有一个用到轮播图的地方,最开始想直接用swiper.js插件实现就好了,可是发现官网项目里之前都没有引入过sw ...
- Swiper 移动端全屏轮播图效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 移动端轮播图实现方法(dGun.js)
本文章介绍在移动端无缝隙轮播图实现的原理,这个轮子比较简单,但可以方便刚刚入门的同学参考.最终效果是在移动端无缝隙无限滑动,可以自定义轮播的速度.支持手势左右滑动.最后会放上源码. HTML部分 &l ...
- 原生js实现简单移动端轮播图
最近项目不是很忙,自己就用原生js写了一个简单的移动端轮播图的小demo,可实现自动轮播和手势滑动轮播,然后就把它记录到个人博客里.还有很多不足的地方,希望多多指出,以便改进. 1.代码部分 分为四个 ...
- 自己用原生JS写的轮播图,支持移动端触摸滑动,分页器圆点可以支持mouseover鼠标移入和click点击,高手看了勿喷哈
自己用原生JavaScript写的轮播图,分页器圆点按钮可支持click点击,也可支持mouseover鼠标悬浮触发,同时支持移动端触摸滑动,有兴趣的友友可以试试哈,菜鸟一枚,高手看了勿喷,请多多指正 ...
- 第124天:移动web端-Bootstrap轮播图插件使用
Bootstrap JS插件使用 > 对于Bootstrap的JS插件,我们只需要将文档实例中的代码粘到我们自己的代码中> 然后作出相应的样式调整 Bootstrap中轮播图插件叫作Car ...
- 自己用原生JS写的轮播图,支持移动端触屏滑动,面向对象思路。分页器圆点支持click和mouseover。
自己用原生javascript写的轮播图,面向对象思路,支持移动端手指触屏滑动.分页器圆点可以选择click点击或mouseover鼠标移入时触发.图片滚动用的setInterval,感觉setInt ...
随机推荐
- 史无前例!一季度Facebook移除22亿假账号
导读 北京时间5月26日上午消息,Facebook表示平台在第一季度移除了22亿虚假账号.这一创纪录的数据显示了这家公司正在与大量试图破坏全球最大社交网络真实性的负面因素进行斗争. 北京时间5月26日 ...
- 基于springboot实现Java阿里短信发送
1.接口TestController import java.util.Random; import com.aliyuncs.DefaultAcsClient; import com.aliyunc ...
- JS - n次方计算
pow 方法返回底表达式的指定次幂. Math.pow(base, exponent) 参数base 必选项.表达式底的值. exponent 必选项.表达式的指数值.
- 面向对象设计思想和MVC设计模式
虽然之前学习Java时有接触过面向对象的设计思想,但因当时Java没学好.所以导致这两天讲php的面向对象设计时,感到没有头绪,这也反应了我练习少和逻辑能力的不足.而MVC设计思想 面向对象就是要将系 ...
- centos7下使用yum安装ifconfig工具
步骤1:搜索安装包 步骤2:使用yum安装 至此,ifconfig工具安装完毕,希望对你有帮助~
- Sklearn 预处理数据
## 版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Lear ...
- Day 19:Properties配置文件类、打印流(printStream) 、 编码与解码
Properties(配置文件类): 主要用于生产配置文件与读取配置文件的信息. Properties要注意的细节: 1. 如果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时 ...
- cf 1241 D. Sequence Sorting(思维)
题意: 一个序列有n个数,有一种操作,你可以选一个数x,使这个序列中等于x的数都移到序列头或尾. 问最少几次操作后,可以使这个序列非降序. 思路: (以下说bi移动到哪里,其实就是指a1……an中等于 ...
- linux下软件包的安装方式
参考:https://blog.51cto.com/13589255/2071277
- POJ 2031:Building a Space Station 最小生成树
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6083 Accepte ...