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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

<!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<script type="text/javascript" src="jQuery.js"></script>

in the html document

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

1

2

3

4

5

6

7

8

9

10

11

&lt;div id="scroll"&gt;

 &lt;p class="subl"&gt;上一张&lt;p/&gt;

 &lt;p class="subr"&gt;下一张&lt;p/&gt;

 &lt;ul&gt;

 &lt;li style="background:red;display:block;"&gt;&lt;/li&gt;

 //上面的li要设定为显示,因为是第一张图片.

 &lt;li style="background:green;"&gt;&lt;/li&gt;

 &lt;li style="background:gray;"&gt;&lt;/li&gt;

 &lt;li style="background:orange;"&gt;&lt;/li&gt;

 &lt;/ul&gt;

 &lt;/div&gt;

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.

1

2

3

4

5

6

7

8

9

10

11

12

#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!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

&lt;script type="text/javascript"&gt;

/*轮播*/

 $(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();

 });

 });

 /*轮播*/

&lt;/script&gt;

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 Article Tags

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)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles