虽然 以前也写过手机端页面 。当时用的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事件轮播图的更多相关文章

  1. js 基础篇(点击事件轮播图的实现)

    轮播图在以后的应用中还是比较常见的,不需要多少行代码就能实现.但是在只掌握了js基础知识的情况下,怎么来用较少的而且逻辑又简单的方法来实现呢?下面来分析下几种不同的做法: 1.利用位移的方法来实现 首 ...

  2. Day050--jQuery表单事件 轮播图 插件库 ajax

    表单控件的事件 change()表单元素发生改变时触发事件 select()文本元素发生改变时触发事件 submit()表单元素发生改变时触发事件 .focus() 获取焦点 .blur() 释放焦点 ...

  3. 自实现PC端jQuery版轮播图

    最近其他项目不是很忙,被安排给公司的官网项目做一个新的页面(之前没接触公司官网项目),其中有一个用到轮播图的地方,最开始想直接用swiper.js插件实现就好了,可是发现官网项目里之前都没有引入过sw ...

  4. Swiper 移动端全屏轮播图效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  5. 移动端轮播图实现方法(dGun.js)

    本文章介绍在移动端无缝隙轮播图实现的原理,这个轮子比较简单,但可以方便刚刚入门的同学参考.最终效果是在移动端无缝隙无限滑动,可以自定义轮播的速度.支持手势左右滑动.最后会放上源码. HTML部分 &l ...

  6. 原生js实现简单移动端轮播图

    最近项目不是很忙,自己就用原生js写了一个简单的移动端轮播图的小demo,可实现自动轮播和手势滑动轮播,然后就把它记录到个人博客里.还有很多不足的地方,希望多多指出,以便改进. 1.代码部分 分为四个 ...

  7. 自己用原生JS写的轮播图,支持移动端触摸滑动,分页器圆点可以支持mouseover鼠标移入和click点击,高手看了勿喷哈

    自己用原生JavaScript写的轮播图,分页器圆点按钮可支持click点击,也可支持mouseover鼠标悬浮触发,同时支持移动端触摸滑动,有兴趣的友友可以试试哈,菜鸟一枚,高手看了勿喷,请多多指正 ...

  8. 第124天:移动web端-Bootstrap轮播图插件使用

    Bootstrap JS插件使用 > 对于Bootstrap的JS插件,我们只需要将文档实例中的代码粘到我们自己的代码中> 然后作出相应的样式调整 Bootstrap中轮播图插件叫作Car ...

  9. 自己用原生JS写的轮播图,支持移动端触屏滑动,面向对象思路。分页器圆点支持click和mouseover。

    自己用原生javascript写的轮播图,面向对象思路,支持移动端手指触屏滑动.分页器圆点可以选择click点击或mouseover鼠标移入时触发.图片滚动用的setInterval,感觉setInt ...

随机推荐

  1. Makefile入门1

    Linux中的编译脚本Makefile的讲解设计 概念 编译控制脚本(.c.h----->bin) Makefile最终要的是清晰编译链接的整个过程 Makefile的优化设计 工作原理 mak ...

  2. CPU与内存

    一.存储单元内存有被划分为若干个存储单元,每个存储单元可存放1一个字节,即8个二进制位.因此内存的最小寻址单位是以字节进行的,每个存储单元都有一个编号.比如1GB的内存,可以储存1024*1024*1 ...

  3. 73.Python中ORM聚合函数详解:Count

    Count:用来求某个数据的个数. 在以下所有的示例中所采用的模型为: from django.db import models # 定义作者模型 class Author(models.Model) ...

  4. HZNU-ACM寒假集训Day12小结 数论入门 题解

    算不出的等式 BJOI2012 看到这题 真没什么办法 无奈看题解 1.注意到p/q 联想到斜率 2.注意到 [ ] 联想到整点 注意到k在变化,构造一次函数 f(x)=p/q*x ,g(x)=q/p ...

  5. CF1141E Superhero Battle

    A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n minu ...

  6. 学习spring的第4天

    关于老式的spring+mybatis整合,使用了druid连接池,还使用了mybatis-spring依赖(用于整合的),但是这个依赖本身就使用了spring-jdbc的某些类来处理事务方面的内容, ...

  7. 大数据高可用集群环境安装与配置(06)——安装Hadoop高可用集群

    下载Hadoop安装包 登录 https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 镜像站,找到我们要安装的版本,点击进去复制下载链接 ...

  8. 在Mac上使用docker+sql server+Navicat

    1. 版本:  2. 安装Kubernetes(并不知道安装这个有什么用) git clone https://github.com/maguowei/k8s-docker-desktop-for-m ...

  9. blkid命令 获取文件系统类型、UUID

    在Linux下可以使用blkid命令对查询设备上所采用文件系统类型进行查询.blkid主要用来对系统的块设备(包括交换分区)所使用的文件系统类型.LABEL.UUID等信息进行查询.要使用这个命令必须 ...

  10. 当chart图遇上bootstrap的TAB切换 无宽高问题?

    .tab-content > .tab-pane, .pill-content > .pill-pane {    display: block; /* undo display:none ...