移动端使用touchstart,touchmove,touchend实现图片轮播

本文介绍了如何利用移动端的touchstart、touchmove和touchend事件实现图片轮播功能。详细讲解了每个事件的含义,并提供了未封装的原始代码示例。通过函数封装优化了滑动体验,解决了速度调整和页面卡顿的问题。此外,为了兼容电脑端,增加了点击按钮切换图片的功能。
摘要由CSDN通过智能技术生成

移动端基础事件介绍

touchstart //手指触碰屏幕
touchmove //手指在屏幕上滑动
touchend //手指离开屏幕

移动端页面滑动图片滚动是再正常不过的事情了,这次碰巧写到这样一个滚动,就把代码放出来分享一下。首先做的是一个没有经过函数封装的单纯的滑屏轮播

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <!-- 防止屏幕进行伸缩 -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
    <style type="text/css">
        body,html,div,ul,li{
            padding:0;
            margin:0;
        }
        #pic{
            width:100%;
            height: 300px;
            overflow: hidden;
        }
        #pic #img{
            width:400%;
            height: 300px;
            position: relative;
        }
        #img div{
            width:25%;
            height: 300px;
            float: left;
            background-color: orange;
            text-align: center;
            line-height: 300px;
            color: white;
        }
        #circle{
            width:72px;
            height: 40px;
            margin:0 auto;
            position: relative;
            z-index: 2;
            top:-40px;
        }
        #circle ul li{
            width:8px;
            height: 8px;
            background-color: white;
            list-style: none;
            float: left;
            margin:15px 5px 0 5px;
        }
        #circle ul .active{
            background-color: gray;
        }
    </style>
</head>
<body>
    <div id="pic">
        <div id="img">
            <div>1111111</div>
            <div>2222222</div>
            <div>3333333</div>
            <div>4444444</div>
        </div>
    </div>
    <div id="circle">
        <ul>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <script type="text/javascript">
        window.o
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值