您可以创建自定义 HSlider 并监听 mouseMove 事件,我创建了简单的示例,您可以自己重构和优化它,请参见代码:
package classes
{
import flash.events.MouseEvent;
import flash.geom.Point;
import mx.controls.HSlider;
import mx.controls.sliderClasses.SliderDataTip;
import mx.core.mx_internal;
import mx.formatters.NumberFormatter;
use namespace mx_internal;
public class HSliderCustom extends HSlider
{
private var _rollOver:Boolean = false;
private var _thumbActivity:Boolean = false;
public function HSliderCustom()
{
super();
}
//
// Handlers
//
protected function onRollOutHandler(event:MouseEvent):void
{
_rollOver = false;
destroyMovieHandler();
if (!_thumbActivity)
{
if (mx_internal::dataTip)
{
systemManager.toolTipChildren.removeChild(mx_internal::dataTip);
mx_internal::dataTip = null;
}
}
}
protected function onRollOverHandler(event:MouseEvent):void
{
_rollOver = true;
addMovieHandler();
}
protected function onMouseMoveHandler(event:MouseEvent):void
{
var p:Point = new Point(event.localX, event.localY);
var p_global:Point = event.target.localToGlobal(p);
var p_content:Point = globalToContent(p_global);
onRollOverTooltip(p_content.x, p_content.y);
}
mx_internal function addMovieHandler():void
{
if (!hasEventListener(MouseEvent.MOUSE_MOVE))
addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler, false, 0, true);
}
mx_internal function destroyMovieHandler():void
{
if (hasEventListener(MouseEvent.MOUSE_MOVE))
{
removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
}
}
//
// Overriden methods
//
override protected function childrenCreated():void
{
super.childrenCreated();
addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
}
override mx_internal function onThumbMove(thumb:Object):void
{
destroyMovieHandler();
_thumbActivity = true;
super.mx_internal::onThumbMove( thumb );
}
override mx_internal function onThumbRelease(thumb:Object):void
{
super.mx_internal::onThumbRelease( thumb );
if (_rollOver)
{
addMovieHandler();
}
_thumbActivity = false;
}
//
// New methods
//
protected function onRollOverTooltip(localX:Number, localY:Number):void
{
if (showDataTip)
{
// Setup number formatter
var dataFormatter:NumberFormatter = new NumberFormatter();
dataFormatter.precision = getStyle("dataTipPrecision");
if (!mx_internal::dataTip)
{
mx_internal::dataTip = SliderDataTip(new sliderDataTipClass());
systemManager.toolTipChildren.addChild(mx_internal::dataTip);
var dataTipStyleName:String = getStyle("dataTipStyleName");
if (dataTipStyleName)
{
mx_internal::dataTip.styleName = dataTipStyleName;
}
}
var formattedVal:String;
if (dataTipFormatFunction != null)
{
formattedVal = this.dataTipFormatFunction(
mx_internal::getValueFromX(localX));
}
else
{
formattedVal = dataFormatter.format(mx_internal::getValueFromX(localX));
}
mx_internal::dataTip.text = formattedVal;
mx_internal::dataTip.validateNow();
mx_internal::dataTip.setActualSize(mx_internal::dataTip.getExplicitOrMeasuredWidth(),mx_internal::dataTip.getExplicitOrMeasuredHeight());
//mx_internal::positionDataTip(thumb);
}
mx_internal::keyInteraction = false;
/*
var event:SliderEvent = new SliderEvent(SliderEvent.THUMB_PRESS);
event.value = getValueFromX(thumb.xPosition);;
event.thumbIndex = thumb.thumbIndex;
dispatchEvent(event);
*/
var o:Point = new Point(localX, localY);
var r:Point = localToGlobal(o);
r = mx_internal::dataTip.parent.globalToLocal(r);
mx_internal::dataTip.x = r.x < 0 ? 0 : r.x - mx_internal::dataTip.width/2;
mx_internal::dataTip.y = y - mx_internal::dataTip.height;
}
}
}