JavaScript轮播图一
源码分析
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
body{
margin: 0;
padding: 0;
}
.carousel{
width: 100%;
height: 33.3vw;
position: relative;
left: 0;
top: 0;
font-size: 0;
min-width: 1000px;
}
.carousel>.img-con{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
transition: all 0.5s;
}
.carousel>.img-con>img{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.carousel>.img-con>div{
position: absolute;
left:10vw;
top: 2vw;
font-size: 20px;
color: white;
}
.carousel>.img-con>div>span>i{
font-style:normal;
font-size: 28px;
}
.carousel>.img-con>div>h1{
font-size: 20px;
}
ul{
position: absolute;
right: 50px;
top:50%;
transform: translate(0,-50%);
list-style: none;
margin: 0;
padding: 0;
}
ul>li{
margin-top: 1vw;
}
ul>li img{
border:2px solid transparent;
}
ul>li:first-child
{
margin-top: 0;
}
style>
head>
<body>
<div class="carousel">
<div class="img-con" id="a">
<img src="./img/a.jpg">
<div>
<span><i>28i>/Jul.2022span>
<h1>与父母的47天自驾游|向疆而行2万里,我们依旧是过客h1>
div>
div>
<div class="img-con" id="b">
<img src="./img/b.jpg">
<div>
<span><i>27i>/Jul.2022span>
<h1>自驾川西小环线,在千碉之国遇见梨花如雪的季节h1>
div>
div>
<div class="img-con" id="c">
<img src="./img/c.jpg">
<div>
<span><i>26i>/Jul.2022span>
<h1>被误解的沙县,原来有这么多美食只有在当地才能吃到!h1>
div>
div>
<div class="img-con" id="d">
<img src="./img/d.jpg">
<div>
<span><i>25i>/Jul.2022span>
<h1>周末出逃计划 | 打卡美丽中国h1>
div>
div>
<div class="img-con" id="e">
<img src="./img/e.jpg">
<div>
<span><i>24i>/Jul.2022span>
<h1>寻迹山川湖海,邂逅云南的冬与夏h1>
div>
div>
<ul>
<li><a href="#a"><img src="./img/a_icon.png">a>li>
<li><a href="#b"><img src="./img/b_icon.png">a>li>
<li><a href="#c"><img src="./img/c_icon.png">a>li>
<li><a href="#d"><img src="./img/d_icon.png">a>li>
<li><a href="#e"><img src="./img/e_icon.png">a>li>
ul>
div>
<script>
var imgPrev,ul,iconPrev;
init();
function init(){
ul=document.querySelector("ul");
window.addEventListener("hashchange",hashChangeHandler);
var index=["#a","#b","#c","#d","#e"].indexOf(location.hash);
if(index===-1) location.href+="#a";
else hashChangeHandler();
}
function hashChangeHandler(e){
var id=location.hash.slice(1);
imgPrev=changePrev(imgPrev,document.getElementById(id),"opacity",0,1);
var index=["#a","#b","#c","#d","#e"].indexOf(location.hash);
iconPrev=changePrev(iconPrev,ul.children[index].firstElementChild.firstElementChild,"borderColor","transparent","#ff9d00")
}
function changePrev(prev,elem,prop,value1,value2){
if(prev){
prev.style[prop]=value1;
}
elem.style[prop]=value2;
return elem;
}
script>
body>
html>

- 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
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177