滚动条算法

package {
        import flash.display.Sprite;
        import flash.events.MouseEvent;
        import flash.geom.Rectangle;
        import flash.text.TextField;
        import flash.text.TextFieldAutoSize;

        public class GundongTiao extends Sprite
        {
                private var dragBar:DragBar;
                private var dragTrack:DragTrack;
                
                private var tf:TextField;
                
                private var content:TextField;
                
                private var tfMask:Sprite;
                
                private var value:Number;
                
                public function GundongTiao()
                {
                        
                        
                        tf = new TextField();
                        tf.width = 100;
                        tf.height = 20;
                        tf.border = true;
                        tf.background = true;
                        tf.x = 50;
                        tf.y = 10;
                        tf.text = "0";
                        addChild(tf);
                        
                        content = new TextField();
                        content.width = 300;
                        content.height = 250;
                        content.wordWrap = true;
                        content.autoSize= TextFieldAutoSize.LEFT;
                        content.border = true;
                        content.background = true;
                        content.x = 50;
                        content.y = 50;
                        content.text = "asdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfnasdaf\ndsafsdfasd\nfasdfasdfasd\nfasdfasdfasdf\nasdafdsafs\ndfasdfas\ndfasdf\nasdfasdfasdfasdfn";
                        addChild(content);
                        
                        tfMask = new Sprite();
                        tfMask.graphics.beginFill(0xffffff);
                        tfMask.graphics.drawRect(0,0,320,250);
                        tfMask.graphics.endFill();
                        tfMask.x = 50;
                        tfMask.y = 50;
                        addChild(tfMask);
                        
                        content.mask = tfMask;
                        
                        dragTrack = new DragTrack();
                        dragTrack.x = 450;
                        dragTrack.y = 50;
                        addChild(dragTrack);
                        
                        dragBar = new DragBar();
                        dragBar.x = 450;
                        dragBar.y = 50;
                        addChild(dragBar);
                        
                        addListener();
                }
                
                private function addListener():void
                {
                        dragBar.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownListener);
                        stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpListener);
                }
                
                private function mouseDownListener(event:MouseEvent):void
                {
                        var rec:Rectangle = new Rectangle(dragTrack.x,dragTrack.y,0,dragTrack.height - dragBar.height);
                        dragBar.startDrag(false,rec);
                        
                        stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveListener);
                }
                
                private function mouseMoveListener(event:MouseEvent):void
                {
                        value = Math.round((dragBar.y - dragTrack.y)/(dragTrack.height - dragBar.height)*100);
                        
                        updateText();
                        
                        updateContent();
                }
                
                private function mouseUpListener(event:MouseEvent):void
                {
                        dragBar.stopDrag();
                        
                        stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveListener);
                }
                
                private function updateText():void
                {
                        tf.text = String(value);
                }
                
                private function updateContent():void
                {
                        content.y = 50 - (value/100*(content.height-250));
                }
        }
}




package 
{
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.geom.Rectangle;
        import gs.TweenLite;
        /**
         * @author FLASH023
         * @date 2010-8-27 12:22
        **/
        public class ScrollText extends Sprite  
        {
                public function ScrollText():void {
                        stage?init(null):addEventListener (Event.ADDED_TO_STAGE, init);
                }
                public function init(e:Event ):void {
                                con_mc.mask=mask_mc;
                                con_mc.x = mask_mc.x;
                                bar_mc.x = line_mc.x= mask_mc.x + mask_mc.width;
                                bar_mc.y = line_mc.y = con_mc.y=mask_mc.y;
                                //
                                /*con_mc._txt.autoSize = "left";
                                con_mc._txt.multiline =true;
                                con_mc._txt.wordWrap =true;
                                con_mc._txt.width = mask_mc.width;
                                setText(con_mc._txt.htmlText);*/
                                //
                                setText(null);
                                bar_mc.buttonMode = true;
                                bar_mc.addEventListener (MouseEvent.MOUSE_DOWN, mouseDown);
                                //
                                removeEventListener (Event.ADDED_TO_STAGE, init);
                }
                private function mouseDown(e:MouseEvent ):void {
                        bar_mc.startDrag(false,new Rectangle(line_mc.x,line_mc.y,0,line_mc.height-bar_mc.height) );
                        stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
                        stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
                }
                
                private function mouseUp(e:MouseEvent):void 
                {
                        stopDrag();
                        stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
                        stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
                }
                
                private function mouseMove(e:MouseEvent):void 
                {
                        var _py:Number =(bar_mc.y-line_mc.y)/(line_mc.height-bar_mc.height) *(mask_mc.height -con_mc.height );
                        TweenLite.to(con_mc,.3,{y:mask_mc.y+_py});
                }
                private function setText(_str:String ) {
                                //con_mc._txt.htmlText = _str;
                                bar_mc.y = con_mc.y=mask_mc.y;
                                //
                                bar_mc.visible = line_mc.visible = con_mc.height > mask_mc.height;
                                //bar_mc.height = mask_mc.height / con_mc.height * line_mc.height;
                        }
        }
        
}

你可能感兴趣的:(算法,Flash,UP)