之前写过一个简单的教程,但有只是滚动条拖动的计算。。之后就有很多初学者一直pm来说textfield中的文字增加了,滚动条不会刷新。那么今天有空就拿回来改了一下,顺便弄成class的简单格式,当然有兴趣的朋友可以自己增加功能之后制作成自己的组件 :)
Java
Asp
PHP
.Net
XML
C/C++
CGI
VB
Jsp
J2ee
J2se
J2me
EJB
Servlet
Tomcat
Resin
Struts
Weblogic
Eclipse
ANT
GUI
JMS
Web servise
IDEA
Webphere
Hibernate
Spring
Jboss
Applet
Swing
Socket
Javamail
Perl
Ajax
P2P
安全
模式
框架
测试
开源
游戏
Windows XP
Windows 2000
Windows 2003
Windows Me
Windows 9.x
Linux
UNIX
注册表
操作系统
服务器
应用服务器
private var root:movieclip;
private var barobj:movieclip;
private var textobj:textfield;
private var text:string = "coding by super-tomato";
public var __width:number = 300;
public var __height:number = 200;
public var min:number;
public var max:number;
public var inc:number;
public function scrolltext(mc:movieclip, depth:number) {
var target:object = this;
root = mc.createemptymovieclip("__scrolltext", depth); //根据使用者提供的深度创建一个mc
createobj(); //建立文本和滚动条
barobj.onpress = function() { //滚动条按下进行拖动的动作
this.onenterframe = function() { //此循环是为了得到最新可拖动范围的值
this.startdrag(false, this._x, target.min, this._x, target.max); //设定拖动范围
target.textobj.scroll = int(this._y/target.inc); //根据滚动条的位置设定文字所滚动到的位置
};
}
barobj.onrelease = barobj.onreleaseoutside = function() { //放开滚动条的动作
this.stopdrag(); //停止拖动
delete this.onenterframe; //删除循环事件以减少计算量
}
setinterval(this, "refreshscroll", 100); //每100毫秒刷新一次
}
private function createobj():void {
root.createtextfield("__textarea",0,0,0,this.__width,this.__height); //创建文本
textobj = root.__textarea;
textobj.border = true; //显示文本的边框
textobj.text = this.text; //设定文本的文字
barobj = root.createemptymovieclip("__scrollbar",1); //建立滚动条
barobj._x = this.__width + 5; //设定滚动条的位置
with(barobj) { //画出滚动条
beginfill(0xff9900);
l.neto(20, 0);
l.neto(20, this.__height);
l.neto(0, this.__height);
l.neto(0,0);
endfill();
}
}
private function refreshscroll():void {
textobj.text = this.text; //每100毫秒设定一次文本的文字
var newheight:number = textobj._height / textobj.textheight; //计算文本的高度和文字高度的比例
if(newheight < 1) { //如果文字的高度大于文本所能够显示的高度
barobj._height = newheight * textobj._height; //更新滚动条的长度
} else { //文本内容少于显示范围
barobj._height = this.__height; //滚动条的长度就等于原来所设定的高度
}
this.min = textobj._y; //文本的顶点位置
this.max = this.min+(textobj._height - barobj._height); //滚动条所能拖动的最低范围
this.inc = int(math.abs(this.max-this.min)/textobj.maxscroll); //根据文本最大的滚动值计算滚动条能够拖动的距离
}
}
关键字 本文所属关键字
相关 与本文相关文章
分类 所有文章关键字导航
源码编程相关
操作系统/服务器相关
标准 网站致力的规范