Home Web Front-end JS Tutorial The simplest JavaScript image carousel code (two methods)_javascript skills

The simplest JavaScript image carousel code (two methods)_javascript skills

May 16, 2016 pm 03:24 PM

By changing the opacity attribute of each image:

Material picture:

Code 1:

<!DOCTYPE html>
 <html lang="en">
 <head>
  <meta charset="UTF-">
  <title>最简单的轮播广告</title>
  <style>
   body, div, ul, li {
    margin: ;
    padding: ;
   }
   ul {
    list-style-type: none;
   }
   body {
    background: #;
    text-align: center;
    font: px/px Arial;
   }
   #box {
    position: relative;
    width: px;
    height: px;
    background: #fff;
    border-radius: px;
    border: px solid #fff;
    margin: px auto;
   }
   #box .list {
    position: relative;
    width: px;
    height: px;
    overflow: hidden;
    border: px solid #ccc;
   }
   #box .list li {
    position: absolute;
    top: ;
    left: ;
    width: px;
    height: px;
    opacity: ;
    transition: opacity .s linear
   }
   #box .list li.current {
    opacity: ;
   }
   #box .count {
    position: absolute;
    right: ;
    bottom: px;
   }
   #box .count li {
    color: #fff;
    float: left;
    width: px;
    height: px;
    cursor: pointer;
    margin-right: px;
    overflow: hidden;
    background: #F;
    opacity: .;
    border-radius: px;
   }
   #box .count li.current {
    color: #fff;
    opacity: .;
    font-weight: ;
    background: #f
   }
  </style>
 </head>
 <body>
 <div id="box">
  <ul class="list">
   <li class="current" style="opacity: ;"><img src="img/images/.jpg" width="" height=""></li>
   <li style="opacity: ;"><img src="img/images/.jpg" width="" height=""></li>
   <li style="opacity: ;"><img src="img/images/.jpg" width="" height=""></li>
   <li style="opacity: ;"><img src="img/images/.jpg" width="" height=""></li>
   <li style="opacity: ;"><img src="img/images/.jpg" width="" height=""></li>
  </ul>
  <ul class="count">
   <li class="current"></li>
   <li class=""></li>
   <li class=""></li>
   <li class=""></li>
   <li class=""></li>
  </ul>
 </div>
 <script>
  var box=document.getElementById('box');
  var uls=document.getElementsByTagName('ul');
  var imgs=uls[].getElementsByTagName('li');
  var btn=uls[].getElementsByTagName('li');
  var i=index=; //中间量,统一声明;
  var play=null;
  console.log(box,uls,imgs,btn);//获取正确
  //图片切换, 淡入淡出效果我是用(transition: opacity .s linear)做的,不纠结、简单 在css里面
  function show(a){    //方法定义的是当传入一个下标时,按钮和图片做出对的反应
   for(i=;i<btn.length;i++ ){
    btn[i].className='';  //很容易看懂吧?每个按钮都先设置成看不见,然后把当前按钮设置成可见。
    btn[a].className='current';
   }
   for(i=;i<imgs.length;i++){ //把图片的效果设置和按钮相同
    imgs[i].style.opacity=;
    imgs[a].style.opacity=;
   }
  }
  //切换按钮功能,响应对应图片
  for(i=;i<btn.length;i++){
   btn[i].index=i; //不知道你有没有发现,循环里的方法去调用循环里的变量体i,会出现调到的不是i的变动值的问题。所以我先在循环外保存住
   btn[i].onmouseover=function(){
    show(this.index);
    clearInterval(play); //这就是最后那句话追加的功能
   }
  }
  //自动轮播方法
 function autoPlay(){
    play=setInterval(function(){ //这个paly是为了保存定时器的,变量必须在全局声明 不然其他方法调不到 或者你可以调用auto.play 也许可以但是没时间试了
    index++;
    index>=imgs.length&&(index=);//可能有优先级问题,所以用了括号,没时间测试了。
    show(index);
   },)
  }
  autoPlay();//马上调用,我试过用window.onload调用这个方法,但是调用之后影响到了其他方法,使用autoPlay所以只能这样调用了
  //div的鼠标移入移出事件
  box.onmouseover=function(){
   clearInterval(play);
  };
  box.onmouseout=function(){
   autoPlay();
  };
  //按钮下标也要加上相同的鼠标事件,不然图片停止了,定时器没停,会突然闪到很大的数字上。 貌似我可以直接追加到之前定义事件中。
 </script>
 </body>
 </html>
Copy after login

Code 2:

The first step is to download a jquery library plug-in. jquery.js can be downloaded from many places on the Internet. The downloaded plug-in should be placed in the directory. Then link

in the html document

The second step is to lay out a DIV, such as:

<div id="scroll">
 <p class="subl">上一张<p/>
 <p class="subr">下一张<p/>
 <ul>
 <li style="background:red;display:block;"></li>
 //上面的li要设定为显示,因为是第一张图片.
 <li style="background:green;"></li>
 <li style="background:gray;"></li>
 <li style="background:orange;"></li>
 </ul>
 </div>
Copy after login

In order to make it easier for netizens to download clearly, I did not use the image path, because it will not be visible on your computer. Here I use the background color.

The third step is to write CSS. Anyone who knows the basics of CSS can understand the following CSS.

#scroll{width:100%; height:180px; background-color:white; position:relative;border-bottom:1px solid gray;}//这里是给整个大的DIV设定属性.
#scroll ul{height:180px; list-style:none;}//DIV下的UL属性.
#scroll ul li{width:100%; height:180px;margin:0px; padding:0px; display:none;}//DIV下的UL下的LI属性.注意:display:none;因为要将所有的li隐藏了先.当点击的时候在显示出来.
 .subl{position:absolute; bottom:20px; left:40%; width:80px;height:20px; line-height:20px; text-align:center;font-size:16px;font-weight:bold; cursor:pointer;}//上一张按钮的属性.注意一个绝对定位.
 .subr{
position:absolute;
bottom:20px; right:40%;
width:80px;height:20px; line-height:20px; text-align:center;font-size:16px;font-weight:bold;cursor:pointer; 
}//下一张按钮的属性.注意一个绝对定位.
.subr:hover{ background:yellow;border-radius:10px;}
.subl:hover{ background:yellow;border-radius:10px;}
//以上两个hover是鼠标经过的效果.
Copy after login

The fourth step is the jquery code! It's also very simple. Read the code first and you will be able to use it!

<script type="text/javascript">
/*轮播*/
 $(function(){
 var i=0;
 var len=$("#scroll ul li").length-1;
 $(".subl").click(function(){
 if(i==len){
i=-1;
}
i++;
$("#scroll ul li").eq(i).fadeIn().siblings().hide();
 });
//到这里分开!上面的是上一张点击的效果代码,下面的是下一张点击的效果代码.
 $(".subr").click(function(){//获取类名的点击事件.
 if(i==0){
i=len+1;
}
i--;
$("#scroll ul li").eq(i).fadeIn().siblings().hide();
 });
 });
 /*轮播*/
</script>
Copy after login

Four easy steps to make a simple wheel!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles