移动端— 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 ...
随机推荐
- P1050 螺旋矩阵
P1050 螺旋矩阵 转跳点:
- Thread start0 启动分析 一图看懂
参考文章: https://segmentfault.com/a/1190000017255007 https://segmentfault.com/a/1190000020194154 1.线程启动 ...
- 六十八、SAP中内表插入的三种方法之二,COLLECT的使用,用于计算数字字段之和
一.使用COLLECT时,如果关键字没有,那么插入,如果有则求所有关键字列的和,代码如下 二.sy-index在循环中,每次循环从1开始递增 三.查看T_DATA数据 四.如下 五.循环时候,我们查看 ...
- 074-PHP数组元素相乘
<?php $arr1=array(3,4,5,6,'7',TRUE); //等价于 3*4*5*6*7*1=2520 $arr2=array(3,4,5,6,'7','hello'); //等 ...
- ES6 之 Proxy
概述 Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改. Proxy 可以理解在目标对象架设一个“拦截”层外界对该对象的访问都必须先通过这层拦截,因此提供了一种机制可以对外界的访问进行 ...
- 备份CSDN
说明:https://blog.csdn.net/Feynman1999/article/details/87908082 源码:https://github.com/Feynman1999/CSDN ...
- Python PIP包管理器
版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...
- 图解HTTP阅读笔记2
TCP协议:三次握手,C端——>SYN——>S端: S端——>SYN/ACK——>C端: C端——>ACK——>S端. 特点:字节流服务,把大块数据分割成以报文段为 ...
- 详解BurpSuite软件 请求包 HTTP (9.23 第十天)
HTTP协议基础 HTTP:HyperText Transfer Protocol,超文本传输协议 1.协议特点: 简单快速,请求方式get post head等8中请求方式 无连接(一次请求就断开) ...
- 实战 迁移学习 VGG19、ResNet50、InceptionV3 实践 猫狗大战 问题
实战 迁移学习 VGG19.ResNet50.InceptionV3 实践 猫狗大战 问题 参考博客:::https://blog.csdn.net/pengdali/article/detail ...