hi friends,
how to get mouse scroll event in flash for scrolling?

Dani AI

Generated

asked how to catch the mouse scroll for scrolling; as noted the right approach depends on which ActionScript model you target. Below are compact, practical options: a native listener for older AS2 code, the AS3 event for newer projects, and a JavaScript fallback when the browser/plugin doesn't forward wheel events.

AS2 (native listener)

var wheelListener:Object = new Object();
wheelListener.onMouseWheel = function(delta:Number) {
    // delta > 0 = wheel up, < 0 = wheel down
    content._y -= delta * 8; // tweak multiplier
    // clamp values to avoid over-scrolling
    if (content._y > 0) content._y = 0;
    if (content._y < -maxScroll) content._y = -maxScroll;
};
Mouse.addListener(wheelListener);

AS3 (native event)

stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
function onMouseWheel(e:MouseEvent):void {
    var delta:int = e.delta; // positive = wheel up
    content.y -= delta * 8;
    content.y = Math.max(Math.min(content.y, 0), -maxScroll);
}

Browser fallback (use when the plugin/browser drops wheel events)

function normalizeWheel(e) {
  e = e || window.event;
  if (e.wheelDelta) return e.wheelDelta/120;
  if (e.detail) return -e.detail/3;
  if (e.deltaY) return -Math.sign(e.deltaY);
  return 0;
}
function wheelHandler(e) {
  var d = normalizeWheel(e);
  var swf = document.getElementById('mySwf');
  if (swf && typeof swf.externalWheel === 'function') swf.externalWheel(d);
  e.preventDefault && e.preventDefault();
}
document.addEventListener('wheel', wheelHandler, false);
document.addEventListener('mousewheel', wheelHandler, false);
document.addEventListener('DOMMouseScroll', wheelHandler, false);

Troubleshooting notes:

  • Ensure the SWF has focus in the page (click it) — otherwise the browser may handle scrolling.
  • Embedding params matter: wmode=transparent/opaque can break focus/interaction; test without it.
  • If using JS-to-Flash calls, expose a callable Flash method (ExternalInterface) and set allowScriptAccess appropriately.
  • Normalize the delta and apply easing/tweening to keep scrolling smooth.

Recommended Answers

All 3 Replies

Which version of actionScript are you using? There is quite a difference between 3.0 and 2.0. Flash CS3 offers hte flexibility of both, Flash 8 is actionscript 2.0.

i am using macromedia flash 8.0, action script 2.0.

Depending on what you're scrolling and your overall desired effect, the code will be slightly different. For example a photo/slide show or a scrolling text field, or a looping graphic in a banner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.