当前位置: 代码迷 >> Web前端 >> 自各儿写的,利用as3 实现 copy 功能
  详细解决方案

自各儿写的,利用as3 实现 copy 功能

热度:779   发布时间:2012-09-19 13:43:54.0
自己写的,利用as3 实现 copy 功能

?

自己写的,利用as3 实现 copy 功能

js 脚本:

?

var HushuilongClipboard = {
	index: 0,
	current_flash_id: '',
	clipboardText: {},
	completeCallback: {},
	init: function(flash_place_id, button_img, button_width, button_height){
		var flash_id = 'HushuilongClipboard_'  + (this.index++);
		this.current_flash_id = flash_id;
		var flashvars = {id:flash_id, button_img: button_img};
		var params = { swliveconnect:"true",wmode:"transparent"};
		var attributes = { id:flash_id, name:flash_id };
		var flash_width = parseInt(button_width,10) + 2;
		var flash_height = parseInt(button_height,10);
		//swfobject.embedSWF("/HushuilongClipboard/HushuilongClipboard.swf?cachetime="+new Date().getTime(), flash_place_id, flash_width, flash_height, "9.0.45.0", "expressInstall.swf",  flashvars, params, attributes);
		swfobject.embedSWF("/HushuilongClipboard/HushuilongClipboard.swf", flash_place_id, flash_width, flash_height, "9.0.45.0", "expressInstall.swf",  flashvars, params, attributes);				
		return this;
	},
	setText: function(str,completeCallback){
		var flash_id = this.current_flash_id;
		this.clipboardText[flash_id] = str;
		this.completeCallback[flash_id] = typeof completeCallback == 'function' ? completeCallback : function(){}; 
		return this;
	},
	thisMovie: function(movieName){ 
		return navigator.appName.indexOf("Microsoft") != -1 
				? window[movieName] 
				: document[movieName];
	},
	flashClickCallback: function(flash_id){
		if( ! this.clipboardText[flash_id]) return ;
		var text = this.clipboardText[flash_id];
		var res = this.thisMovie(flash_id).jsCallflashToSetClipboard(text);			
		this.completeCallback[flash_id](res);
	}
};

?

as3 脚本:

package {



	import flash.display.Stage;

	import flash.display.StageScaleMode;

	import flash.display.StageAlign;

	import flash.net.URLRequest;

	import flash.net.URLLoader;

	import flash.events.*;

	import flash.display.Loader;

	import flash.display.LoaderInfo;

	import flash.utils.ByteArray;

	import flash.display.Sprite;

	import flash.display.Bitmap;

	import flash.external.ExternalInterface;

	import flash.system.System;

	import flash.system.Security;	



	public class HushuilongClipboard extends Sprite {

		

		private var buttonContainer:Sprite;

		

		public function HushuilongClipboard() {

			flash.system.Security.allowDomain("*");

			stage.scaleMode = StageScaleMode.NO_SCALE;

			stage.align = StageAlign.TOP_LEFT;			

			

			buttonContainer = new Sprite();

			buttonContainer.buttonMode = true;

			buttonContainer.useHandCursor = true;

			addChild(buttonContainer);			

			

			load_button_img();

			

			buttonContainer.addEventListener(MouseEvent.CLICK, function(event:Event):void{

				ExternalInterface.call("HushuilongClipboard.flashClickCallback",ExternalInterface.objectID);

			});			

			ExternalInterface.addCallback("jsCallflashToSetClipboard", setClipboard);

		}

		

		public function load_button_img(){			

			try{

				var buttonImg:String = LoaderInfo( this.root.loaderInfo ).parameters.button_img;		

				var loader:Loader = new Loader();

				loader.load( new URLRequest(buttonImg) );

				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(evt:Event){

					var image:Bitmap = Bitmap(loader.content);

					buttonContainer.addChild(image);

				});

			}catch(error:Error){

				

			}

		}

		

		public function setClipboard(str) {

			try{

				System.setClipboard(str);

				return true;

			}catch(error:Error){

				return false;

			}

		}

	}

}

?

?

原理:

???? flash 加载图片按钮,点击按钮触发事件调用JS ,在Js 设置要复制的字符串, 再把设置好的字符串传给 as3

??? ,???? 利用 as3 中的 system.setClipboard 设置系统剪贴板,可跨多浏览器。

?

?

  相关解决方案