<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<title
>Title
</title
>
<style
>
*{
margin
: 0;
padding
: 0;
text
-decoration
: none
;
}
#container
{
width
: 600px
;
height
: 400px
;
margin
: 30px auto
;
position
: relative
;
overflow
: hidden
;
}
#list
{
width
: 4200px
;
height
: 400px
;
position
: absolute
;
}
#list img
{
float
: left
;
}
#pointDiv
{
position
: absolute
;
bottom
: 20px
;
width
: 80px
;
left
: 260px
;
}
#pointDiv span
{
width
: 10px
;
height
: 10px
;
border
-radius
: 50%;
background
: #
333;
border
:1px solid #fff
;
float
: left
;
margin
-right
: 5px
;
}
#pointDiv
.on
{
background
: orangered
;
}
#pointDiv span
:last
-child
{
margin
-right
: 0;
}
.arrow
{
position
: absolute
;
cursor
: pointer
;
color
: #fff
;
width
: 40px
;
height
: 40px
;
line
-height
: 40px
;
text
-align
: center
;
font
-size
: 32px
;
background
: rgba(0,0,0,0.3);
top
: 180px
;
}
.arrow
:hover
{
background
: rgba(0,0,0,0.7);
}
#prev
{
left
: 20px
;
}
#next
{
right
: 20px
;
}
#container
:hover
{
cursor
: pointer
;
}
</style
>
</head
>
<body
>
<div id
="container">
<div id
="list" style
="left:-600px">
<img src
="img/5.jpg" alt
="">
<img src
="img/1.jpg" alt
="">
<img src
="img/2.jpg" alt
="">
<img src
="img/3.jpg" alt
="">
<img src
="img/4.jpg" alt
="">
<img src
="img/5.jpg" alt
="">
<img src
="img/1.jpg" alt
="">
</div
>
<div id
="pointDiv">
<span class
="on"></span
>
<span
></span
>
<span
></span
>
<span
></span
>
<span
></span
>
</div
>
<a href
="javascript:;" id
="prev" class
="arrow"><</a
>
<a href
="javascript:;" id
="next" class
="arrow">></a
>
</div
>
<script src
="js/jquery.1.10.2.js"></script
>
<script
>
$
(function(){
var $container
= $
('#container');
var $list
= $
('#list');
var $points
= $
('#pointDiv span');
var $prev
= $
('#prev');
var $next
= $
('#next');
var imgCount
= $points
.length
;
var index
= 0;
var PAGE_WIDTH
= 600;
var TIME
= 400;
var TIME_TIME
= 20;
var moving
= false;
$next
.click(function(){
nextPage(true);
})
$prev
.click(function(){
nextPage(false);
})
var intervalId
= setInterval(function(){
nextPage(true);
},3000)
$
('#container').hover(function(){
clearInterval(intervalId
)
},function(){
intervalId
= setInterval(function(){
nextPage(true);
},3000)
})
$points
.click(function(){
var targetIndex
= $
(this
).index();
if(targetIndex
!= index
){
nextPage(targetIndex
);
}
})
function nextPage(next
){
if(moving
){
return;
}
moving
= true;
var offset
= 0;
if(typeof next
== 'boolean'){
offset
= next
? -PAGE_WIDTH
: PAGE_WIDTH
;
}else{
offset
= -(next
-index
) * PAGE_WIDTH
;
}
var itemOffset
= offset
/(TIME
/TIME_TIME
);
var currLeft
= $list
.position().left
;
var targetLeft
= currLeft
+ offset
;
console
.log(targetLeft
);
var intervalId
= setInterval(function(){
currLeft
+= itemOffset
;
if(currLeft
== targetLeft
){
clearInterval(intervalId
);
moving
= false;
if(currLeft
== -(imgCount
+ 1)* PAGE_WIDTH
){
currLeft
= -PAGE_WIDTH
;
}else if(currLeft
== 0){
currLeft
= -imgCount
* PAGE_WIDTH
;
}
}
$list
.css('left',currLeft
);
},TIME_TIME
)
updatePoints(next
);
}
function updatePoints(next
){
var targetIndex
= 0;
if(typeof next
== 'boolean'){
if(next
){
targetIndex
= index
+ 1;
if(targetIndex
== imgCount
){
targetIndex
= 0;
}
}else{
targetIndex
= index
-1;
if(targetIndex
== -1){
targetIndex
= imgCount
- 1;
}
}
}else{
targetIndex
= next
;
}
$points
[index
].className
= '';
$points
[targetIndex
].className
= 'on';
index
= targetIndex
;
}
})
</script
>
</body
>
</html
>
转载请注明原文地址: https://lol.8miu.com/read-8565.html