虽然 以前也写过手机端页面 。当时用的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. Ubuntu安装Python版本管理工具pyenv

    gyf@gyf-VirtualBox:~$ git clone https://github.com/yyuu/pyenv.git ~/.pyenvCloning into '/home/gyf/.p ...

  2. 基于 Annotation 的装配(注解)

    注解:就是一个类,使用@注解名称 开发中:使用注解 取代 xml配置文件. 1. @Component取代<bean class=""> @Component(&quo ...

  3. (转)Navicat Premium 连接Oracle 数据库(图文教程)

    Navicat premium是一款数据库管理工具,是一个可多重连线资料库的管理工具,它可以让你以单一程式同时连线到MySQL.SQLite.Oracle及PostgreSQL 资料库,让管理不同类型 ...

  4. css画布

    绘制基本图形 绘制直线 <style> .canvas{ } </style> <canvas id="myCanvas1" style=" ...

  5. Spring配置数据源的三种方法

    前言:今天接触新项目发现用的是JNDI配置数据源,用度娘倒腾了一会也没弄好,只好用平常用的方法,结果发现BasicDataSource和DriverManagerDataSource也是不同的,所以记 ...

  6. jsp页面使用<% 语句%> SQL Server数据库报空指针异常(在控制台可以正常执行)

    一直反感用SQL Server数据库,很影响电脑性能!!数据库作业不得不用 前几天作业一直报空指针异常: 自己检查了所传参数,和数组不为空 数据库查询语句不为空 然后查看SQL服务是否启动  主要是S ...

  7. java web 最方便的配置filter方法,filter到底怎么配置?

    小白学习的艰辛历程! 我只说最新版本的eclipse自从servelt3.0以后 servelt 和filter 都不用到web.xml中配置,个人对servelt和filter理解不太深入,但是自从 ...

  8. (排序EX)P1093 奖学金

    题解: #include<iostream>using namespace std;int r=0;void swap(int &a,int &b){  int t=a;  ...

  9. intellij idea 怎么全局搜索

    1.Ctrl+N按名字搜索类 相当于eclipse的ctrl+shift+R,输入类名可以定位到这个类文件,就像idea在其它的搜索部分的表现一样,搜索类名也能对你所要搜索的内容多个部分进行匹配,而且 ...

  10. 【剑指Offer】面试题10- II. 青蛙跳台阶问题

    题目 一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶.求该青蛙跳上一个 n 级的台阶总共有多少种跳法. 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返 ...