移动端— 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 ...
随机推荐
- 《ES6标准入门》(阮一峰)--4.字符串的扩展
1.字符的 Unicode 表示法 ES6 加强了对 Unicode 的支持,允许采用\uxxxx形式表示一个字符,其中xxxx表示字符的 Unicode 码点. "\u0061" ...
- Web基础之Spring MVC
Spring MVC Spring MVC 说是框架,对Tomcat来说其实就是一个Servlet,关于如何从上古时期的Servlet演化到如今的SpringMVC的,可以看看这篇博文:Spring ...
- Vue - slot-scope="scope" 的意义
<template slot-scope="scope"> <el-button type="primary ...
- CSS - 设置placeholder样式
input::placeholder { font-size: 12px; font-family: Arial; letter-spacing: 5p ...
- 指令——df
df是disk free 的简称,这个指令的功能和作用是查看磁盘空间. 可以加上 -h 的选项,来提高可读性. [he@localhost ~]$ df -h文件系统(磁盘名称) 总容量 ...
- Python 中使用 ddt 来进行数据驱动,批量执行用例,修改ddt代码
1. 什么是数据驱动? 使用数据驱动有什么好处? 用例执行是靠数据来驱动的,每条测试用例除了测试数据不一样意外,所有的用例代码都是一样的,为了使用例批量执行,我们会使用数据驱动的思想来批量执行测试用例 ...
- 类的始祖Object
一.概述 Object时java中顶级父类,也是唯一没有父类的类:它是整个java中最基本的类,在java中所有的类都默认继承了Object. 二.重要方法 1.clone方法 克隆出一个新的对象. ...
- 四、React创建组件、 JSX使用、绑定数据、引用图片方式、数组(列表)循环输出
接:https://www.cnblogs.com/chenxi188/p/11702799.html 用上节建好的my-app项目: my-app/ README.md node_modules/ ...
- (六--二)scrapy框架之持久化操作
scrapy框架之持久化操作 基于终端指令的持久化存储 基于管道的持久化存储 1 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过 ...
- SpringBoot实现OAuth2认证服务器
一.最简单认证服务器 1. pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <a ...