2

I need to reposition the slider on a scrollbar programatically (not move the scrollbar, just the slider; i.e. reset it to top or left, etc.). Anybody figured this out?

4

1 回答 1

3

我本来要提到程序化蒙皮,但再想一想,听起来你想要做的只是设置组件的滚动位置。那正确吗?如果是这样,这取决于您正在使用哪种控件,以及您想要执行哪种滚动(例如,到列表中的项目,或者只是到水平或垂直位置):

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

    <mx:Script>
        <![CDATA[

            private function scrollToIndex():void
            {
                ls.scrollToIndex(15);
            }

            private function scrollToPoint():void
            {
                cv.verticalScrollPosition += 20;
            }

        ]]>
    </mx:Script>

    <mx:VBox width="300">
        <mx:List id="ls" width="100%" rowCount="5">
            <mx:dataProvider>
                <mx:Array>
                    <mx:String>Item 1</mx:String>
                    <mx:String>Item 2</mx:String>
                    <mx:String>Item 3</mx:String>
                    <mx:String>Item 4</mx:String>
                    <mx:String>Item 5</mx:String>
                    <mx:String>Item 6</mx:String>
                    <mx:String>Item 7</mx:String>
                    <mx:String>Item 8</mx:String>
                    <mx:String>Item 9</mx:String>
                    <mx:String>Item 10</mx:String>
                    <mx:String>Item 11</mx:String>
                    <mx:String>Item 12</mx:String>
                    <mx:String>Item 13</mx:String>
                    <mx:String>Item 14</mx:String>
                    <mx:String>Item 15</mx:String>
                    <mx:String>Item 16</mx:String>
                    <mx:String>Item 17</mx:String>
                    <mx:String>Item 18</mx:String>
                    <mx:String>Item 19</mx:String>
                    <mx:String>Item 20</mx:String>
                </mx:Array>
            </mx:dataProvider>
        </mx:List>
        <mx:Button label="Scroll" click="scrollToIndex()" />
        <mx:Canvas id="cv" width="100%" height="200">
            <mx:TextArea width="100%" height="600" verticalScrollPolicy="off">
                <mx:text>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed 
                    do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
                    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
                    nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
                    reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
                    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in 
                    culpa qui officia deserunt mollit anim id est laborum.

                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed 
                    do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
                    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
                    nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
                    reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
                    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in 
                    culpa qui officia deserunt mollit anim id est laborum.

                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed 
                    do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
                    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
                    nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
                    reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
                    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in 
                    culpa qui officia deserunt mollit anim id est laborum.

                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed 
                    do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
                    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
                    nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
                    reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
                    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in 
                    culpa qui officia deserunt mollit anim id est laborum.              
                </mx:text>
            </mx:TextArea>
        </mx:Canvas>
        <mx:Button label="Scroll" click="scrollToPoint()" />
    </mx:VBox>  

</mx:Application>

verticalScrollPosition 和 Horizo​​ntalScrollPosition 属性(可用于 Container 类的所有实现)和 scrollToIndex 方法(用于列表)可能是您正在寻找的。这个例子说明了两者。希望能帮助到你!

于 2009-01-22T05:26:50.347 回答