原生JavaScript+CSS3实现移动端滑块效果

        在做web页面时,无论PC端还是移动端,我们会遇到滑块这样的效果,可能我们往往会想着去网上找插件,其实这个效果非常的简单,插件代码的的代码往往过于臃肿,不如自己动手,自给自足。首先看一下效果图:

 

分析效果:

    1、按钮的右侧有一个小动画,深黄色的小头是一圈圈循环流动的。

    2、只在按钮上滑动可以启动按钮。

    3、并且要判断按钮是否滑到头,如果没有滑到头,手指离开屏幕,按钮自动弹回左侧;如果滑到头,执行一个函数。

 

解决办法:

    1、动画效果需要使用CSS3里面的@keyframes来操作,代码如下:

 1 .god-bottom .swifter-track{
 2     width: 50%;
 3     height: 93%;
 4     border-radius: 5px;
 5     position: absolute;
 6     right:0;
 7     top:0;
 8     background:  url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track1.png") no-repeat 0 55%;
 9     background-size: 100% 70%;
10     -webkit-animation:track 0.5s infinite;
11     animation:track 0.5s infinite;
12 }
13 @keyframes track {
14     0%{
15         background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track1.png") no-repeat 0 55%;
16         background-size: 100% 70%;
17     }
18     35%{
19         background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track2.png") no-repeat 0 55%;
20         background-size: 100% 70%;
21     }
22     70%{
23         background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track3.png") no-repeat 0 55%;
24         background-size: 100% 70%;
25     }
26     100%{
27         background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track4.png") no-repeat 0 55%;
28         background-size: 100% 70%;
29     }
30 }
31 @-webkit-keyframes  track {
32     0%{
33         background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track1.png") no-repeat 0 55%;
34         background-size: 100% 70%;
35     }
36     35%{
37         background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track2.png") no-repeat 0 55%;
38         background-size: 100% 70%;
39     }
40     70%{
41         background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track3.png") no-repeat 0 55%;
42         background-size: 100% 70%;
43     }
44     100%{
45         background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track4.png") no-repeat 0 55%;
46         background-size: 100% 70%;
47     }
48 }

 

    2、这里需要分别监听touchstart、touchmove、touchend事件,代码如下:

 1 function sliderFun(outDiv,inDiv,funName) {
 2     var startX,endX,distance=0;
 3     var dis=outDiv.clientWidth-inDiv.clientWidth;
 4     inDiv.addEventListener('touchstart',function (e) {
 5         startX=e.touches[0].clientX;
 6     },false)
 7     inDiv.addEventListener('touchmove',function (e) {
 8         endX=e.touches[0].clientX;
 9         distance=endX-startX;
10         if(distance<=dis&&distance>=0){
11             inDiv.style.left=distance+'px';
12         }
13     },false)
14     inDiv.addEventListener('touchend',function (e) {
15         if(distance<=dis&&distance>=0){
16             inDiv.style.left='0px';
17         }else if(distance>=dis){
18             funName();
19         }
20     },false)
21 }  

 

        为了方便大多去测试呢,最后我把所有的代码一并贴上,希望可以帮助到大家:

 

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><!--强制以webkit内核来渲染-->
  6     <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"><!--按设备宽度缩放,并且用户不允许手动缩放-->
  7     <meta name="format-detection" content="telephone=no"><!--不显示拨号链接-->
  8     <title></title>
  9     <style>
 10         *{
 11             margin:0;
 12             padding:0;
 13         }
 14         html,body{
 15             width:100%;
 16         }
 17         .shadow-big{
 18             width: 100%;
 19             height: 100%;
 20             background-color: rgba(0,0,0,0.6);
 21             position: fixed;
 22             z-index: 2;
 23             top: 0;
 24             left:0;
 25             /*display: none;*/
 26         }
 27         .money-god-big{
 28             width: 77.333vw;
 29             height: 93vw;
 30             position: fixed;
 31             left:0;
 32             right:0;
 33             top:40vw;
 34             margin: auto;
 35             z-index: 10;
 36         }
 37         .god-bottom{
 38             width: 82.759%;
 39             height: 58.333%;
 40             margin: 0 auto;
 41             background-color: #d63532;
 42             border-radius: 8px;
 43             position: relative;
 44         }
 45         .god-bottom>div{
 46             position: absolute;
 47             left:0;
 48             right:0;
 49             margin: auto;
 50             text-align: center;
 51             width: 100%;
 52             color: #fff;
 53             font-size: 4.8vw;
 54         }
 55         .god-bottom .bottom-ttl{
 56             top: 9.524%;
 57             color: #fcd741;
 58         }
 59         .god-bottom .bottom-money{
 60             top: 27.381%;
 61             font-size: 9.6vw;
 62         }
 63         .god-bottom .bottom-reward{
 64             top: 53.095%;
 65         }
 66         .god-bottom .swifter-out{
 67             width: 87.5%;
 68             height: 21.905%;
 69             top:70.952%;
 70             background-color: #a92d2b;
 71             border-radius: 5px;
 72             position: relative;
 73         }
 74         .god-bottom .swifter-in{
 75             width: 50%;
 76             height: 93%;
 77             background-color: #fcd741;
 78             border-radius: 5px;
 79             color: #d63532;
 80             font-size: 4.8vw;
 81             line-height: 2.4;
 82             position: absolute;
 83             left:0;
 84             top:0;
 85         }
 86         .god-bottom .swifter-track{
 87             width: 50%;
 88             height: 93%;
 89             border-radius: 5px;
 90             position: absolute;
 91             right:0;
 92             top:0;
 93             background:  url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track1.png") no-repeat 0 55%;
 94             background-size: 100% 70%;
 95             -webkit-animation:track 0.5s infinite;
 96             animation:track 0.5s infinite;
 97         }
 98         @keyframes track {
 99             0%{
100                 background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track1.png") no-repeat 0 55%;
101                 background-size: 100% 70%;
102             }
103             35%{
104                 background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track2.png") no-repeat 0 55%;
105                 background-size: 100% 70%;
106             }
107             70%{
108                 background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track3.png") no-repeat 0 55%;
109                 background-size: 100% 70%;
110             }
111             100%{
112                 background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track4.png") no-repeat 0 55%;
113                 background-size: 100% 70%;
114             }
115         }
116         @-webkit-keyframes  track {
117             0%{
118                 background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track1.png") no-repeat 0 55%;
119                 background-size: 100% 70%;
120             }
121             35%{
122                 background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track2.png") no-repeat 0 55%;
123                 background-size: 100% 70%;
124             }
125             70%{
126                 background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track3.png") no-repeat 0 55%;
127                 background-size: 100% 70%;
128             }
129             100%{
130                 background: url("http://images.cnblogs.com/cnblogs_com/luanhewei/880330/o_track4.png") no-repeat 0 55%;
131                 background-size: 100% 70%;
132             }
133         }
134     </style>
135 
136 </head>
137 <body>
138     <!--shadow-->
139     <div class="shadow-big">
140         <div class="money-god-big">
141             <div class="god-bottom">
142                 <div class="bottom-ttl">支付成功 奖励红包</div>
143                 <div class="bottom-money">6.00元</div>
144                 <div class="bottom-reward">累计奖励23.25元</div>
145                 <div class="swifter-out">
146                     <div class="swifter-in">存入余额</div>
147                     <div class="swifter-track"></div>
148                 </div>
149             </div>
150         </div>
151     </div>
152     <script>
153 //        获取className
154         function CN(t) {
155             return document.getElementsByClassName(t);
156         }
157 
158         var swifterOut1=CN('swifter-out')[0];
159         var swifterIn1=CN('swifter-in')[0];
160         var trackImg1=CN('track-img')[0];
161       
162         var shadowBig=CN('shadow-big')[0];
163   
164         sliderFun(swifterOut1,swifterIn1,final1);
165 //        滑动效果
166         function sliderFun(outDiv,inDiv,funName) {
167             var startX,endX,distance=0;
168             var dis=outDiv.clientWidth-inDiv.clientWidth;
169             inDiv.addEventListener('touchstart',function (e) {
170                 startX=e.touches[0].clientX;
171             },false)
172             inDiv.addEventListener('touchmove',function (e) {
173                 endX=e.touches[0].clientX;
174                 distance=endX-startX;
175                 if(distance<=dis&&distance>=0){
176                     inDiv.style.left=distance+'px';
177                 }
178             },false)
179             inDiv.addEventListener('touchend',function (e) {
180                 if(distance<=dis&&distance>=0){
181                     inDiv.style.left='0px';
182                 }else if(distance>=dis){
183                     funName();
184                 }
185             },false)
186 }
187         function final1() {
188             alert("阴影将要消失了!");
189             shadowBig.style.display='none';
190         }
191     </script>
192 </body>
193 </html>
View Code

 

