3

当光标在我的 flex 应用程序上时,是否可以在我的网页上禁用鼠标滚轮滚动?

我的 flex 应用程序是一个允许用户使用鼠标滚轮放大和缩小的地图;但是,当我将 flex 应用程序放到网页上时,滚轮会导致页面滚动而不是放大和缩小...

编辑:

我在我的 flex 应用程序中添加了声音,它告诉我我的鼠标事件是正确的。我还向 javascript 添加了一个警报框,以便我知道正在调用 MyApp.initialize 函数,但鼠标滚轮仍在滚动网页而不是我的 flex 应用程序。这是我正在使用的代码,当我在我的 flex 应用程序之上时它没有锁定滚动条:

var bname;
var MyApp = {
   initialize : function() {  

      this.debugging = true;
      this.busyCount = 0;
      this._debug('initialize');
      bname = navigator.appName;
      //alert(bname + ‘ is browser’);
      document.getElementById('flashDiv').onload = this.start;
      if(window.addEventListener)/** DOMMouseScroll is for mozilla. */
      window.addEventListener('DOMMouseScroll', this.wheel, false);

      /** IE/Opera. */
      window.onmousewheel = document.onmousewheel = this.wheel;
      if (window.attachEvent) //IE exclusive method for binding an event
     window.attachEvent("onmousewheel", this.wheel);
      }
   , start : function() {
      window.document.network_map.focus();
      }
   , //caputer event and do nothing with it.
   wheel : function(event) {
      if(this.bname == "Netscape") {
         // alert(this.bname);
         if (event.detail)delta = 0;
         if (event.preventDefault) {
            //console.log(’prevent default exists’);
            event.preventDefault();
            event.returnValue = false;
            }
         }
      return false;
      }
   , _debug : function(msg) {
      if( this.debugging ) console.log(msg);
      }
   }; 

我一定是错过了什么!?

4

3 回答 3

4

这适用于 AS3 flex/flash。使用以下代码在 flex/flash swf 中允许鼠标滚轮控制。当鼠标光标位于 flex/flash swf 之外时,它将滚动浏览器。

package com.custom {

import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.external.ExternalInterface;

/**
 * MouseWheelTrap - Simultaneous browser/Flash mousewheel scroll issue work-around
 * @version 0.1
 * @author Liam O'Donnell
 * @usage Simply call the static method MouseWheelTrap.setup(stage)
 * @see http://www.spikything.com/blog/?s=mousewheeltrap for updates
 * This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
 * (c) 2009 spikything.com
 */

public class MouseWheelTrap {

    static private var _mouseWheelTrapped :Boolean;

    public static function setup(mainStage:Stage):void {

        mainStage.addEventListener(MouseEvent.ROLL_OVER, function():void{ 
            allowBrowserScroll(false); 
            }
        );

        //i added 'mx.core.FlexGlobals.topLevelApplication.'making it work better for flex. use 'stage' for flash   
        mainStage.addEventListener(MouseEvent.ROLL_OUT, function():void{ 
            allowBrowserScroll(true); 
            }
        );
    }

    private static function allowBrowserScroll(allow:Boolean):void
    {
        createMouseWheelTrap();
        if (ExternalInterface.available){
            ExternalInterface.call("allowBrowserScroll", allow);
        }
    }
    private static function createMouseWheelTrap():void
    {
        if (_mouseWheelTrapped) {return;} _mouseWheelTrapped = true; 
        if (ExternalInterface.available){
            ExternalInterface.call("eval", "var browserScrolling;function allowBrowserScroll(value){browserScrolling=value;}function handle(delta){if(!browserScrolling){return false;}return true;}function wheel(event){var delta=0;if(!event){event=window.event;}if(event.wheelDelta){delta=event.wheelDelta/120;}else if(event.detail){delta=-event.detail/3;}if(delta){handle(delta);}if(!browserScrolling){if(event.preventDefault){event.preventDefault();}event.returnValue=false;}}if(window.addEventListener){window.addEventListener('DOMMouseScroll',wheel,false);}window.onmousewheel=document.onmousewheel=wheel;allowBrowserScroll(true);");
        }
    }
  }
}

在您的主 Flash 文档“frame 1 or where ever”或您的主 flex mxml 文件中,放置以下内容:

import com.custom.MouseWheelTrap;
MouseWheelTrap.setup(stage);

您可以通过以下 URL 访问我偶然发现的网站: http ://www.spikything.com/blog/index.php/2009/11/27/stop-simultaneous-flash-browser-scrolling/

一个星期的工作终于在 5 分钟内解决了……一定要爱上编程!

于 2010-01-09T18:16:41.517 回答
1

答案是通过 externalInterface 调用来调用 justkevin 的禁用代码。在应用程序 mouseOver 上调用一个 javascript 函数来禁用鼠标滚轮,并在应用程序 mouseOut 上启用它。

于 2010-01-05T18:32:37.790 回答
0

您不能在 Flex/Flash 中执行此操作,但有一些方法可以使用 javascript 执行此操作。

您需要使用您的 Flash 应用程序在您的页面上放置一些 javascript。它可能不适用于所有浏览器。

于 2010-01-05T15:07:30.583 回答