自己一直很喜欢开发组件,只是OPP学的不是很精,自己在项目中用别人的框架进行项目开发,难免受制于人,也许这就是个人实际项目需求和框架提供的多少有点不符,引导我自己尝试开发一些自己常用的组件,话不多说,直接贴代码。

HTML代码部分:

 <body>
<div id="slide">
<div class="sliderbar-wrap">
<div class="sliderba-dot"></div>
<span class="sliderba-line"></span>
<span class="sliderba-baseline"></span>
</div>
</div>
<div id="slide1">
<div class="sliderbar-container">
<div class="sliderba-dot"></div>
<span class="sliderba-line"></span>
<span class="sliderba-baseline"></span>
</div>
</div>
<div id="slide2">
<div class="sliderbar-wrap1">
<div class="sliderba-dot"></div>
<span class="sliderba-line"></span>
<span class="sliderba-baseline"></span>
</div>
</div>
<div id="slide3">
<div class="sliderbar-container2">
<div class="sliderba-dot"></div>
<span class="sliderba-line"></span>
<span class="sliderba-baseline"></span>
</div>
</div>
</body>

CSS代码部分:

 #slide{
width: 200px;
height: 40px;
margin-top:10px;
}
#slide1{
width: 300px;
height: 40px;
margin-top:10px;
}
#slide2{
width: 400px;
height: 40px;
margin-top:10px;
}
#slide3{
width: 500px;
height: 40px;
margin-top:10px;
} .sliderbar-wrap,.sliderbar-container,.sliderbar-wrap1,.sliderbar-container2{
height: 40px;
position: relative;
}
.sliderba-dot{
width: 20px;
height: 20px;
background-color: #ccc;
border-radius: 50%;
border: 2px solid #ccc;
position: absolute;
top: 10px;
z-index:;
}
.sliderba-line,.sliderba-baseline{
height: 2px;
position: absolute;
top: 20px;
left: 20px;
}
.sliderba-line{
background-color: #ccc;
}
.sliderba-baseline{
z-index:;
}

Javascript代码部分:

 //构造函数
function SlideBar(sClasee){
var _this=this;
this.aWrap=document.querySelector(sClasee);
this.oDot=this.aWrap.querySelector('.sliderba-dot');
this.oLine=this.aWrap.querySelector('.sliderba-line');
this.oBaseline=this.aWrap.querySelector('.sliderba-baseline');
this.disX=0;
this.disY=0;
this.oDot.onmousedown=function(ev){
var ev=ev||window.event;
_this.fnDown(ev);
};
return false;
}
//mousedown函数
SlideBar.prototype.fnDown=function(ev){
var ev=ev||window.event;
var _this=this;
this.ww=this.aWrap.offsetWidth-24;
this.disX=ev.clientX-this.oDot.offsetLeft;
document.onmousemove=function(ev){
_this.fnMove(ev);
};
document.onmouseup=this.fnUp;
return false;
};
//mousemove函数
SlideBar.prototype.fnMove=function(ev){
var ev=ev||window.event;
var _this=this;
this.oDot.style.left=(ev.clientX-this.disX<0)?0:((ev.clientX-this.disX>this.ww)?(this.ww+'px'):(ev.clientX-this.disX+'px'));
this.oBaseline.style.width=this.oDot.offsetLeft+'px';
this.callback({
percent:Math.floor(this.oDot.offsetLeft/(this.aWrap.offsetWidth-this.oDot.offsetWidth)*100),
distanceLeft:this.oDot.offsetLeft*this.step
});
};
//mouseup函数
SlideBar.prototype.fnUp=function(){
document.onmousemove=null;
document.onmouseup=null;
}; //配置函数
SlideBar.prototype.config=function(options){
this.options=options===undefined?{}:options;
this.oDot.style.left=this.options.initPos === undefined?0:this.options.initPos;
this.step=this.options.step===undefined? 1 : this.options.step;
this.skin=this.options.skin===undefined? 1 : this.options.skin;
this.element=this.options.element===undefined?'FFF':this.options.element;
this.aWrap.style.width=this.options.width === undefined?'200px' : this.options.width ;
this.oLine.style.width=this.options.width === undefined?'160px' : parseInt(this.options.width)-this.oDot.offsetWidth*2+4 +'px';
this.callback=this.options.callback;
if(this.skin==1){
this.oDot.style.backgroundColor='#18df52';
this.oDot.style.borderColor='#18df52';
this.oBaseline.style.backgroundColor='#18df52';
}else if(this.skin==2){
this.oDot.style.backgroundColor='#18a2de';
this.oDot.style.borderColor='#18a2de';
this.oBaseline.style.backgroundColor='#18a2de';
}else if(this.skin==3){
this.oDot.style.backgroundColor='#b53400';
this.oDot.style.borderColor='#b53400';
this.oBaseline.style.backgroundColor='#b53400';
}else if(this.skin==4){
this.oDot.style.backgroundColor='#6b38de';
this.oDot.style.borderColor='#6b38de';
this.oBaseline.style.backgroundColor='#6b38de';
}
}
</script>

