jquery自定义进度条与h5原生进度条
<div class="box-nine">
<div class="progress"> <!--一整条进度条-->
<div class="progress-bar"></div> <!--进度-->
<div class="progress-circle"></div><!--控制点-->
</div>
<input type="text" value=""><!--显示进度值-->
</div>
简单的写一下样式
.progress{
width:200px;
height:6px;
background: #cccccc;
border-radius: 6px;
position: relative;
display: inline-block;
cursor: pointer;
}
.progress .progress-bar{
width:100px;
height: 6px;
position: absolute;
background:red;
border-radius: 6px;
top:0;
}
.progress .progress-circle{
width:14px;
height:14px;
background: #ffffff;
border:1px solid #ccc;
box-shadow: 0 0 5px 0 #000 0.27;
border-radius: 50%;
position: absolute;
top:-4px;
cursor: pointer;
left:98px;
}
input[type='text']{
width:47px;
height:30px;
line-height: 30px;
}
看下简单的静态页面效果

接下来看实现
第一步:引入jquery
<script type="text/javascript" src="jquery.js"></script>
第二部:jquery
var lastX, NewX, that, thewidth = 0;
var auto = false;//控制是否可以拖动的; false表示不能拖动
$('.box-nine').on('mousedown', '.progress-circle', function(e) {
lastX = e.clientX; //鼠标按下时距离浏览器的x轴距离
e.preventDefault(); //阻止默认事件
that = $(this); //保存当前this
thewidth = that.prev().width() || $(".progress-bar").width(); // 获取当前进度的宽度,也就是红色条的宽度
auto = true; //鼠标按下的时候设置为true,允许拖动
})
$(document).on('mousemove', function (e) { //记住,这里一定要绑定在document上,防止拖动过快造成问题
if (auto) {
e.preventDefault();
NewX = e.clientX; //拖动后,重新获取鼠标距离浏览器的x轴距离
var mcs = NewX - lastX; //由mcs的值大小确定鼠标是向左滑动还是右滑动;
var differ = $(".progress").width() - $(".progress-circle").width(); //圆圈能拖动的最大距离 红色进度条的最大宽度就是整个进度条200px,而圆圈能滑动的最大距离要减去本身的宽度
if (mcs > 0) {//判断左滑还是右滑
that.prev().css('width', mcs + thewidth < differ ? mcs + thewidth : $(".progress").width()); //每拖动一次,都要加上当前红色条的宽度 红色条宽度=每次的鼠标差+ 上一次红色条宽度
that.css('left', mcs + thewidth < differ ? mcs + thewidth : differ);
} else {
that.prev().css('width', thewidth + mcs - that.width() > 0 ? thewidth + mcs - that.width() : 0);
that.css('left', thewidth + mcs - that.width() > 0 ? thewidth + mcs - that.width() : 0);
}
var val = that.prev().width();
that.parents('.box-nine').find('input').val(Math.ceil(val / 2));//实时将值显示在Input框 这里是模拟音量 0-100的值
}
})
$(document).on('mouseup', function(e) {
if(auto) {
auto = false;//鼠标松开不允许拖动
}
})
$(".box-nine").on('click', '.progress', function(e) { //点击进度条时,进度的变化
lastX = e.clientX;
var offsetX = $(this).offset().left;
$(this).find('.progress-bar').css('width', lastX - offsetX>$(this).width()?$(this).width():lastX-offsetX);
$(this).find('.progress-circle').css('left', lastX - offsetX>$(this).width()- $(this).find('.progress-circle').width()?$(this).width()- $(this).find('.progress-circle').width():lastX-offsetX);
var val =$(this).find('.progress-bar').width();
$(this).parents('.box-nine').find('input').val(Math.ceil(val / 2));
})
<div class="box-one" style="position:relative;margin-top:10px;">
<progress value="" max="100"> </progress> <!--h5 进度条-->
<div class="progress-circle1"></div>
<input type="text" value=""><!--显示进度值-->
</div>
progress{
background: #cccccc;
width:202px;
height:6px;
border:1px solid #ddd;
border-radius: 6px;
cursor: pointer;
position: relative;
margin-right:10px;
}
progress::-moz-progress-bar { background: #ccc}
progress::-webkit-progress-value {
background: red;
height:6px;
border-radius: 6px 0 0 6px;
position: absolute;
top:-1px;
}
progress::-webkit-progress-bar {
background: #ccc
}
.progress-circle1{
width:14px;
height:14px;
background: #ffffff;
border:1px solid #ccc;
box-shadow: 0 0 5px 0 #000 0.27;
border-radius: 50%;
position: absolute;
top:15px;
cursor: pointer;
left:0px;
}
此样式不兼容谷歌哟;通过手动修改progress的样式
var lastX,offset, thats,thewidth=0;
var auto=false;
$(".box-one").on('mousedown','.progress-circle1',function(e){
lastX=e.clientX;
e.preventDefault();
thats=$(this);
thewidth=thats.prev().val() || $("progress").val();
auto=true;
})
$(document).on('mousemove',function(e){
if(auto){
e.preventDefault();
newX=e.clientX;
var mcs=newX-lastX;
var differ=thats.prev().width()-thats.width();
if(mcs>0){
thats.prev().val(thewidth*2+mcs>$("progress").width()?$("progress").width()/2:(thewidth*2+mcs)/2);
thats.css('left',mcs+thewidth*2<differ?mcs+thewidth*2:differ );
}else{
thats.prev().val( thewidth+ mcs/2>0?thewidth+ mcs/2:0);
thats.css('left',thewidth*2 + mcs - thats.width()> 0 ? thewidth*2 + mcs - thats.width():0)
}
var val = thats.prev().val() || $("progress").val();
thats.parents('.box-one').find('input').val(Math.ceil(val ));//实时将值显示在Input框 这里是模拟音量 0-100的值 }
})
$(document).on('mouseup', function(e) {
if(auto) {
auto = false;//鼠标松开不允许拖动
}
})
$(".box-one").on('click', 'progress', function (e) {
that=$(this);
lastX = e.clientX;
offset = $(this).offset().left;
that.parent().find('.progress-circle1').css('left', lastX - offset>that.width() ? that.width()-$(".progress-circle1").width():lastX - offset);
that.val((lastX - offset)/2);
that.parent().find('input').val(Math.ceil((lastX - offset)/2));
})

详细代码:https://github.com/sulishibaobei/progress/tree/master
jquery自定义进度条与h5原生进度条的更多相关文章
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...
- 30款基于 jQuery & CSS3 的加载动画和进度条插件
我们所生活每一天看到的新技术或新设计潮流的兴起,Web 开发正处在上升的时代.HTML5 & CSS3 技术的发展让 Web 端可以实现的功能越来越强大. 加载动画和进度条使网站更具吸引力.该 ...
- 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)
今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...
- CodePush自定义更新弹框及下载进度条
CodePush 热更新之自定义更新弹框及下载进度 先来几张弹框效果图 非强制更新场景 image 强制更新场景 image 更新包下载进度效果 image 核心代码 这里的热更新Modal框,是封装 ...
- Jquery progressbar通过Ajax请求获取后台进度演示
项目源代码下载:http://download.csdn.net/detail/nuptboyzhb/6262253 1.简介 本文主要演示Jquery progressbar的进度条功能.js通过a ...
- 安卓自定义View实现图片上传进度显示(仿QQ)
首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果): 再看下图我们实现的效果: 实现原理很简单,首先我们上传图片时需要一个进度值progress,这个不管是自己写的上传的方法还是使用第三方 ...
- jQuery自定义插件--banner图滚动
前言 jQuery是一个功能强大的库,提供了开发JavaScript项目所需的所有核心函数.很多时候我们使用jQuery的原因就是因为其使用插件的功能,然而,有时候我们还是需要使用自定义代码来扩展这些 ...
- JQuery自定义插件详解之Banner图滚动插件
前 言 JRedu JQuery是什么相信已经不需要详细介绍了.作为时下最火的JS库之一,JQuery将其"Write Less,Do More!"的口号发挥的极致.而帮助J ...
- 基于Jquery WeUI的微信开发H5页面控件的经验总结(2)
在微信开发H5页面的时候,往往借助于WeUI或者Jquery WeUI等基础上进行界面效果的开发,由于本人喜欢在Asp.net的Web界面上使用JQuery,因此比较倾向于使用 jQuery WeUI ...
随机推荐
- mysql存储过程 详细注释
原文:https://my.oschina.net/u/3582142/blog/1581929
- 【图文】如何在centos上安装tomcat
先到tomcat官网下载安装包(随便下载你想要的版本) 假设你现在使用的是windows系统 那么就把你下载来的压缩包解压,放到一个目录中 在你本地的windows系统中安装个xshell和xftp ...
- nodejs 之 nvm和pm2
说道 node不得不提到nodejs的版本管理nvm和Node应用的进程管理器pm2. 当然,关于这两个的介绍的文章那么多,随意baidu,bing,google就可以. 我这里是给自己打一个标签,方 ...
- 使用ui-route实现多层嵌套路由
一.预期实现效果: https://liyuan-meng.github.io/uiRouter-app/index.html (项目地址:https://github.com/liyuan-meng ...
- Android CoordinatorLayout、AppBarLayout、DrawerLayout、NavigationView 的使用及问题小结
这里只对Material Design中这几种组件使用的重要部分以及容易出现问题的地方进行汇总(遇坑请直接看最后常见问题部分),详细用法请自行查阅官方文档 一.CoordinatorLayout 介绍 ...
- 巧用CSS居中未知高度的块元素
在网页中让一个未知高度的块元素水平垂直居中是一个老生常谈的问题,但是总是有些特殊场景让你无法得心应手的实现居中,本文介绍几种常用的经典的居中方法,总有一种适合你! 1. position .paren ...
- C语言中不同变量的访问方式
C语言中的变量大致可以分为全局变量,局部变量,堆变量和静态局部变量,这些不同的变量存储在不同的位置,有不同的生命周期.一般程序将内存分为数据段.代码段.栈段.堆段,这几类变量存储在不同的段中,造成了它 ...
- CentOS7服务去Nginx使用-安装
1.官网下载目前稳定版本nginx 这里我使用1.12.1版本进行安装. 上传到linux的/mnt/pack/nginx这个目录,主要是存放linux安装相关的文件. 可以使用window下载好上传 ...
- 什么时候可以用delete替代delete[]
针对gcc编译器 C++内存分配和释放函数 //分配单个对象 operator new(std::size_t size) //分配数组对象 operator new[](std::size_t si ...
- Java分布式应用技术架构
分布式架构的演进 系统架构演化历程-初始阶段架构初始阶段 的小型系统 应用程序.数据库.文件等所有的资源都在一台服务器上通俗称为LAMP特征:应用程序.数据库.文件等所有的资源都在一台服务器上.描述: ...