首先,基于行前几篇开发的的框架,我们在目录 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控件开发之滚动条控件的更多相关文章

  1. MFC编程入门之二十六(常用控件:滚动条控件ScrollBar)

    回顾上一节,讲的是组合框控件Combo Box的使用.本节详解滚动条控件Scroll Bar的相关内容. 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框和组合 ...

  2. VS2010/MFC编程入门之二十六(常用控件:滚动条控件Scroll Bar)

    回顾上一节,鸡啄米讲的是组合框控件Combo Box的使用.本节详解滚动条控件Scroll Bar的相关内容. 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框 ...

  3. VS2010-MFC(常用控件:滚动条控件Scroll Bar)

    转自:http://www.jizhuomi.com/software/191.html 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框和组合框设置了相应属性 ...

  4. WinForm控件开发总结目录

    WinForm控件开发总结(一)------开篇 WinForm控件开发总结(二)------使用和调试自定义控件 WinForm控件开发总结(三)------认识WinForm控件常用的Attrib ...

  5. 自己开发基于c#的垂直滚动条控件

    由于Visual Studio工具箱中自带滚动条控件外观太老,而且没有颜色外观属性可设置. 所以自己就试着开发一个垂直的滚动条,它可以用来控制TextBox的滚动. 由于代码比较多,源文件已经打包到网 ...

  6. JavaScript日历控件开发 C# 读取 appconfig文件配置数据库连接字符串,和配置文件 List<T>.ForEach 调用异步方法的意外 ef 增加或者更新的习惯思维 asp.net core导入excel 一个二级联动

    JavaScript日历控件开发   概述 在开篇之前,先附上日历的代码地址和演示地址,代码是本文要分析的代码,演示效果是本文要实现的效果代码地址:https://github.com/aspwebc ...

  7. 自己动手用Javascript写一个无刷新分页控件

    .NET技术交流群:337901356 ,欢迎您的加入! 对 于一个用户体验好的网站来说,无刷新技术是很重要的,无刷新,顾名思义,就是局部刷新数据,有用过Asp.net Web Form技术开发网页的 ...

  8. COM组件开发实践(八)---多线程ActiveX控件和自动调整ActiveX控件大小(下)

    源代码下载:MyActiveX20081229.rar 声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改 ...

  9. WP8.1开发中ListView控件加载图列表的简单使用(1)

    我也是刚接触WP编程没几个月,就是在这段时间一直闲着没事,然后又比较喜欢WP这款系统,就学习了WP这方面的开发言语,自学是很困难的,掌握这方面的资料不多,很初级,就是自己在网上找资料学习过程中,看到别 ...

随机推荐

  1. fiddler报错:creation of the root certificate was not successful 证书安装不成功

    fiddler提示:creation of the root certificate was not successful 证书安装不成功 首先 找到Tools——>Options 在弹出的菜单 ...

  2. PAT甲级——A1087 All Roads Lead to Rome【30】

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  3. STL与泛型编程-学习2-GeekBand

    9, 容器 Deque 双向队列 和vector类似, 新增加: push_front 在头部插入一个元素 pop_front 在头部弹出一个元素 Deque和vector内存管理不同: 大块分配内存 ...

  4. bootstrap-select 插件示例

      本文原创地址:http://www.cnblogs.com/landeanfen/p/7457283.html 一.组件开源地址以及API说明 bootstrap-select开源地址:https ...

  5. 跟我一起学koa之在koa中使用mongoose(四)

    第一步安装mongoose,创建数据库文件夹 第二步引入mongoose,连接数据库 第三步运行项目 这个报错 只需要将es6写法变成es5写法即可 我们连接数据库,并且以post请求的方式插入数据 ...

  6. hdu1693 Eat the Trees [插头DP经典例题]

    想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...

  7. 模板:KD-Tree

    KD-Tree,用来解决多维空间中的问题,其实就是优化暴力(逃 一般cdq能做的它都能做,而且...既然是优化暴力,那就学习一下了 对与几个n维点,我们将它每一维分割,建立一颗二叉树,方便我们搜索剪枝 ...

  8. Creating a bootable Ubuntu USB stick

    Windows: https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-windows#0 Ubuntu: http ...

  9. JavaSE_02_Thread类01

    1.1 并发与并行 并发:指两个或多个事件在同一个时间段内发生. 这在单 CPU 系统中,每一时刻只能有一道程序执行,即微观上这些程序是分时的交替运行,只不过是给人的感觉是同时运行,那是因为分时交替运 ...

  10. md5密码入库

    <?php //连接数据库 $pdo = new PDO('mysql:host=localhost;dbname=md5;charset=UTF8', 'root', ''); # 设置为fa ...