196 lines
5.2 KiB
HTML
196 lines
5.2 KiB
HTML
<!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>Document</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
|
|
ul {
|
|
list-style: none;
|
|
}
|
|
.slider {
|
|
width: 590px;
|
|
height: 470px;
|
|
border: 1px solid #ccc;
|
|
padding: 10px;
|
|
margin: 50px auto;
|
|
}
|
|
|
|
.inner {
|
|
width: 590px;
|
|
height: 470px;
|
|
overflow: hidden;
|
|
position: relative;
|
|
}
|
|
|
|
.inner ul {
|
|
width: 1000%;
|
|
position: absolute;
|
|
}
|
|
.inner ul li {
|
|
float: left;
|
|
}
|
|
.focus {
|
|
position: absolute;
|
|
bottom: 30px;
|
|
left: 30px;
|
|
}
|
|
.focus span {
|
|
padding:5px;
|
|
background: rgba(0, 0, 0, .5);
|
|
border-radius: 50%;
|
|
font-size: 0;
|
|
cursor: pointer;
|
|
display: inline-block;
|
|
transition: all .2s ease;
|
|
margin-left: 3px;
|
|
}
|
|
.focus span:hover ,.current{
|
|
transform: scale(1.2);
|
|
background: red;
|
|
}
|
|
|
|
.btns span {
|
|
position: absolute;
|
|
left: 10px;
|
|
top: 50%;
|
|
padding: 10px 20px;
|
|
background: rgba(0, 0, 0, .5);
|
|
color: #fff;
|
|
font-size: 30px;
|
|
cursor: pointer;
|
|
transform: translateY(-50%);
|
|
|
|
}
|
|
|
|
.btns span:last-child {
|
|
left:auto;
|
|
right: 10px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="slider" id="slider">
|
|
<div class="inner">
|
|
<ul>
|
|
<li><a href="#"><img src="https://img20.360buyimg.com/pop/s590x470_jfs/t1/82345/16/13178/77786/5da93550E6449f58e/f5b14f641913efba.jpg.webp" alt="" srcset=""></a></li>
|
|
<li><a href="#"><img src="https://img13.360buyimg.com/pop/s590x470_jfs/t1/73861/8/7311/133024/5d562902E29c956c1/da390c8c345c48e0.jpg.webp" alt="" srcset=""></a></li>
|
|
<li><a href="#"><img src="https://img13.360buyimg.com/pop/s590x470_jfs/t1/79136/10/13206/197242/5da92d47Ee54665df/39d85a2990ba58d1.jpg.webp" alt="" srcset=""></a></li>
|
|
<li><a href="#"><img src="https://imgcps.jd.com/ling/22692212401/6JGh6JCE6YWS6ZKc5oOg/6YOo5YiGMuS7tjXmipg/p-5bd8253082acdd181d02fa02/153dad62.jpg" alt="" srcset=""></a></li>
|
|
<li><a href="#"><img src="https://img14.360buyimg.com/pop/s590x470_jfs/t1/46901/24/7545/62345/5d52317fEebadcf80/8fb168cddd5ef2b9.jpg.webp" alt="" srcset=""></a></li>
|
|
</ul>
|
|
<!-- <div class="focus">
|
|
<span>1</span>
|
|
<span>2</span>
|
|
<span>3</span>
|
|
<span>4</span>
|
|
<span>5</span>
|
|
</div> -->
|
|
<div class="btns">
|
|
<span><</span>
|
|
<span>></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
window.onload = function(){
|
|
function my$(id) {
|
|
return document.getElementById(id)
|
|
}
|
|
//获取元素 初始化dom结构 焦点需要跟据图片数量去生成
|
|
var slider = my$('slider');
|
|
var inner = slider.children[0]
|
|
var ul = inner.children[0]
|
|
var imgW = inner.offsetWidth
|
|
var lis = ul.children
|
|
var timer = null
|
|
//根据li个数初始化焦点个数 初始化dom
|
|
// 先生成div 设置类
|
|
var div = document.createElement('div');
|
|
//设置class
|
|
div.classList.add('focus');
|
|
//批量生成span
|
|
for(var i = 0; i < lis.length; i++) {
|
|
var span = document.createElement('span')
|
|
span.innerHTML = i+1
|
|
span.index = i
|
|
// 给每个焦点绑定移入移出事件
|
|
span.onmouseover = function() {
|
|
//当前焦点变成选中状态
|
|
//ul 向左滚动
|
|
//排他
|
|
clearInterval(timer);
|
|
for(var i = 0; i< spans.length; i++) {
|
|
spans[i].className = ''
|
|
}
|
|
this.className = 'current'
|
|
// ul 向左移动多少
|
|
pic = this.index
|
|
animate(ul,-imgW*pic,10)
|
|
}
|
|
|
|
//鼠标移出
|
|
span.onmouseout = function() {
|
|
clearInterval(timer)
|
|
timer = null
|
|
timer = setInterval(run,2000)
|
|
}
|
|
div.appendChild(span)
|
|
}
|
|
//设置当前第一个焦点为选中状态
|
|
//把div放进inner中
|
|
inner.appendChild(div)
|
|
var spans = div.children
|
|
spans[0].className = 'current';
|
|
|
|
//克隆最后一张图放到第一张
|
|
ul.appendChild(ul.children[0].cloneNode(true))
|
|
|
|
//自动播放 记录图片的第一张图的位置 索引
|
|
var pic = 0;
|
|
|
|
|
|
//设置图片自动播放
|
|
timer = setInterval(run,2000)
|
|
|
|
function run(){
|
|
pic++;
|
|
if(pic > 5) {
|
|
pic = 1;
|
|
ul.style.left = 0;
|
|
}
|
|
for(var i = 0; i< spans.length; i++) {
|
|
spans[i].className = ''
|
|
}
|
|
//焦点和图片不对等 当图片是第六张的时候焦点是第一张焦点
|
|
spans[pic>4?0:pic].className = 'current';
|
|
//ul移动
|
|
animate(ul,-imgW*pic)
|
|
}
|
|
//移动动画
|
|
function animate(ele, target, step) {
|
|
clearInterval(ele.timer)
|
|
ele.timer = setInterval(function(){
|
|
var current = ele.offsetLeft
|
|
var step = step || 10
|
|
step = target - current >= 0 ? step : -step
|
|
current += step
|
|
if(Math.abs(target - current) > Math.abs(step)) {
|
|
ele.style.left = current + 'px';
|
|
} else {
|
|
clearInterval(ele.timer)
|
|
ele.style.left = target + 'px'
|
|
ele.timer = null
|
|
}
|
|
},10)
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |