ActionScript 3: maintaining textarea UIscrollbar position on loss of focus in flash embed

这一生的挚爱 提交于 2020-01-07 04:38:38

问题


I'm using Flash CS4. Everything functions as it should when CS4 previews the swf after compiling it.

However, after embedding the flash item in a webpage, if the textArea loses focus within the flash piece, the textarea's built-in UIscrollbar resets to the very top.

Here's the kicker: if I add a FOCUS_OUT event listener to the textArea to store the current scrollbar value, I find that the scrollbar value has been reset to minimum even before the FOCUS_OUT event is triggered! WTF?

I think this is occurring because the textArea's htmlText propery is dynamically populated. Adobe AIR has advanced methods for handing HTML, but not simple AS3, oh no. How obnoxious. What can be done?


回答1:


I never thought I'd answer my own question, but here it is. Turns out the htmlText thing may have been a canard. The scrollbar jitter happens in between the dynamically generated content window's being clicked and its losing focus, so this captures the current position and whether the scrollbar's at the bottom on the click event and passes that info to the focus event. displayWindow is the one with dynamically generated content.

I am relatively new to AS3, so let me know if any of this isn't kosher.

displayWindow.addEventListener(MouseEvent.ROLL_OUT, handleClick);
function handleClick(event:MouseEvent):void
{
    //here, user has clicked output window
    var currentPosition = displayWindow.verticalScrollPosition;
    var atTheBottom:Boolean = (currentPosition == displayWindow.maxVerticalScrollPosition);
   var focusAdded:Boolean = false;
   displayWindow.addEventListener(FocusEvent.FOCUS_OUT, 
   function handy() {
        //here, user has clicked away from output window  

       if (!focusAdded) {
            if (atTheBottom)
                displayWindow.verticalScrollPosition = displayWindow.maxVerticalScrollPosition;
            else
                displayWindow.verticalScrollPosition = currentPosition;

            focusAdded = true;

        } else {
            displayWindow.removeEventListener(FocusEvent.FOCUS_OUT, handy);
            focusAdded = false;
        }
    }
   );
}


来源:https://stackoverflow.com/questions/3964288/actionscript-3-maintaining-textarea-uiscrollbar-position-on-loss-of-focus-in-fl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!