在Flash元素中使用鼠标滚轮时禁用文档滚动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Flash元素中使用鼠标滚轮时禁用文档滚动相关的知识,希望对你有一定的参考价值。

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.
  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. })()

以上是关于在Flash元素中使用鼠标滚轮时禁用文档滚动的主要内容,如果未能解决你的问题,请参考以下文章