JS achieves banner image carousel effect (mouse event ),

Source: Internet
Author: User

JS achieves banner image carousel effect (mouse event ),

1. effects to be achieved

1. switch between the left and right clicks

2. Click the dot to switch the image

II.

Iii. Code

1. css

<style type="text/css">body,img,span,ul,li{margin: 0;padding: 0;}#div1{width: 600px;height: 350px;margin: 150px auto;border: 10px solid #eee;position: relative;}img{width: 600px;height: 350px;}span{display: inline-block;position: absolute;width: 50px;height: 50px;border-radius: 25px;background:rgba(0,0,0,.3); font-size: 30px;line-height: 50px;text-align: center;cursor: pointer;color: white;}span:hover{background:rgba(255,255,255,.5);color:black;transform:scale(1.3);}span#L{left: 10px;top: 150px;}span#R{right: 10px;top: 150px;}#div1 #ul{position: absolute;bottom: 10px;left: 250px;width: 125px;height: 20px;}#div1 #ul li{list-style: none;float: left;height: 20px;width: 20px;border-radius: 10px;background:rgba(255,255,255,.5); margin-right: 5px;cursor: pointer;}#div1 #ul .active{background:rgba(0,0,0,.5);}</style>

2. html

<body>  <div id="div1">        <span id="L"><</span>    <span id="R">></span>    <ul id="ul">     <li></li>     <li></li>     <li></li>     <li></li>     <li></li>    </ul>  </div></body>

3. js

<Script type = "text/javascript"> window. onload = function () {var div1 = document. getElementById ('div1 '); var oImg = div1.getElementsByTagName ('img') [0]; var spanL = document. getElementById ('l'); var spanR = document. getElementById ('R'); var oUl = document. getElementById ('ul '); var oLi = oUl. getElementsByTagName ('lil'); var arrImg = ['img2/1.jpg ', 'img2/2.jpg', 'img2/3.jpg ', 'img2/4.jpg ', 'img2/5.jpg ']; var num = 0; var oldLi = 0; function fn (num) {oImg. src = arrImg [num];} fn (0); function fnLi (num) {oLi [oldLi]. className = ''; oLi [num]. className = 'active'; oldLi = num;} fnLi (0);/* switch between left and right span images * // * switch between li and spanL when left and right spans are clicked */spanL. onclick = function () {if (num <1) {num = arrImg. length-1; fn (num); fnLi (num);} else {num --; fnLi (num); fn (num) ;}} spanR. onclick = function () {if (num = arrImg. length-1) {num = 0; fn (num); fnLi (num) ;}else {num ++; fnLi (num); fn (num );}} /* click li to implement image switching */for (var I = 0; I <oLi. length; I ++) {oLi [I]. index = I; oLi [I]. onclick = function () {fn (this. index); fnLi (this. index) ;}}</script>

Summary

The above is a small series of JS implementation banner image carousel effect (mouse event), I hope to help you, if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.