网页FLASH动画禁止右键菜单的方法

作者:网络 来源:佚名 更新时间:2009-08-09 17:33:31 点击:

在做一个实验时遇到一个禁止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();
}
}
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
}
}
}

 

  1. }
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  0 && mx

支持的浏览器有:

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