How to Use the replication function implemented by zero clipboard JS + SWF

Source: Internet
Author: User

Replication is often used in development. It is relatively simple to implement in IE. However, it is difficult to achieve cross-browser. This article describes a cross-browser Library Class zero clipboard. It uses flash for copying, so as long as the browser is installed with flash, it can run, and more flexible than the document.exe ccommand ("copy") of IE.

Implementation principle of zero clipboard
Zero clipboard uses flash for copying. Previously there was a clipboard copy solution, which used a hidden flash. However, the latest Flash Player 10 allows you to start the clipboard only by performing operations on the flash. Therefore, zero clipboard has been improved, and a transparent flash is used to float the button. In this way, the flash copy function can be used instead of the button but flash.

How to use zero clipboard
Download the zero clipboard and decompress it. Two files are required: zeroclipboard. js and zeroclipboard.swf. Put these two files into your project.


Zero clipboard open-source JavaScript + flash Copy Library Class

Demo address:
Http://demo.jb51.net/js/ZeroClipboard/index.html

Core functions
Step 1: import the zeroclipboard. js file:

<SCRIPT type = "text/JavaScript" src = "zeroclipboard. js"> </SCRIPT>
Set the path of the zeroclipboard.swf file:

Zeroclipboard. setmoviepath ("zeroclipboard.swf ");
Note: The paths of zeroclipboard. js and zeroclipboard.swf files must be replaced with those of the corresponding files in your project. Or an absolute path.

Then we use:
CopyCodeCode: var clip = new zeroclipboard. Client (); // create an object
Clip. sethandcursor (true); // set the mouse to the Hand Type
Clip. settext ("Haha"); // set the text to be copied.
// Register a button with the parameter ID. Click this button to copy it.
// This button is not necessarily an input button or other DOM elements.
Clip. Glue ("copy-botton"); // cannot be exchanged with the previous position

In this way, the basic functions are implemented, and you can click the button to copy the set text. You may have noticed that the text to be copied is fixed. If you want to change it dynamically, for example, copying the content in an input box. Don't worry, as we will talk about below.

Other functions
Zero clipboard also provides some other functions, some of which are very useful.

reposition () method
because there is a flash button floating on the button, the flash button may be misplaced when the page size changes. Zero clipboard provides a reposition () method to recalculate the position of the flash button. We can bind it to the resize event. copy Code the code is as follows: BIND (window, "resize", function () {
clip. reposition ();
});

BIND is a cross-browser event binding function.
========================================================== ========================
Everyone knows this. The event binding function of IE is attachevent, while Firefox and Safari are addeventlistener, and opera supports both. Encapsulation.Copy codeThe Code is as follows :/************************************
* Add event binding
* @ Param OBJ: element of the event to be bound
* @ Param type: event name. "On" is not added. For example, "click" instead of "onclick ".
* @ Param FN: event processing function
************************************/
Function BIND (OBJ, type, FN ){
If (obj. attachevent ){
OBJ ['E' + Type + FN] = FN;
OBJ [type + FN] = function () {OBJ ['E' + Type + FN] (window. Event );}
OBJ. attachevent ('on' + type, OBJ [type + FN]);
} Else
OBJ. addeventlistener (type, FN, false );
}

For example, add a page Click Event:Copy codeThe Code is as follows: BIND (document, "click", function (){
Alert ("Hello, world !! ");
});

========================================================== ========================
Hide () and show () Methods
These two methods can hide and display the flash button. The show () method calls the reposition () method.

Setcsseffects () method
When you move the mouse over a button or click it, the pseudo classes such as CSS ": hover" and ": active" may become invalid due to the occlusion of the flash button. The setcsseffects () method solves this problem. First, we need to change the pseudo class to a class, for example:Copy codeThe Code is as follows: # copy-Botton: hover {
Border-color: # ff6633;
}
// You can change it to ": hover" to ". Hover"
# Copy-botton.hover {
Border-color: # ff6633;
}

We can call clip. setcsseffects (true); in this way, the zero clipboard will automatically process for us: Treat class. hover as a pseudo class: hover.

Gethtml () method
If you want to create a flash instance by yourself without the zero clipboard attachment method, this method can help you. It accepts two parameters: the width and height of the flash. The HTML code corresponding to flash is returned. For example:

VaR html = clip. gethtml (150, 20 );
You can use innerhtml or directly document. Write (); for output.
The following is the output HTML code:

<Embed id = "zeroclipboardmovie_1" src = "zeroclipboard/zeroclipboard.swf" loop = "false" menu = "false" Quality = "best" bgcolor = "# ffffff" width = "150" height = "20" name = "zeroclipboardmovie_1" align = "Middle" allowScriptAccess = "always" allowfullscreen = "false" type = "application/X-Shockwave-flash" pluginspage = "http://www.macromedia.com/go/getflashplayer "flashvars =" id = 1 & width = 150 & Height = 20 "wmode =" Transparent "/>
There is a bug in the Flash JavaScript Communication Interface of IE. You must insert an object tag to an existing Dom element. Before writing data to innerhtml, make sure that the element has been inserted into the DOM using the appendchild method.

Zero clipboard event handling
Zero clipboard provides some events that you can customize functions to process. The zero clipboard event processing function is addeventlistener (). For example, when Flash is fully loaded, an event "LOAD" is triggered ".

Clip. addeventlistener ("LOAD", function (client ){
Alert ("Flash loaded! ");
});
Zero clipboard will pass the clip object as a parameter. That is, the "client" in the above example ".
"Load" can also be written as "onLoad", and other events can also be written as follows.

Other events include:

Mouseover
Mouseout mouse removal event
Mousedown
Mouseup mouse release event
Complete replication success event
Among them, Mouseover events and complete events are commonly used.
As mentioned above, if you need to dynamically change the content to be copied, The Mouseover event can be used. For example, if you need to dynamically copy a value in the input box with the ID of test, you can reset the value when the mouse is over.

Clip. addeventlistener ("Mouseover", function (client ){
VaR test = Document. getelementbyid ("test ");
Client. settext (test. Value); // reset the value to be copied
});
Copied successfully:

Clip. addeventlistener ("complete", function (){
Alert ("Copied successfully! ");
});
Now, let's introduce it here. Try it on your own.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.