您的位置:首页 > Web前端 > JavaScript

js新手图片切换,样式好看,功能强大,代码简单(用新手能懂得方式写的)

2019-01-23 21:30 267 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_42044542/article/details/86617914

大概就是这个样式,挺完美的,很我尽量用最简单的方法写的,因为我也是新手,哈哈,如果你要用加上自己的图片就好了。

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>js轮播图</title>

<style type="text/css">

*{

margin: 0;

padding: 0;

}

#content-1{

position: relative;

width: 70%;

height: 600px;

background: red;

left: 300px;

}

.ul-1{

width: 100%;

height: 600px;

position: relative;

list-style: none;

}

.li-1{

width: 100%;

height: 100%;

position: absolute;

color: white;

font-size: 70px;

opacity: 0;

transition: all .5s;

}

.li-1:nth-child(1){

background: rgb(251, 255, 3);

}

.li-1:nth-child(2){

background: rgb(7, 242, 250);

}

.li-1:nth-child(3){

background:palevioletred;

}

.li-1:nth-child(4){

background:rgb(243, 6, 85);

}

.li-1:nth-child(5){

background:rgb(9, 247, 68);

}

.button_big{

width: 40px;

height: 80px;

background: white;

font-size: 30px;

position: absolute;

top: 250px;

border: 0;

z-index: 999;

}

#btn-pre{

left: 20px;

}

#btn-next{

right: 20px;

}

.li-1.active{

z-index: 888;

opacity:1;

}

/*五个圆圈*/

#ul-2{

position: absolute;

top:80%;

left: 36%;

z-index: 1000;

list-style: none;

}

.circle{

width: 12px;

height: 12px;

margin-left: 20px;

float: left;

border: 3px solid white;

border-radius: 100%;

background: rgb(112, 110, 111);

cursor: pointer;

}

.circle.styles{

background: white;

}

</style>

</head>

<body>

 

<div id="content-1">

<ul class="ul-1"><!--图片区域-->

<li class="li-1 active" >0</li>

<li class="li-1" >1</li>

<li class="li-1" >2</li>

<li class="li-1" >3</li>

<li class="li-1" >4</li>

</ul>

<button type="button" class="button_big" id="btn-pre"><</button>

<button type="button" class="button_big" id="btn-next">></button>

<ul id="ul-2"><!--小圈圈区域-->

<li class="circle styles" data-index="0"></li>

<li class="circle" data-index="1"></li>

<li class="circle" data-index="2"></li>

<li class="circle" data-index="3"></li>

<li class="circle" data-index="4"></li>

</ul>

</div>


 

<script type="text/javascript">

var li = document.getElementsByClassName("li-1");//图片

var pre = document.getElementById("btn-pre");//按钮

var next = document.getElementById("btn-next");

var circles = document.getElementsByClassName("circle");//获取圆圈

var index = 0;

//清空样式

function clearli(){

for(var i = 0;i<li.length;i++){

li[i].className = "li-1";

circles[i].className = "circle";

}

}

//添加样式

function goindex(){

clearli();

li[index].className = "li-1 active";

circles[index].className = "circle styles";

}

//返回上一张图片

function gopre(){

if(index==0){

index=4;

}

else{

index--;

}

goindex();

}

//加click事件,执行gopre函数

pre.addEventListener("click",function(){

gopre();

})

//进入下一张图片

function gonext(){

if(index==4){

index=0;

}

else{

index++;

}

goindex();

}

//加click事件,执行gonext函数

next.addEventListener("click",function(){

gonext();

})

//小圆圈的样式和图片切换

for(a=0;a<circles.length;a++){

circles[a].addEventListener("click",function(){

var data_index = this.getAttribute("data-index");

index = data_index;

goindex();

})

}

</script>

</body>

</html>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: