Scrollbar

这是一个滚动条类,例子中用到了一个资源在这里:http://files.cnblogs.com/yili16438/ScrollBar.zip

注:同样导入了TweenLite,传送门:http://www.greensock.com/tweenlite/

下面是源代码:

package com.easily.scrollbar
{
import com.greensock.TweenLite;

import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

public class Scrollbar
{
private static const PRECISION:Number = 0.5 ;

private var mListener:Object;
private var mBg:MovieClip;
private var mDragBar:MovieClip;
private var mUpBtn:SimpleButton;
private var mDownBtn:SimpleButton;
private var mContent:DisplayObject;
private var mMaskMc:MovieClip;
private var mDragRect:Rectangle;
private var mSpeed:Number;
private var mPart: int ;

public function Scrollbar(listener:Object, part: int = 10 , speed:Number = 0.2 )
{
mListener
= listener;

mSpeed
= speed;
mPart
= part;
mBg
= mListener.getBg();
mDragBar
= mListener.getDragBar();
mUpBtn
= mListener.getUpBtn();
mDownBtn
= mListener.getDownBtn();
mContent
= mListener.getContent();
mMaskMc
= mListener.getMaskMc();
mDragRect
= new Rectangle(mUpBtn.x, mUpBtn.y + mUpBtn.height, 0 , mBg.height - mUpBtn.height - mDownBtn.height - mDragBar.height);

mDragBar.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
mDragBar.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
mDragBar.addEventListener(MouseEvent.ROLL_OUT, onMouseUp);
mUpBtn.addEventListener(MouseEvent.CLICK, onMoveUp);
mDownBtn.addEventListener(MouseEvent.CLICK, onMoveDown);
}

public function setContent(content:DisplayObject): void
{
mContent
= content;
}

private function onMouseDown(e:MouseEvent): void
{
mDragBar.startDrag(
false , mDragRect);
}

private function onMouseUp(e:MouseEvent): void
{
mDragBar.stopDrag();
}

private function onMoveUp(e:MouseEvent): void
{
TweenLite.to(mDragBar, mSpeed
/ 2 , {y:Math.max(mDragRect.y, mDragBar.y - mDragRect.height / mPart)});
}

private function onMoveDown(e:MouseEvent): void
{
TweenLite.to(mDragBar, mSpeed
/ 2 , {y:Math.min(mDragBar.y + mDragRect.height / mPart, mDragRect.bottom)});
}

public function update(): void
{
var y:Number
= 0 ;

if (mDragBar.y != mDragRect.y)
{
var posPercent:Number
= (mDragBar.y - mDragRect.y) / mDragRect.height;
y
= mMaskMc.y - (mContent.height - mMaskMc.height) * posPercent;
}
else
{
y
= mMaskMc.y;
}

if (Math.abs(Math.abs(mContent.y) - Math.abs(y)) > PRECISION)
{
TweenLite.to(mContent, mSpeed, {y:y});
}
}

public function dispose(): void
{
mDragBar.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
mDragBar.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
mDragBar.removeEventListener(MouseEvent.ROLL_OUT, onMouseUp);
mUpBtn.removeEventListener(MouseEvent.CLICK, onMoveUp);
mDownBtn.removeEventListener(MouseEvent.CLICK, onMoveDown);

mListener
= null ;
mBg
= null ;
mDragBar
= null ;
mUpBtn
= null ;
mDownBtn
= null ;
mContent
= null ;
mMaskMc
= null ;
mDragRect
= null ;
}
}
}

下面是使用例子:

package test.scrollbar
{
import com.easily.scrollbar.Scrollbar;
import com.easily.view.ScrollbarMc;

import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;

public class ScrollbarTest extends Sprite
{
private var mScrollbar:Scrollbar;
private var mBody:MovieClip;

public function ScrollbarTest()
{
super();

mBody
= new ScrollbarMc();
mBody.content.mask
= mBody.maskMc;
addChild(mBody);

mScrollbar
= new Scrollbar( this );
addEventListener(Event.ENTER_FRAME, update);
}

private function update(e:Event): void
{
mScrollbar.update();
}

public function getDragBar():MovieClip
{
return mBody.dragBar;
}

public function getBg():MovieClip
{
return mBody.bg;
}

public function getUpBtn():SimpleButton
{
return mBody.up_btn;
}

public function getDownBtn():SimpleButton
{
return mBody.down_btn;
}

public function getContent():MovieClip
{
return mBody.content;
}

public function getMaskMc():MovieClip
{
return mBody.maskMc;
}
}
}

你可能感兴趣的:(scroll)