javascript控件开发之滚动条控件
首先,基于行前几篇开发的的框架,我们在目录 component\ui\下添加文件 com.ui.scrollBar.js, 在文件中定义com.ui.scrollBar类,继承com.ui.window类,如下
/**
* 滚动条控件.
* 创建:QZZ
* 日期:2014-03-01
*/
(function(undefined) {
nameSpace("com.ui");
/**
* 滚动条控件.
*/
com.ui.scrollBar = Extend(com.ui.window, {
/**
* 创建函数
*/
create:function() {
},
/**
* 渲染.
*/
render:function() {
}
});
})();
在滚动条的创建函数中定义一些基本属性,如滚动条类型、滚动总数量、显示数量、滚动位置、滚动一条的步长及拖动按钮最短长度等,如下
/**
* 创建函数
*/
create:function() {
this.base();
this.className = "com.ui.scrollBar";
this.logInfo("create");
//绑定明细总数量
this.scrollCount = 0;
//显示区域可见数量
this.showCount = 0;
//滚动位置
this.scrollIndex = 0;
//每步滚动像素长
this.stepLen = 2;
//拖动按钮最短长
this.dragBoxMinLen = 20;
//横、纵向滚动条
this.option.scrollType = this.option.scrollType || "V";
//滚动条是否可见
this.visible = true;
//按钮宽度
this.btnWidth = 15;
//初始化宽高属性
if(this.option.scrollType == "V") {
this.option.width = this.option.width || 16;
this.option.height = this.option.height || 100;
} else if(this.option.scrollType == "H") {
this.option.width = this.option.width || 100;
this.option.height = this.option.height || 16;
}
this.dragState = false;
},
定义好基本的属性后,开始绘制滚动条的框架, 这里我们采用来实现, 比如纵向滚动条,则生成一个三行一列的,第一行与第三行用作上下按钮,中间用作滚动区域,(横向的同理),
然后在滚动区域(上边的第二行)中又加入一个两行一列的,用作中间拖动按钮,第一行,绘制边线与背景相同,第二行绘制成按钮,通过调整第一行的行高来设定按钮的位置,
绘制完成后,添加按钮的事件,及拖动事件, 如下:
/**
* 渲染.
*/
render:function() {
this.base();
//创建滚动条结构
if(this.scrollTable == null) {
this.scrollTable = this.createElement("TABLE");
}
//清除原有的行列,结构变化的时候,会有已存在行
while(this.scrollTable.rows.length > 0) {
this.scrollTable.deleteRow(0);
}
//创建拖动按钮结构
if(this.dragTable == null) {
this.dragTable = this.createElement("TABLE");
}
//清除原有行列,结构变化的时候,会有已存在行,
while(this.dragTable.rows.length > 0) {
this.dragTable.deleteRow(0);
}
//插入行列
if(this.option.scrollType == "V" && this.scrollTable.rows.length != 3) {
this.priorBtn = this.scrollTable.insertRow(0).insertCell(0);
this.centerRect = this.scrollTable.insertRow(1).insertCell(0);
this.nextBtn = this.scrollTable.insertRow(2).insertCell(0);
this.dragLen = this.dragTable.insertRow(0).insertCell(0);
this.dragBox = this.dragTable.insertRow(1).insertCell(0);
this.centerRect.appendChild(this.dragTable);
} else if(this.option.scrollType == "H" && this.scrollTable.rows.length != 1) {
var tr = this.scrollTable.insertRow(0);
this.priorBtn = tr.insertCell(0);
this.centerRect = tr.insertCell(1);
this.nextBtn = tr.insertCell(2);
var tr1 = this.dragTable.insertRow(0);
this.dragLen = tr1.insertCell(0);
this.dragBox = tr1.insertCell(1);
this.centerRect.appendChild(this.dragTable);
}
var _this = this;
//拖动框鼠标按下事件, 并打印标志dragState,把控件置为焦点
this.dragBox.onmousedown = function(ev) {
//拖动状态
_this.dragState = true;
_this.focus = true;
}
//上一个按钮鼠标按下事件,这里处理长按时的效果,采用延迟执行的方式,
//scrollPrior()函数,是后续定义的向上滚动一行的函数,
this.setInt = null;
this.priorBtn.onmousedown = function(ev) {
_this.scrollPrior();
_this.mouseDown = true;
setTimeout(function() {
if(_this.mouseDown) {
_this.setInt = setInterval(function() {
_this.scrollPrior();
if(_this.scrollIndex == _this.scrollCount - _this.showCount) {
clearInterval(_this.setInt);
}
}, 50);//setInterval
}
}, 500); //setTimeout
}
//下一个按钮鼠标按下事件,这里处理长按时的效果,采用延迟执行的方式,
//scrollNext()函数,是后续定义的向下滚动一行的函数,
this.nextBtn.onmousedown = function(ev) {
_this.scrollNext();
_this.mouseDown = true;
setTimeout(function() {
if(_this.mouseDown) {
_this.setInt = setInterval(function() {
_this.scrollNext();
if(_this.scrollIndex == _this.scrollCount - _this.showCount) {
clearInterval(_this.setInt);
};
}, 50); //setInterval
}
}, 500); //setTimeout
}
//中间区域鼠标按下事件, 控制向下、上翻页,
//scrollPriorPage, scrollNextPage是后继定义的翻页函数。
this.centerRect.onmousedown = function(ev) {
var ev = ev || event;
if(_this.dragState != true) {
var dlen = 0, clen;
//debugger;
var rect = this.getBoundingClientRect();
if(_this.option.scrollType == "V") {
dlen = _this.dragLen.offsetHeight;
clen = ev.clientY - rect.top;
} else if(_this.option.scrollType == "H") {
dlen = _this.dragLen.offsetWidth;
clen = ev.clientX - rect.left;
}
if(clen > dlen) {
_this.scrollPriorPage();
} else {
_this.scrollNextPage();
}
}
}
//添加到win容器中
this.win.appendChild(this.scrollTable);
//刷新滚动条框, 该方法为后继定义的函数,用于刷新滚动的样式及位置。
this.refreshBox();
},
上边我们只是绘制出滚动条的结构,需要进行样式控制,下边refreshBox函数就是用于控制滚动条的样式及位置的,
/**
* 刷新布局
*/
refreshBox:function() {
var rw = this._getRectWidth(), rh = this._getRectHeight()
//设置按钮的样式及长、宽度,
if(this.option.scrollType == "V") {
this.priorBtn.style.height = this.btnWidth - 1 + "px";
this.setStyle(this.priorBtn, "scrollUp");
this.centerRect.style.height = (rh - 2 * this.btnWidth) + "px";
this.setStyle(this.centerRect, "scrollCell");
this.nextBtn.style.height = this.btnWidth - 1 + "px";
this.setStyle(this.nextBtn, "scrollDown");
this.dragTable.style.width = (rw - 1) + "px";
this.dragLen.style.height = "0px";
this.dragBox.style.height = this.btnWidth + "px";
} else if(this.option.scrollType == "H") {
this.priorBtn.style.width = this.btnWidth + "px";
this.setStyle(this.priorBtn, "scrollPrior");
this.centerRect.style.width = (rw - 2 * this.btnWidth) + "px";
this.setStyle(this.centerRect, "scrollCell");
this.nextBtn.style.width = this.btnWidth + "px";
this.setStyle(this.nextBtn, "scrollNext");
this.dragTable.style.height = rh + "px";
this.dragLen.style.width = "0px";
this.dragBox.style.width = this.btnWidth + "px";
}
//设置滚动区域的样式,及拖动按钮的样式,
this.setStyle(this.dragLen, "scrollCell");
this.setStyle(this.dragBox, "scrollDrag");
//设置滚动表格和拖动表格的样式,
this.setStyle(this.scrollTable, "scrollBox");
this.setStyle(this.dragTable, "scrollBox");
//设置宽、高
this.scrollTable.style.width = rw + "px";
this.scrollTable.style.height = rh + "px";
},
这上边用到几个样式,在com.comStyle.css文件中定义如下:
.scrollBox {
border-collapse:collapse;
border-spacing:0px;
padding:0px;
}
.scrollCell {
padding:0px;
vertical-align:top;
background-color:#eeeeee;
}
.scrollUp {
padding:0px;
background-color:#ddeeff;
border-Bottom:1px solid #C3D2E6;
}
.scrollDown {
padding:0px;
background-color:#ddeeff;
border-Top:1px solid #C3D2E6;
}
.scrollPrior {
padding:0px;
background-color:#ddeeff;
border-right:1px solid #C3D2E6;
}
.scrollNext {
padding:0px;
background-color:#ddeeff;
border-Left:1px solid #C3D2E6;
}
.scrollDrag {
padding:0px;
background-color:#ddeeff;
border:1px solid #C3D2E6;
}
完成以上结构后,只是完成了显示的效果,因为我们这个滚动条是通过设置数据明细数量来设定滚动的,因此需要与象素进行转换计算,以设置拖动按钮的位置,下面我们添加一个刷新函数,用于刷新滚动条的长度、拖动按钮的长度及位置,添加refresh函数,如下:
/**
*跟据滚动数据,刷新滚动条位置
*/
refresh:function() {
//计算滚动区域外的数量
var sl = this.scrollCount - this.showCount;
//发生了变化才重新计算
if(sl != this.tmpCount) {
//计算总象素长度
var slpx = sl * this.stepLen;
//计算拖动框长度
var cenLen = this.option.scrollType == "V"?
this.centerRect.offsetHeight:this.centerRect.offsetWidth;
var dragBoxLen = cenLen - slpx;
//如果计算出来的拖动按钮过短,则重新计算步长,保证按钮不再缩短
if(dragBoxLen cenLen - dragBoxLen) {
len = cenLen - dragBoxLen
}
//设置位置值
if(this.option.scrollType == "V") {
this.dragLen.style.height = len + "px";
} else if(this.option.scrollType == "H") {
this.dragLen.style.width = len + "px";
}
},
javascript控件开发之滚动条控件的更多相关文章
- MFC编程入门之二十六(常用控件:滚动条控件ScrollBar)
回顾上一节,讲的是组合框控件Combo Box的使用.本节详解滚动条控件Scroll Bar的相关内容. 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框和组合 ...
- VS2010/MFC编程入门之二十六(常用控件:滚动条控件Scroll Bar)
回顾上一节,鸡啄米讲的是组合框控件Combo Box的使用.本节详解滚动条控件Scroll Bar的相关内容. 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框 ...
- VS2010-MFC(常用控件:滚动条控件Scroll Bar)
转自:http://www.jizhuomi.com/software/191.html 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框和组合框设置了相应属性 ...
- WinForm控件开发总结目录
WinForm控件开发总结(一)------开篇 WinForm控件开发总结(二)------使用和调试自定义控件 WinForm控件开发总结(三)------认识WinForm控件常用的Attrib ...
- 自己开发基于c#的垂直滚动条控件
由于Visual Studio工具箱中自带滚动条控件外观太老,而且没有颜色外观属性可设置. 所以自己就试着开发一个垂直的滚动条,它可以用来控制TextBox的滚动. 由于代码比较多,源文件已经打包到网 ...
- JavaScript日历控件开发 C# 读取 appconfig文件配置数据库连接字符串,和配置文件 List<T>.ForEach 调用异步方法的意外 ef 增加或者更新的习惯思维 asp.net core导入excel 一个二级联动
JavaScript日历控件开发 概述 在开篇之前,先附上日历的代码地址和演示地址,代码是本文要分析的代码,演示效果是本文要实现的效果代码地址:https://github.com/aspwebc ...
- 自己动手用Javascript写一个无刷新分页控件
.NET技术交流群:337901356 ,欢迎您的加入! 对 于一个用户体验好的网站来说,无刷新技术是很重要的,无刷新,顾名思义,就是局部刷新数据,有用过Asp.net Web Form技术开发网页的 ...
- COM组件开发实践(八)---多线程ActiveX控件和自动调整ActiveX控件大小(下)
源代码下载:MyActiveX20081229.rar 声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改 ...
- WP8.1开发中ListView控件加载图列表的简单使用(1)
我也是刚接触WP编程没几个月,就是在这段时间一直闲着没事,然后又比较喜欢WP这款系统,就学习了WP这方面的开发言语,自学是很困难的,掌握这方面的资料不多,很初级,就是自己在网上找资料学习过程中,看到别 ...
随机推荐
- TCP/IP四层模型和OSI七层模型(模型分层的作用是什么)
TCP/IP四层模型和OSI七层模型的概念(模型分层的作用是什么) 一.总结 一句话总结: 减轻问题的复杂程度,一旦网络发生故障,可迅速定位故障所处层次,便于查找和纠错: 在各层分别定义标准接口,使具 ...
- dns 逐级查找顺序
1.浏览器 dns 缓存 2.Windows Host 文本 3.windows 本地 dns 缓存 方法/步骤 首先我们来查看win系统内保存的dns缓存并进行清空dns缓存操作. ... 点击确定 ...
- Delphi的日志库
1. 安装 Log4D下载: 官网地址 LoggerPro下载 GitHub地址 特点: log4d简单易用.性能稳定 LoggerPro貌似功能很强大,只是没有详细的文档,懒得翻源码 安装步骤 Lo ...
- MySQL中修改多个数据表的字段拼接问题
错误1: 异常:Truncated incorrect DOUBLE value: 'lili' 问题分析:我的修改sql语句是:update video set vname='汉字' and vdi ...
- mysql索引优化及explain关键字段解释
一.explain关键字解释 1.id MySQL QueryOptimizer 选定的执行计划中查询的序列号,表示查询中执行select 子句或操作表的顺序.id 值越大优先级越高,越先被执行.id ...
- MapReduce深入理解输入和输出格式(1)-输入分片与记录
一个输入分片( in put split)就是能够被单个map 操作 处理的输入块. 每一个map 操作只处理一个输入分片,并且一个一个地处理每条记录,也就是一个键/值对.输入分片和记录都是逻辑上的, ...
- lc6 ZigZag Conversion
lc6 ZigZag Conversion 分成两步, 第一步垂直向下, 1 1 1 1 第二步倾斜向上 1 1 1 1 1 1 1 用nRows个StringBuilder 然后将他们合并即可 cl ...
- css的层叠性+继承性+优先级+权重
一.层叠性 1.含义 多种css样式叠加,浏览器处理冲突的能力. 2.原则 1>一般情况下,若出现冲突,会按照css的书写顺序,以最后的样式为准 2>样式不冲突,就不会层叠 二.css的继 ...
- spark dataframe 将null 改为 nan
由于我要叠加rdd某列的数据,如果加数中出现nan,结果也需要是nan,nan可以做到,但我要处理的数据源中的nan是以null的形式出现的,null不能叠加,而且我也不能删掉含null的行,于是我用 ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...