转载于:https://www.cnblogs.com/luanhewei/p/5857506.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/372341.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql的连接名是哪个文件_mysql连接名是什么

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云数据库专家保驾护航&#xff0c;为用户…

Activiti绩效对决

每个人在学习Activiti时都会一直问到的问题&#xff0c;与软件开发本身一样古老&#xff1a;“它如何执行&#xff1f;”。 到现在为止&#xff0c;当您问我同样的问题时&#xff0c;我将告诉您Activiti如何以各种可能的方式最小化数据库访问&#xff0c;如何将流程结构分解为“…

怎么使用CKEDITOR

出于工作需求&#xff0c;自己在网上找了个文本编辑器控件, 网址是http://ckeditor.com/ 怎么使用&#xff1f; 先插入脚本<script type"text/javascript" src"*/ckeditor.js"></script>, 然后&#xff0c;在自己的脚本里调用CKEDITOR.replac…

centos 打开pdo_mysql_centos中添加php扩展pdo_mysql步骤

pdo_mysql是php中一个mysql连接类了&#xff0c;我们可以直接使用pdo_mysql来操作数据库这样自己可以不需要写数据库操作类了&#xff0c;下面来介绍在centos中安装pdo_mysql扩展的步骤。本文内容是以 CentOS 为例&#xff0c;红帽系列的 Linux 方法应该都是如此&#xff0c;下…

