[JavaScript-implemented Image Viewer] similar to lightbox

Source: Internet
Author: User

I always think Javascript is a good thing and want to learn it well. Of course, I also had to learn something. After nearly a month, I finally got something to write.

Today, I have made an image viewer using JavaScript.

First look:

First, we will introduce the following functions:

1. Click the thumbnail to display the larger image. The larger image will cover the page.

2. The local image can be displayed. You only need to select an image to obtain the image address.

3. Add images dynamically. You can also click the image to display the larger image.

4. A close button is attached to the displayed big image. You can click the big image or close button to close the big image.

Specific implementation of each function:

(1) first set a fixed Div layer to hide. The DIV layer has an image label and the property SRC is null. Set the src attribute to the address of the thumbnail and set the DIV to visible on the click on the thumbnail event.

(2) Add a file tag and start the timer in The onclick method. After a local image is selected, the image address will appear in the text box in the file tag, at this time, the timer copies the address to the DIV layer displayed in the big graph,

Set the DIV layer to visible.

(3) adding images dynamically means adding a tag and setting attributes. However, it is a little complicated to bind events.

There are different binding methods based on the browser:

 

If (element. addeventlistener) {// Mozilla, Netscape, Firefox element. addeventlistener ('click', play, false);} else if (element. attachevent) {// ie element. attachevent ("onclick", play );}

Play is the method name.

(4) In order to have a better user experience, add a close button next to the big picture. In fact, a button label is added to the image tag, and a method is bound to it. Of course, The onclick event on the Image

The disabled method is also bound to the user experience.

Various technologies used:

I. Timer operation:

Set the timer: setinterval (getpic, 500). getpic indicates the function name, and 500 indicates that the timer runs every 0.5 seconds.

Clear Timer: window. clearinterval (interval);, when the timer is cleared, the input parameter is the timer number: var interval = setinterval (getpic, 500); a global variable needs to be defined here.

II. DOM technology: Tag operations.

Obtain the tag: Document. getelementbyid ("fixpic"). The input parameter is the tag ID.

Modify the attributes of a tag: Document. getelementbyid ("fixpic"). setattribute ("src", URL );

Of course, this image viewer can be expanded a lot, and interested readers can implement it as needed. The difficulty is how to implement it. Check whether your brain is flexible and whether there is a good method.

In the timer, there are some difficult points to control. For example, when should you disable the timer after it is enabled? When can I turn it on again?

If you have any questions about these logics, you are welcome to discuss them with me.

Almost forgot the source code: http://files.cnblogs.com/Zeech-Lee/javascript.rar

Related Article

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.