常做FLASH 网站,滑动条是最经常用到的小东西了.虽然不起眼,但是如何更加方便的加入到现有的程序中,是需要我们规划的了.
因此,我按照自己的习惯写了这个小滑动条,去除了基本无用的上下箭头...
使用方法:首先在库里放入滚动条的文件夹,在需要滚动条的地方加入如下的代码就可以了.
this.attachMovie("scrollmc", "scrollmc", this.getNextHighestDepth(), {_x:mc._x+mc._width, _y:mc._y, showHeight:msk._height, myHeight:msk._height, speed:0.3, targetMovie:mc,tension:false});
文件夹内包括:bar:MovieClip line:MovieClip Scrollbar:MovieClip(由bar和line组成,网站中使用的时候就是他)
整个代码如下:
/*
版本信息:智能滑动条1.0版
作者:刘毅[E-MAIL:luckliuyi@163.com QQ:14238910 技术讨论QQ群:1934054新的域名:
www.ourbrander.com,老域名(4belief.com)不要了]
使用者请保留此段信息,只为激励作者。
*/
/*使用例子:this.attachMovie("scrollmc", "scrollmc", this.getNextHighestDepth(), {_x:402, _y:0, showHeight:msk._height, myHeight:msk._height, speed:0.3, targetMovie:mc,tension:false});
myHeight:自己的高度
targetMovie:目标
showHeight:目标内容展示的高度
speed:目标内容滑动的速度
line:滑动条的背景
dragMC:拖动的按钮
tension:拖动条是否可以拉伸
*/
var scroll_id = 0;
//程序运行的标签
var isdrag = false;
//是否正在拖动
var first_time = true;
//是否刚初始化
var first_y;
//目标初始位置Y轴
var first_x;
//目标初始位置X轴(升级版时可用)
//=============函数定义=================================
function target_go() {
trace(targetMovie._y);
dix = (targetMovie._height-showHeight)*(dragMC._y)/(myHeight-dragMC._height)-first_y;
trace(myHeight+"/"+showHeight);
targetMovie._y += (-dix-targetMovie._y)*speed;
if (targetMovie._y>-dix-1 && targetMovie._y<-dix+1 && isdrag == false) {
clearInterval(scroll_id);
targetMovie._y = -dix;
scroll_id = 0;
}
}
//===================运行==========================
dragMC.onPress = function() {
isdrag = true;
this.startDrag(false, 0, 0, 0, myHeight-this._height);
if (scroll_id<1) {
//clearInterval(scroll_id);
scroll_id = setInterval(target_go, 50);
}
};
dragMC.onRelease = dragMC.onReleaseOutside=function () {
this.stopDrag();
isdrag = false;
//clearInterval(scroll_id);
if (scroll_id<1) {
//clearInterval(scroll_id);
scroll_id = setInterval(target_go, 50);
}
};
dragMC.onRollOver = function() {
this.gotoAndPlay(2);
};
dragMC.onRollOut = function() {
this.gotoAndPlay(11);
};
//=============================================
line._height = myHeight;
if (tension == true) {
//如果选择了滑动条拉伸
tmp_heigt = dragMC._height;
dragMC._height = (myHeight/targetMovie._height)*myHeight;
trace(dragMC._height);
} else {
//没有选择滑动条拉伸
dragMC._height = dragMC._height;
}
if (first_time == true) {
//获得目标初始坐标
first_y = targetMovie._y;
first_x = targetMovie._x;
}
//代码完======================================
至于如何使用,可以在载我上传的例子.
对了,有朋友说我为什么要选用setInterVal 我认为 存在的就是合理的,不好管理并不代表不好使用,不能使用.onEnterFrame是好管理,可是却受到了动画运行速度的限制.滑动条是有非常广泛的使用范围的,我不想因为某一个动画的运行帧速设置过低而使得滑动效果不平滑.