Created
September 1, 2012 14:35
-
-
Save RhinoLu/3574954 to your computer and use it in GitHub Desktop.
AS3 TestField ScrollBar 文字欄位捲動軸
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
import flash.geom.Rectangle; | |
import flash.text.TextField; | |
/** | |
* @usage | |
* For TextField only | |
* | |
* @example | |
* linkage this class to a library symbol. (With four public sprites below.) | |
* var scroll:MyTextScroll = new MyTextScroll(); | |
* scroll.scrollTargetName = "someTextFieldName"; | |
*/ | |
public class MyTextScroll extends Sprite | |
{ | |
public var scroll_bar:Sprite; | |
public var scroll_up:Sprite; | |
public var scroll_down:Sprite; | |
public var scroll_bg:Sprite; | |
private var _scrollTargetName:String; | |
private var _txt:TextField; | |
private var topY:Number; // ------ scroll bar 最高點位置 | |
private var bottomY:Number; // --- scroll bar 最低點位置 | |
public function MyTextScroll() | |
{ | |
topY = scroll_up.y; | |
bottomY = scroll_down.y - scroll_bar.height; | |
scroll_bar.addEventListener(MouseEvent.MOUSE_DOWN, scrollDOWN); | |
addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage); | |
} | |
private function scrollDOWN(e:MouseEvent):void | |
{ | |
stage.addEventListener(MouseEvent.MOUSE_UP , scrollUP); | |
stage.addEventListener(MouseEvent.MOUSE_MOVE, onBarMove); | |
_txt.removeEventListener(Event.SCROLL, onTextScroll); | |
scroll_bar.startDrag(false, new Rectangle(scroll_bar.x, topY, 0, bottomY - topY)); | |
} | |
private function onBarMove(e:MouseEvent):void | |
{ | |
//trace("----- onBarMove -----"); | |
//trace("scrollV : " + _txt.scrollV); // ---------- 目前TextField可視區域最高的是第幾行 | |
//trace("bottomScrollV : " + _txt.bottomScrollV); // ---- 目前TextField可視區域最低的是第幾行 | |
//trace("maxScrollV : " + _txt.maxScrollV); // ------- 當卷到最下方時,TextField可視區域最高的是第幾行 | |
_txt.scrollV = ((scroll_bar.y - topY) / ((bottomY - topY) / (_txt.maxScrollV - 1))) + 1; | |
} | |
private function scrollUP(e:MouseEvent):void | |
{ | |
stage.removeEventListener(MouseEvent.MOUSE_UP , scrollUP); | |
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onBarMove); | |
_txt.addEventListener(Event.SCROLL, onTextScroll); | |
scroll_bar.stopDrag(); | |
} | |
private function onRemoveFromStage(e:Event):void | |
{ | |
removeEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage); | |
scroll_bar.removeEventListener(MouseEvent.MOUSE_DOWN , scrollDOWN); | |
stage.removeEventListener(MouseEvent.MOUSE_UP , scrollUP); | |
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onBarMove); | |
} | |
public function set scrollTargetName(value:String):void | |
{ | |
_scrollTargetName = value; | |
_txt = parent.getChildByName(value) as TextField; | |
_txt.addEventListener(Event.SCROLL, onTextScroll); | |
} | |
private function onTextScroll(e:Event):void | |
{ | |
//trace("----- onTextScroll -----"); | |
//trace("scrollV : " + _txt.scrollV); // ---------- 目前TextField可視區域最高的是第幾行 | |
//trace("bottomScrollV : " + _txt.bottomScrollV); // ---- 目前TextField可視區域最低的是第幾行 | |
//trace("maxScrollV : " + _txt.maxScrollV); // ------- 當卷到最下方時,TextField可視區域最高的是第幾行 | |
scroll_bar.y = topY + ((bottomY - topY) / (_txt.maxScrollV - 1)) * (_txt.scrollV - 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment