87,993
社区成员
发帖
与我相关
我的任务
分享


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>aaaaa</title>
<style>
body,
ul,
ol {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
img {
border: none;
vertical-align: top;
}
a {
display: block;
width: 50px;
height: 50px;
}
#box {
width: 470px;
height: 150px;
position: relative;
margin: 30px auto;
overflow: --hidden;
}
ul {
width: 470px;
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
ul li {
width: 470px;
position: absolute;
top: 0;
height: 150px;
background-color: yellow;
}
ul li:nth-of-type(1){
background-color: red;
}
ul li:nth-of-type(2){
background-color: cyan;
}
ul li img {
display: block;
width: 470px;
height: 150px;
}
ol {
z-index: 2;
width: 120px;
position: absolute;
right: 10px;
bottom: 10px;
}
ol li {
width: 20px;
height: 20px;
float: left;
margin: 0 2px;
display: inline;
background: #fff;
color: #f60;
line-height: 20px;
text-align: center;
}
ol .active {
background: #f60;
color: #fff;
}
</style>
<script src="https://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function() {
//选择ol li
var $olLi = $("ol li");
//选择ul li
var $ulLi = $("ul li");
//获取到一个li的宽度
var oneLiWidth = $ulLi.outerWidth();
//当前图片位置
var iNow = 0;
var delayTimer = null;
var autoTimer = null;
var $aNext = $("#next");
var $aPrev = $("#prev");
var flag = true;
function posPic() {
if (flag) {
$ulLi.slice(1).each(function() {
$(this).css('left', -oneLiWidth)
})
} else if (flag == false) {
$ulLi.slice(1).each(function() {
$(this).css('left', oneLiWidth)
})
}
}
posPic();
function go() {
if (flag) {
iNow++;
console.log(iNow)
} else if (flag == false) {
iNow--;
var index = $ulLi.eq(iNow).index();
iNow = index;
}
if (iNow > $ulLi.length - 1) {
iNow = 0;
}
animatePic(flag, iNow)
}
$aNext.on('click', function() {
console.log($ulLi.is(":animated"));
//防止多次点击
if ($ulLi.is(":animated")) {
return false;
}
next();
posPic();
auto();
})
$aPrev.on('click', function() {
//防止多次点击
if ($ulLi.is(":animated")) {
return false;
}
prev();
posPic();
console.log(flag);
auto();
})
function next() {
clearTimeout(delayTimer);
clearInterval(autoTimer);
flag = true;
go();
}
function prev() {
clearTimeout(delayTimer);
clearInterval(autoTimer);
flag = false;
go();
}
function animatePic(bol, iNow) {
if (bol) {
$('ol li').eq(iNow).addClass('active').siblings().removeClass('active');
//先把要点击的定位到左边
$ulLi.eq(iNow).css({
"left": oneLiWidth
});
$ulLi.eq(iNow).animate({
"left": 0
}, 1500).siblings().animate({
"left": -oneLiWidth
}, 1500);
} else {
console.log(flag + '7777');
posPic();
$('ol li').eq(iNow).addClass('active').siblings().removeClass('active');
//当前索引值
var _index = $ulLi.index();
console.log(_index)
console.log(iNow)
if (iNow < _index) {
//准备移动选的index图片定位到右边
$ulLi.eq(_index).css("left", oneLiWidth);
console.log('当前iNow=' + iNow + '当前->1 index=' + _index)
//判断当前的iNow值小于索引值从右到左
//让当前的图片移动到左边
$ulLi.eq(iNow).animate({
left: -oneLiWidth
}, 1500);
} else if (iNow > _index) {
//准备移动选的index图片定位到左边
$ulLi.eq(_index).css("left", oneLiWidth);
console.log('当前iNow=' + iNow + '当前->2 index=' + _index)
//判断当前的iNow值大于索引值从左到右
//让当前的图片移动到右边
$ulLi.eq(iNow).animate({
left: -oneLiWidth
}, 1500);
}
//让当前的图片从左边移动到右边
$ulLi.eq(_index).animate({
left: 0
}, 1500);
flag = true;
}
}
function auto() {
autoTimer = setInterval(go, 3000);
}
auto();
//给鼠标移动到当前的li加active
$olLi.on("mouseover", function() {
var _this = this;
clearTimeout(delayTimer);
clearInterval(autoTimer);
console.log(iNow);
delayTimer = setTimeout(function() {
$(_this).addClass('active').siblings().removeClass('active');
//当前索引值
var _index = $(_this).index();
if (iNow < _index) {
//准备移动选的index图片定位到右边
$ulLi.eq(_index).css("left", oneLiWidth);
console.log('当前iNow=' + iNow + '当前->1 index=' + _index)
//判断当前的iNow值小于索引值从右到左
//让当前的图片移动到左边
$ulLi.eq(iNow).animate({
left: -oneLiWidth
}, 1500);
} else if (iNow > _index) {
//准备移动选的index图片定位到左边
$ulLi.eq(_index).css("left", -oneLiWidth);
console.log('当前iNow=' + iNow + '当前->2 index=' + _index)
//判断当前的iNow值大于索引值从左到右
//让当前的图片移动到右边
$ulLi.eq(iNow).animate({
left: oneLiWidth
}, 1500);
}
//让当前的图片从左边移动到右边
$ulLi.eq(_index).animate({
left: 0
}, 1500);
//给iNow赋值
iNow = _index;
}, 400)
});
//鼠标离开的时候继续执行auto
$olLi.on("mouseout", function() {
console.log(iNow);
auto();
})
});
</script>
</head>
<body>
<div id="box">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ol>
<li class="active">1</li>
<li>2</li>
<li>3</li>
</ol>
</div>
<a id='next' href="javascript:;">next</a>
<a id='prev' href="javascript:;">prev</a>
</body>
</html>
这是代码 我前面想的是发到jsbin上然后好一点么...才想起来这里也可以发代码