调用:

 <script>
window.onload=function(){ //实例化一个对象 int var int=new SlideBar('.sliderbar-wrap'); //设置配置参数 int.config({ initPos:0,//初始距离左边位置 默认是 0 step:1, //步长 默认是1 skin:1, // 圆点的颜色 skin 类型 1 2 3 width:'200px', //外层sliderbar-wrap的宽度 callback:function(res){//回调函数 默认传一个obj返回2个key 一个是百分比 一个是距离左边的px值 console.log(res) } }) //实例化一个对象 init var init = new SlideBar('.sliderbar-container'); //设置配置参数 init.config({ initPos:'0px',//初始距离左边位置 默认是 0 step:2, //步长 默认是1 skin:2, // 圆点的颜色 skin 类型 1 2 3 width:'300px', //外层sliderbar-wrap的宽度 callback:function(res){//回调函数 默认传一个obj返回2个key 一个是百分比 一个是距离左边的px值 console.log(res) } }) //实例化一个对象 init var init1 = new SlideBar('.sliderbar-wrap1'); //设置配置参数 init1.config({ initPos:'0px',//初始距离左边位置 默认是 0 step:3, //步长 默认是1 skin:3, // 圆点的颜色 skin 类型 1 2 3 width:'400px', //外层sliderbar-wrap的宽度 callback:function(res){//回调函数 默认传一个obj返回2个key 一个是百分比 一个是距离左边的px值 console.log(res) } }) //实例化一个对象 init var init2 = new SlideBar('.sliderbar-container2'); //设置配置参数 init2.config({ initPos:'0px',//初始距离左边位置 默认是 0 step:4, //步长 默认是1 skin:4, // 圆点的颜色 skin 类型 1 2 3 width:'500px', //外层sliderbar-wrap的宽度 callback:function(res){//回调函数 默认传一个obj返回2个key 一个是百分比 一个是距离左边的px值 console.log(res) } }) }
</script>

整个插件开发HTML:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>滑动条-slide 插件开发</title>
</head>
<style>
#slide{
width: 200px;
height: 40px;
margin-top:10px;
}
#slide1{
width: 300px;
height: 40px;
margin-top:10px;
}
#slide2{
width: 400px;
height: 40px;
margin-top:10px;
}
#slide3{
width: 500px;
height: 40px;
margin-top:10px;
} .sliderbar-wrap,.sliderbar-container,.sliderbar-wrap1,.sliderbar-container2{
height: 40px;
position: relative;
}
.sliderba-dot{
width: 20px;
height: 20px;
background-color: #ccc;
border-radius: 50%;
border: 2px solid #ccc;
position: absolute;
top: 10px;
z-index: 10;
}
.sliderba-line,.sliderba-baseline{
height: 2px;
position: absolute;
top: 20px;
left: 20px;
}
.sliderba-line{
background-color: #ccc;
}
.sliderba-baseline{
z-index: 9;
}
</style>
<body>
<div id="slide">
<div class="sliderbar-wrap">
<div class="sliderba-dot"></div>
<span class="sliderba-line"></span>
<span class="sliderba-baseline"></span>
</div>
</div>
<div id="slide1">
<div class="sliderbar-container">
<div class="sliderba-dot"></div>
<span class="sliderba-line"></span>
<span class="sliderba-baseline"></span>
</div>
</div>
<div id="slide2">
<div class="sliderbar-wrap1">
<div class="sliderba-dot"></div>
<span class="sliderba-line"></span>
<span class="sliderba-baseline"></span>
</div>
</div>
<div id="slide3">
<div class="sliderbar-container2">
<div class="sliderba-dot"></div>
<span class="sliderba-line"></span>
<span class="sliderba-baseline"></span>
</div>
</div>
</body>
<script>
window.onload=function(){ //实例化一个对象 int var int=new SlideBar('.sliderbar-wrap'); //设置配置参数 int.config({ initPos:0,//初始距离左边位置 默认是 0 step:1, //步长 默认是1 skin:1, // 圆点的颜色 skin 类型 1 2 3 width:'200px', //外层sliderbar-wrap的宽度 callback:function(res){//回调函数 默认传一个obj返回2个key 一个是百分比 一个是距离左边的px值 console.log(res) } }) //实例化一个对象 init var init = new SlideBar('.sliderbar-container'); //设置配置参数 init.config({ initPos:'0px',//初始距离左边位置 默认是 0 step:2, //步长 默认是1 skin:2, // 圆点的颜色 skin 类型 1 2 3 width:'300px', //外层sliderbar-wrap的宽度 callback:function(res){//回调函数 默认传一个obj返回2个key 一个是百分比 一个是距离左边的px值 console.log(res) } }) //实例化一个对象 init var init1 = new SlideBar('.sliderbar-wrap1'); //设置配置参数 init1.config({ initPos:'0px',//初始距离左边位置 默认是 0 step:3, //步长 默认是1 skin:3, // 圆点的颜色 skin 类型 1 2 3 width:'400px', //外层sliderbar-wrap的宽度 callback:function(res){//回调函数 默认传一个obj返回2个key 一个是百分比 一个是距离左边的px值 console.log(res) } }) //实例化一个对象 init var init2 = new SlideBar('.sliderbar-container2'); //设置配置参数 init2.config({ initPos:'0px',//初始距离左边位置 默认是 0 step:4, //步长 默认是1 skin:4, // 圆点的颜色 skin 类型 1 2 3 width:'500px', //外层sliderbar-wrap的宽度 callback:function(res){//回调函数 默认传一个obj返回2个key 一个是百分比 一个是距离左边的px值 console.log(res) } }) }
//构造函数
function SlideBar(sClasee){
var _this=this;
this.aWrap=document.querySelector(sClasee);
this.oDot=this.aWrap.querySelector('.sliderba-dot');
this.oLine=this.aWrap.querySelector('.sliderba-line');
this.oBaseline=this.aWrap.querySelector('.sliderba-baseline');
this.disX=0;
this.disY=0;
this.oDot.onmousedown=function(ev){
var ev=ev||window.event;
_this.fnDown(ev);
};
return false;
}
//mousedown函数
SlideBar.prototype.fnDown=function(ev){
var ev=ev||window.event;
var _this=this;
this.ww=this.aWrap.offsetWidth-24;
this.disX=ev.clientX-this.oDot.offsetLeft;
document.onmousemove=function(ev){
_this.fnMove(ev);
};
document.onmouseup=this.fnUp;
return false;
};
//mousemove函数
SlideBar.prototype.fnMove=function(ev){
var ev=ev||window.event;
var _this=this;
this.oDot.style.left=(ev.clientX-this.disX<0)?0:((ev.clientX-this.disX>this.ww)?(this.ww+'px'):(ev.clientX-this.disX+'px'));
this.oBaseline.style.width=this.oDot.offsetLeft+'px';
this.callback({
percent:Math.floor(this.oDot.offsetLeft/(this.aWrap.offsetWidth-this.oDot.offsetWidth)*100),
distanceLeft:this.oDot.offsetLeft*this.step
});
};
//mouseup函数
SlideBar.prototype.fnUp=function(){
document.onmousemove=null;
document.onmouseup=null;
}; //配置函数
SlideBar.prototype.config=function(options){
this.options=options===undefined?{}:options;
this.oDot.style.left=this.options.initPos === undefined?0:this.options.initPos;
this.step=this.options.step===undefined? 1 : this.options.step;
this.skin=this.options.skin===undefined? 1 : this.options.skin;
this.element=this.options.element===undefined?'FFF':this.options.element;
this.aWrap.style.width=this.options.width === undefined?'200px' : this.options.width ;
this.oLine.style.width=this.options.width === undefined?'160px' : parseInt(this.options.width)-this.oDot.offsetWidth*2+4 +'px';
this.callback=this.options.callback;
if(this.skin==1){
this.oDot.style.backgroundColor='#18df52';
this.oDot.style.borderColor='#18df52';
this.oBaseline.style.backgroundColor='#18df52';
}else if(this.skin==2){
this.oDot.style.backgroundColor='#18a2de';
this.oDot.style.borderColor='#18a2de';
this.oBaseline.style.backgroundColor='#18a2de';
}else if(this.skin==3){
this.oDot.style.backgroundColor='#b53400';
this.oDot.style.borderColor='#b53400';
this.oBaseline.style.backgroundColor='#b53400';
}else if(this.skin==4){
this.oDot.style.backgroundColor='#6b38de';
this.oDot.style.borderColor='#6b38de';
this.oBaseline.style.backgroundColor='#6b38de';
}
}
</script>
</html>

效果图如上,如果有BUG欢迎留言,共同完善。

 

插件开发-滑条(slide)开发的更多相关文章

  1. 滑屏 H5 开发实践九问

    滑屏的交互形式自从在 H5 中流行起来,便广泛应用在产品宣传.广告.招聘和活动运营等场景中,作为微信朋友圈广告惯用的形式,其影响力更是得到了强化与放大.如今滑屏H5可谓玲琅满目,数不尽数. 作为一个 ...

  2. [js插件开发教程]一步步开发一个可以定制配置的隔行变色小插件

    隔行变色功能,不用js,直接用css伪类就可以做,这个实例可以作为js插件开发很好的入门级实例.本文实现的隔行变色包括以下功能: 1,支持2种常用结构共存( div元素 和 表格类型 ) 2,一个页面 ...

  3. 一条java开发工程师的升级路线,从初级到无语言障碍

    看了一篇文章,讲述的是如何进行后端开发升级,现在分享下,我的总结,感谢写文章的作者大大,觉得他很会坚持,虽然一直在骂人,但是,我觉得人最大的敌人就是懒惰,所以骂得好 现在写下我的总结,希望对有志者有帮 ...

  4. QGIS 3.14插件开发——Win10系统PyCharm开发环境搭建四步走

    前言:最近实习要求做一个QGIS插件,网上关于QGIS 3.14插件开发环境搭建的文档不多,而且也不算太全面.正好实习的时候写了一个文档,在这里给大家分享一下. 因为是Word转的Markdown,可 ...

  5. H5滑条(input type=range)

    input[type=range] { -webkit-appearance: none; width: 230px; border-radius: 10px; /*这个属性设置使填充进度条时的图形为 ...

  6. 16条Android开发小经验

    1. TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以sp为单位的. 所以如果直接用返回的值来设置会出错,解决办法是 用setTextSiz ...

  7. 【插件开发】—— 10 JFace开发详解

    前文回顾: 1 插件学习篇 2 简单的建立插件工程以及模型文件分析 3 利用扩展点,开发透视图 4 SWT编程须知 5 SWT简单控件的使用与布局搭配 6 SWT复杂空间与布局搭配 7 SWT布局详解 ...

  8. 滑条滚动发请求要用Debounce

    import debounce from 'lodash.debounce'; this.deboucedFunc = debounce(this.viewModel.v_onHomeworkRequ ...

  9. IOS开发-UI学习-UISlider(滑动条)的使用

    滑动条即UISlider,是我们常见的软件中设置音量,亮度等的滑条,初始化及基本设置如下: // 新建滑动条 UISlider *slider = [[UISlider alloc]initWithF ...

随机推荐

  1. src-d engine 强大的git 历史分析工具

    src-d engine 包含了Babelfish ,同时可以看作是Babelfish 的升级版本,我们可以使用sql 进行代码分析 备注: 注意运行的时候需要容器支持 使用 安装docker   参 ...

  2. 使用kubebapps 管理helm 仓库已经应用使用Monocular专门提供helm 仓库查找

    Monocular 从1.0 开始专注于helm 的UI展示,对于部署以及维护已经去掉了,官方也提供了相关的说明以及 推荐了几个可选的部署工具,从使用以及架构上来说kubeapps 就是Monocul ...

  3. 对象的继承(prototype)

    修改构造函数的原型对象,批量修改所有子对象的继承关系

  4. HI3516EV100 RTMP添加音频

    1.先试试只发音频 失败 2.保存音频,发送视频,成功

  5. 笔记:Javascript 会提升变量声明

    笔记:Javascript 会提升变量声明 Javascript 会自动提升变量声明,但不会提升变量赋值. 如下代码, 按 F12 控制器显示的是 Hello, undefined 说明只是把 b 了 ...

  6. 原码,反码与补码的概念以及Java中数的存储方式

    *原码,反码,补码必须满8位,不足在前填0: 1,原码:用符号位和数值位表示一个带符号的数 +  -> 0                 -   -> 1     表示数的范围-127~ ...

  7. nginx 学习资料

    nginx 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-type( ...

  8. NET设计模式 第二部分 结构性模式(8):桥接模式(Bridge Pattern)

    桥接模式(Bridge Pattern) ——.NET设计模式系列之九 Terrylee,2006年2月 概述 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维 ...

  9. 大数据离线分析平台 JavaSDK数据收集引擎编写

    JavaSDK设计规则 JavaSDK提供两个事件触发方法,分别为onChargeSuccess和onChargeRefund.我们在java sdk中通过一个单独的线程来发送线程数据,这样可以减少对 ...

  10. Linux命令之sed

    sed命令格式 sed [options] 'command' file(s) 选项 -e :直接在命令行模式上进行sed动作编辑,此为默认选项; -f :将sed的动作写在一个文件内,用–f fil ...