Java线程死锁–案例研究

本文将描述从在IBM JVM 1.6上运行的Weblogic 11g生产系统中观察到的最新Java死锁问题的完整根本原因分析。 此案例研究还将证明掌握线程转储分析技能的重要性&#xff1b; 包括用于IBM JVM Thread Dump格式。 环境规格 – Java EE服务器&#xff1a;Oracle Weblogic Server 1…

bzoj1968: [Ahoi2005]COMMON 约数研究

水题。。。 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep(i,s,t) for(int is;i<t;i) int main(){int ans0,n;scanf("%d",&n);rep(i,1,n) ansn/i;printf("%d\n…

题目1457:非常可乐(广度优先遍历BFS)

题目链接&#xff1a;http://ac.jobdu.com/problem.php?pid1457 详解链接&#xff1a;https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码&#xff1a; // // 1457 非常可乐.cpp // Jobdu // // Created by PengFei_Zheng on 22/04/2017. // Copyright © 2017 Pe…

mysql查询某张表的所有外键_oracle中查询所有外键引用到某张表的记录

欢迎进入Oracle社区论坛&#xff0c;与200万技术人员互动交流 >>进入 oracle中查询所有外键引用到某张表的记录 //查询表的主键约束名 select * from user_constraints e where e.table_name表名;--输入 //查询所有引用到该主键的记录 select b.table_name,b.column_欢迎…

BTrace for Java应用程序简介

本文的目的是学习如何使用BTrace动态跟踪/观察正在运行的Java应用程序&#xff08;JDK 6&#xff09;&#xff0c;而无需更改应用程序的代码和配置参数。 什么是BTrace&#xff1f; BTrace是一个开源项目&#xff0c;始于2007年&#xff0c;最初由A.Sundararajan和K.Balasubra…

丛铭俣 160809324 (作业1)

老师&#xff0c;助教好&#xff01;我是计科3班的丛铭俣&#xff0c;我的性格阳光开朗&#xff0c;待人大方友善&#xff0c;凡事不喜欢斤斤计较&#xff1b;本人热心&#xff0c;喜欢乐于助人&#xff0c;也喜欢和积极向上的人交朋友。最喜欢打羽毛球&#xff0c;其次是篮球&…

mysql死锁分析_MySQL死锁分析

熟悉或者了解数据库的朋友都知道锁的概念&#xff0c;这里不做过多的解析&#xff01;锁的种类有很多&#xff0c;不同数据库的锁管理方式也不同。这里主要谈下MySQL innodb引擎下的死锁。死锁通俗的来讲就是2个事务相互请求对方持有的锁&#xff0c;这样就会造成2个事务相互等…

在Akka中实现主从/网格计算模式

主从模式是容错和并行计算的主要示例。 模式背后的想法是将工作划分为相同的子任务&#xff0c;然后将其委派给从属。 这些从节点或实例将处理工作任务&#xff0c;并将结果发送回主节点。 然后主节点将编译从所有从节点接收到的结果。关键是从节点仅知道如何处理任务&#xff…

java学习笔记总略

二、正文&#xff08;一&#xff09;Java1.接口和抽象类的区别①抽象类里可以有构造方法&#xff0c;而接口内不能有构造方法。②抽象类中可以有普通成员变量&#xff0c;而接口中不能有普通成员变量。③抽象类中可以包含非抽象的普通方法&#xff0c;而接口中所有的方法必须是…

react实现路由跳转_react实现hash路由

众所周知&#xff0c;目前单页面使用的路由有两种实现方式&#xff1a;hash 模式history 模式hash 模式路由原理&#xff1a;我们先来看hash模式&#xff0c;页面首次加载时需要在load事件中解析初始的URL&#xff0c;从而展示进入的页面。当 # 后面的哈希值发生变化时&#xf…

Java中的Google协议缓冲区

总览 协议缓冲区是一种用于结构化数据的开源编码机制。 它是由Google开发的&#xff0c;旨在实现语言/平台中立且可扩展。 在本文中&#xff0c;我的目的是介绍Java平台上下文中协议缓冲区的基本用法。 Protobuff比XML更快&#xff0c;更简单&#xff0c;并且比JSON更紧凑。 当…

匈牙利哦模板 二分匹配 完全匹配问题

匈牙利算法的核心思想就是 腾空间, 有条件 创造,没条件也要创造! bool find(int x){int i,j;for (j1;j<m;j){ //扫描每个被匹配的人 if (line[x][j]true && used[j]false) //如果有关系并且还没有标记过(这里标记的意思是这次查找曾试图改变过的归属问题&a…

ThinkPHP 中验证码的看不清切换

<!--HTML页面--> <!DOCTYPE html><html><head> <title></title></head><body><script type"text/javascript" src"__PUBLIC__/js/jquery-1.8.2.min.js"></script><form action"{:U(H…

mysql从表截取信息_mysql中循环截取用户信息并插入到目标表对应的字段中

操作环境&#xff1a;有表game_list&#xff0c;字段&#xff1a;uid&#xff0c;score1&#xff0c;score2&#xff0c;seat_id&#xff0c;last_update&#xff1b;传入参数为i_player_detail &#xff0c;传入的值为多个用户的id、之前分数、之后分数、座位号&#xff0c;每…

Java中的数组,列表,集合,映射,元组,记录文字

有时&#xff0c;当我对JavaScript的强大功能和表现力感到兴奋时&#xff0c;我发现自己错过了Java世界中的一两个功能。 除了lambda表达式/闭包或任何您想称为“匿名函数”的东西之外&#xff0c;它还对数组&#xff0c;数组&#xff0c;列表&#xff0c;集合&#xff0c;映射…

mysql锁表问题的解决方法_MYSQL锁表问题的解决方法

本文实例讲述了MYSQL锁表问题的解决方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;很多时候&#xff01;一不小心就锁表&#xff01;这里讲解决锁表终极方法&#xff01;案例一mysql>show processlist;参看sql语句一般少的话mysql>kill thread_id;就可以解…