1: 轮播的实现原理是怎样的?如果让你来实现,你会抽象出哪些函数(or接口)供使用?(比如 play())
实现原理:
将轮播的图片都水平排列,复制第一张和最后一张图片分别放到最后面和最前面,当滚动到复制的两个图片时,回到原图片的位置。
接口:
playNext() //轮播到下一张图片
playPre() //轮播到上一张图片
setBullet() //激活点击的按钮
2: 实现视频中的左右滚动无限循环轮播效果
<script>
$prebtn = $('a.pre');
$nextbtn = $('a.next');
$imgCt = $('.img-ct');
$imgs = $('.img-ct>li');
$bullets = $('.bullet li');
var pageIndex = 0;
var isanimate = false;
$imgCount = $imgs.length;
$imgWidth = $imgs.width();
$imgCt.append($imgs.first().clone());
$imgCt.prepend($imgs.last().clone());
$imgCt.width(($imgCount + 2) * $imgWidth);
$imgCt.css({left: -$imgWidth});
$prebtn.on('click',function(){
imgPre(1);
});
$nextbtn.on('click',function(){
imgNext(1);
});
function imgPre(len){
if(isanimate) return;
isanimate = true;
$imgCt.animate({
left: '+=' + len*$imgWidth
},function(){
pageIndex-=len;
if(pageIndex < 0){
pageIndex = 3;
$imgCt.css({left:-($imgCount)*$imgWidth});
}
setBullet();
isanimate = false;
});
}
function imgNext(len){
if(isanimate) return;
isanimate = true;
$imgCt.animate({
left: '-=' + len*$imgWidth
},function(){
pageIndex+=len;
if(pageIndex > 3){
pageIndex = 0;
$imgCt.css({left:-$imgWidth});
}
setBullet();
isanimate = false;
});
}
function setBullet(){
$bullets.removeClass('active')
.eq(pageIndex).addClass('active');
}
$bullets.on('click',function(){
$bullets.removeClass('active');
$(this).addClass('active');
var index = $(this).index();
console.log(index);
if(index > pageIndex){
imgNext(index - pageIndex);
}
if(index < pageIndex){
imgPre(pageIndex - index);
}
})
</script>
3: 实现一个渐变轮播效果, 效果范例
主要是利用fadeIn和fadeOut函数,另外需要注意一下css的写法,如果是采用注释掉的写法,渐变过程会有空白,而第二种则会好很多。我的天,好神奇。
code
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset="utf-8">
<title>轮播</title>
<style>
ul,li {
list-style: none;
margin: 0;
padding: 0;
}
/*div.carousel {
width: 350px;
height: 200px;
overflow: hidden;
position: relative;
}
ul.img-ct {
width: 350px;
}
ul.img-ct li {
display: none;
}
ul.img-ct li:first-child {
display: list-item;
}
ul.img-ct li img {
width: 350px;
height: 200px;
}*/
div.carousel{
position: relative;
width: 350px;
height: 200px;
}
div.carousel ul.img-ct{
position: relative;
}
div.carousel ul.img-ct img{
width: 350px;
height: 200px;
}
div.carousel .img-ct li{
position: absolute;
display: none;
}
ul.img-ct li:first-child {
display: list-item;
}
.arrow {
display: inline-block;
text-decoration: none;
color: white;
width: 40px;
height: 40px;
border: 1px solid white;
border-radius: 50%;
text-align: center;
line-height: 40px;
}
.arrow:hover {
opacity: 0.8;
}
a.pre {
position: absolute;
top: 50%;
left: 10px;
margin-top: -20px;
}
a.next {
position: absolute;
top: 50%;
right: 10px;
margin-top: -20px;
}
ul.bullet::after {
content: '';
display: block;
clear: both;
}
ul.bullet li {
border: 1px solid green;
width: 30px;
height: 5px;
float: left;
margin: 3px;
border-radius: 3px;
cursor: pointer;
}
ul.bullet {
position: absolute;
left: 50%;
bottom: 10px;
transform: translate(-50%,0);
}
ul.bullet li.active {
background: #ccc;
}
</style>
</head>
<body>
<div class="carousel">
<ul class="img-ct">
<li data-id=0><a href="#">![](http://upload-images.jianshu.io/upload_images/5359241-6ee888d901ecf912.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
<li data-id=1><a href="#">![](http://cdn.jirengu.com/book.jirengu.com/img/3.jpg)</a></li>
<li data-id=2><a href="#">![](http://upload-images.jianshu.io/upload_images/5359241-f52d051321d737a6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
<li data-id=3><a href="#">![](http://upload-images.jianshu.io/upload_images/5359241-feed6cca6a8ea0c3.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
</ul>
<a class="pre arrow" href="#"><</a>
<a class="next arrow" href="#">></a>
<ul class="bullet">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<script>
$prebtn = $('a.pre');
$nextbtn = $('a.next');
$imgCt = $('.img-ct');
$imgs = $('.img-ct>li');
$bullets = $('.bullet li');
var curIndex = 0;
var isanimate = false;
$imgCount = $imgs.length;
$imgWidth = $imgs.width();
// showImg(0);
// autoPlay();
$prebtn.on('click',function(){
showImg(($imgCount + curIndex - 1) % $imgCount);
});
$nextbtn.on('click',function(){
showImg((curIndex + 1) % $imgCount);
});
function showImg(index){
if(isanimate) return;
isanimate = true;
$imgs.eq(curIndex).fadeOut(500);
$imgs.eq(index).fadeIn(500,function(){
isanimate =false;
});
curIndex = index;
console.log(curIndex);
setBullet();
}
function setBullet(){
$bullets.removeClass('active')
.eq(curIndex).addClass('active');
}
$bullets.on('click',function(){
var index = $(this).index();
console.log(index);
showImg(index);
})
function autoPlay(){
setInterval(function(){
showImg((curIndex + 1) % $imgCount);
}, 2000);
}
</script>
</body>
</html>