tion %> 山东棋牌手机版
當前位置:首頁 > 建站知識

一個禁止flash右鍵的方法

更新時間:2009.06.22 瀏覽次數:

在做一個實驗時遇到一個禁止flash的右鍵功能的問題。在網上找了很多方法,要不就完全不行,要不就瀏覽器不兼容。Google了一下國外的情況,終于找到了一個不錯的方法。該方法用了javascript和as的結合,完美解決了問題。

以下是javascript部分代碼:

var RightClick = {  
    /**  
     *  Constructor  
     */   
    init: function () {  
        this.FlashObjectID = "customRightClick";  
        this.FlashContainerID = "flashcontent";  
        this.Cache = this.FlashObjectID;  
        if(window.addEventListener){  
             window.addEventListener("mousedown", this.onGeckoMouse(), true);  
        } else {  
            document.getElementById(this.FlashContainerID).onmouseup = function() { document.getElementById(RightClick.FlashContainerID).releaseCapture(); }  
            document.oncontextmenu = function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }}  
            document.getElementById(this.FlashContainerID).onmousedown = RightClick.onIEMouse;  
        }  
    },  
    /**  
     * GECKO / WEBKIT event overkill  
     * @param {Object} eventObject  
     */  
    killEvents: function(eventObject) {  
        if(eventObject) {  
            if (eventObject.stopPropagation) eventObject.stopPropagation();  
            if (eventObject.preventDefault) eventObject.preventDefault();  
            if (eventObject.preventCapture) eventObject.preventCapture();  
         if (eventObject.preventBubble) eventObject.preventBubble();  
        }  
    },  
    /**  
     * GECKO / WEBKIT call right click  
     * @param {Object} ev  
     */  
    onGeckoMouse: function(ev) {  
        return function(ev) {  
        if (ev.button != 0) {  
            RightClick.killEvents(ev);  
            if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {  
                RightClick.call();  
            }  
            RightClick.Cache = ev.target.id;  
        }  
      }  
    },  
    /**  
     * IE call right click  
     * @param {Object} ev  
     */  
    onIEMouse: function() {  
        if (event.button> 1) {  
            if(window.event.srcElement.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {  
                RightClick.call();   
            }  
            document.getElementById(RightClick.FlashContainerID).setCapture();  
            if(window.event.srcElement.id)  
            RightClick.Cache = window.event.srcElement.id;  
        }  
    },  
    /**  
     * Main call to Flash External Interface  
     */  
    call: function() {  
        document.getElementById(this.FlashObjectID).rightClick();  
    }  
}


以下是as部分代碼:

package {  
      
    import flash.display.*;  
    import flash.external.ExternalInterface;  
   
    public classclass RightClick extends Sprite  
    {  
          
        public function RightClick()  
        {  
            stage.scaleMode = StageScaleMode.NO_SCALE;  
            stage.align = StageAlign.TOP_LEFT;  
              
            var methodName:String = "rightClick";  
            var method:Function = onRightClick;  
            ExternalInterface.addCallback(methodName, method);  
        }  
          
        private function onRightClick():void {  
   
            var mx:int = stage.mouseX;  
            var my:int = stage.mouseY;  
   
            if(my> 0 && my <stage.stageHeight && mx> 0 && mx <stage.stageWidth) {  
                // YOUR CODE HERE  
            }  
        }  
    }  
}


支持的瀏覽器有:

WINDOWS VISTA

Internet Explorer 7.0.6001 (16549)
Firefox 2.0.0.6 (with mouse gestures disabled)
Maxthon 2 (with mouse gestures disabled)
Safari 3.0.3 (522.15.5)

Windows XP SP2

Internet Explorer 6
Internet Explorer 7
Maxthon 2 (with mouse gestures disabled)
FireFox 2 (with mouse gestures disabled)
Safari 3
Netscape 8

Mac OSX 10.4.10 (Intel)

Firefox 2
Safari 3.0.3
畢竟是國外的產品,似乎對某些國產瀏覽器支持不好,擁有最多用戶的maxthon瀏覽器如果開啟鼠標手勢就不被支持。解決方法還在嘗試中。

項目地址:http://code.google.com/p/custom-context-menu/

Demo可以到我的首頁查看:http://niuniuzhu.cn (暫時失效)
作者Demo地址:
http://www.uza.lt/rightclick/

烟台麻将规则 幸运飞艇下载 亲友湖南麻将作弊器 台球直播比分直播 福建31选7开奖结 球探比分直播捷报 棒球比分雪缘园 福建31选7 白银怎么炒 杭州绿城足球直播 微信大唐河北麻将群 500万彩票比分网 陕西十一选五任五遗 球探网足球篮球即时比分网 鼎牛配资 青海11选五彩票 国标麻将单机