Use JS to achieve carousel image effects (2). js

Source: Internet
Author: User

Use JS to achieve carousel image effects (2). js

On the basis of the Code (1) for implementing image carousel effect using JS in the previous article, the response event of the left and right arrows is added, and the left and right arrows can be clicked to scroll the image:

The js Code is as follows:

Window. onload = function () {// carousel initialization var lunbo = document. getElementById ('content'); var imgs = lunbo. getElementsByTagName ('img '); var uls = lunbo. getElementsByTagName ('ul '); var lis = lunbo. getElementsByTagName ('lil'); var next = document. getElementById ('Next'); var prev = document. getElementById ('prev'); var item = 0; // In the initial state, a circle is in the highlighted mode lis [0]. style. fontSize = '26px '; lis [0]. style. color = '# fff'; imgs [0]. style. Display = 'block'; // defines a global variable for automatic carousel Image Sequence changes var pic_index = 1; // automatic carousel. you can use clearInterval () to clear the recording at any time by using pic_time. Var pic_time = setInterval (autobofang, 1000); // function autobofang () {if (pic_index> = lis. length) {pic_index = 0;} picChange (pic_index); pic_index ++;} // Manual carousel for (var I = 0; I <lis. length; I ++) {lis [I]. addEventListener ("mouseover", change, false);} function change (event) {var event = event | window. event; var targetmediaevent.tar get | event. srcElement; var children = target. parentNode. children; for (var I = 0; I <children. length; I ++) {if (target = children [I]) {picChange (I) ;}}// function picChange (I) {// do not display all images. All circles are in the common style for (var j = 0; j 

: Move the cursor over the Arrow

When you click the arrow, the image will change, and the small circle below will show the highlighted effect of the corresponding image.

Summary:

The basic carousel effect has been achieved. What needs to be done later is to streamline the code, encapsulate the code, and improve the running efficiency.

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.