Disable document scrolling when using mouse-wheel in Flash elements


/ Published in: JavaScript
Save to your folder(s)

Often in Flash, the mouse is used to zoom or scroll, and by default the document tag will also pick this up, making the page scroll away - which is annoying when you just want to view the Flash element.

This snippet intercepts mouse scroll over object or embed tags, and prevents the default behaviour.

The code is cross-browser, not dependent on a 3rd party library, and executes inside a self-executing function, so it won't pollute your global scope.


Copy this code and paste it in your HTML
  1. (function()
  2. {
  3. function addEventListener(event, elem, func)
  4. {
  5. if (elem.addEventListener)
  6. {
  7. return elem.addEventListener(event, func, false);
  8. }
  9. else if (elem.attachEvent)
  10. {
  11. return elem.attachEvent("on" + event, func);
  12. }
  13. }
  14.  
  15. function disableMouseWheel (event)
  16. {
  17. if(event.target.nodeName.toLowerCase().match(/embed|object/))
  18. {
  19. event.preventDefault();
  20. event.stopImmediatePropagation();
  21. return false;
  22. }
  23. }
  24.  
  25. var events = ['DOMMouseScroll', 'mousewheel'];
  26. for(var i = 0; i < events.length; i++)
  27. {
  28. addEventListener(events[i], document.body, disableMouseWheel);
  29. }
  30. })()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.