1、无缝连续滚动特效
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 1000px;
height: 130px;
border: 1px solid #000;
margin: 50px auto;
overflow: hidden;
}
.box ul {
list-style: none;
/* 设置大一点,这样li才能浮动 */
width: 5000px;
position: relative;
}
.box ul li {
float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<div id="box" class="box">
<ul id="list">
<li><img src="../../images/number/0.png" alt=""></li>
<li><img src="../../images/number/1.png" alt=""></li>
<li><img src="../../images/number/2.png" alt=""></li>
<li><img src="../../images/number/3.png" alt=""></li>
<li><img src="../../images/number/4.png" alt=""></li>
<li><img src="../../images/number/5.png" alt=""></li>
</ul>
</div>
<script>
var box = document.getElementById('box');
var list = document.getElementById('list');
// 复制多一遍所有的li
list.innerHTML += list.innerHTML;
// 全局变量,表示当前list的left值
var left = 0;
// 定时器,全局变量
var timer;
move();
// 动画封装成函数
function move() {
// 设表先关,防止动画积累
clearInterval(timer);
timer = setInterval(function() {
left -= 4;
// 验收
if (left <= -1260) {
left = 0;
}
list.style.left = left + 'px';
}, 20);
}
// 鼠标进入停止定时器
box.onmouseenter = function() {
clearInterval(timer);
};
// 鼠标离开继续定时器
box.onmouseleave = function() {
move();
};
</script>
</body>
</html>
- 将ul复制两份,让用户产生无限滚动的错觉。
- 采用定时器,控制ul的滚动
timer = setInterval(function() {
left -= 4;
// 验收
if (left <= -1260) {
left = 0;
}
list.style.left = left + 'px';
}, 20);
- 当鼠标进入box,就立即停止定时器;将定时器封装在一个函数中,方便鼠标离开重新启动。
box.onmouseenter = function() {
clearInterval(timer);
};
box.onmouseleave = function() {
move();
};
- 康康效果~
2.轮播图特效
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#carousel{
width: 650px;
height: 360px;
margin: 50px auto;
border: 1px solid #000000;
position: relative;
overflow: hidden;
}
#carousel ul{
list-style:none;
width: 6000px;
position: relative;
left: 0;
transition: left .5s ease 0s;
}
#carousel ul li{
float: left;
}
#carousel .leftbtn{
position: absolute;
width: 50px;
height: 50px;
top: 50%;
margin-top: -25px;
left: 20px;
background-color:rgba(102,180,254,.4);
}
#carousel .leftbtn:hover,#carousel .rightbtn:hover{
background-color:rgba(102,180,254,.8);
}
#carousel .rightbtn{
position: absolute;
width: 50px;
height: 50px;
top: 50%;
margin-top: -25px;
right: 20px;
background-color:rgba(102,180,254,.4);
}
</style>
</head>
<body>
<div id="carousel">
<ul id="list">
<li><img src="../../images/beijing/0.jpg" alt=""></li>
<li><img src="../../images/beijing/1.jpg" alt=""></li>
<li><img src="../../images/beijing/2.jpg" alt=""></li>
<li><img src="../../images/beijing/3.jpg" alt=""></li>
<li><img src="../../images/beijing/4.jpg" alt=""></li>
</ul>
<a href="javascript:;" class="leftbtn" id="leftbtn"><img src="../../images/prev_active.png" ></a>
<a href="javascript:;" class="rightbtn" id="rightbtn"><img src="../../images/next_active.png" ></a>
</div>
<script type="text/javascript">
var list = document.getElementById('list');
var leftbtn = document.getElementById('leftbtn');
var rightbtn = document.getElementById('rightbtn');
var cloneli = list.firstElementChild.cloneNode(true);
list.appendChild(cloneli);
//记录到了第几个li
var index = 0;
//节流
var lock = true;
rightbtn.onclick = function(){
if(!lock) return;
lock = false;
list.style.transition = 'left .5s ease 0s';
index++;
if(index>4){
//延时器目的是为了瞬移到第一张照片,为什么要延迟0.5s呢,因为0.5s需要从最后一张照片过渡到复制的第一张照片上。
setTimeout(function(){
list.style.transition = 'none';
list.style.left = 0;
index=0;
// alert('hhh');
},500);
}
//点击最后一张照片时,先过渡到复制的那张照片上,再执行延时器函数。
// else{
list.style.left = -(index*650)+'px';
// }
setTimeout(function(){
lock = true;
},500);
}
leftbtn.onclick = function(){
if(!lock) return;
lock = false;
// list.style.transition = 'left .5s ease 0s';
if(index==0){
list.style.transition = 'none';
list.style.left= -(650*5)+'px';
//瞬间过渡到最后一张,再延时0s执行平缓过渡到倒数第二张操作
setTimeout(function(){
list.style.transition = 'left .5s ease 0s';
list.style.left = -(650*4)+'px';
index=4;
},0);
}else{
index--;
list.style.left = -(index*650)+'px';
}
setTimeout(function(){
lock = true;
},500);
}
</script>
</body>
</html>
- 我们先制作右按钮特效,复制第一个li到ul的最后一个子节点
var cloneli = list.firstElementChild.cloneNode(true);
list.appendChild(cloneli);
- 实现机理就是当鼠标点击最后一个li的右按钮时,先平缓过渡到新增的第一张li上,再执行瞬间跳转到第一张li中,这里使用延时器,将瞬间移动操作延时到平缓过渡之后,让使用者视觉上察觉不出来
rightbtn.onclick = function(){
list.style.transition = 'left .5s ease 0s';
index++;
if(index>4){
//延时器目的是为了瞬移到第一张照片,为什么要延迟0.5s呢,因为0.5s需要从最后一张照片过渡到复制的第一张照片上。
setTimeout(function(){
list.style.transition = 'none';
list.style.left = 0;
index=0;
// alert('hhh');
},500);
}
//点击最后一张照片时,先过渡到复制的那张照片上,再执行延时器函数。
list.style.left = -(index*650)+'px';
}
- 制作左按钮特效,当用户点击第一张li的左按钮时,将ul瞬移到最后一张新增的li上,然后再利用延时器平缓过渡到倒数第二张上。
leftbtn.onclick = function(){
if(!lock) return;
lock = false;
// list.style.transition = 'left .5s ease 0s';
if(index==0){
list.style.transition = 'none';
list.style.left= -(650*5)+'px';
//瞬间过渡到最后一张,再延时0s执行平缓过渡到倒数第二张操作
setTimeout(function(){
list.style.transition = 'left .5s ease 0s';
list.style.left = -(650*4)+'px';
index=4;
},0);
}else{
index--;
list.style.left = -(index*650)+'px';
}
setTimeout(function(){
lock = true;
},500);
}
- 最后为了防止用户操作过于频繁,需要进行节流操作。
var lock = true;
btn.onclick = function(){
if(!lock) return;
lock = false;//上锁
......
setTimeout(function(){
lock = true;//开锁
},500);
}
- 康康效果~
呼吸轮播图特效
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#carousel{
width: 650px;
height: 360px;
margin: 50px auto;
border: 1px solid #000000;
position: relative;
}
#carousel ul{
list-style:none;
}
#carousel ul li{
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 1s ease 0s;
}
#carousel ul li:first-child{
opacity: 1;
}
#carousel .leftbtn{
position: absolute;
width: 50px;
height: 50px;
border-radius: 50%;
top: 50%;
margin-top: -25px;
left: 20px;
background-color:rgba(102,180,254,.4);
}
#carousel .leftbtn:hover,#carousel .rightbtn:hover{
background-color:rgba(102,180,254,.8);
}
#carousel .rightbtn{
position: absolute;
width: 50px;
height: 50px;
border-radius: 50%;
top: 50%;
margin-top: -25px;
right: 20px;
background-color:rgba(102,180,254,.4);
}
</style>
</head>
<body>
<div id="carousel">
<ul id="list">
<li><img src="../../images/beijing/0.jpg" alt=""></li>
<li><img src="../../images/beijing/1.jpg" alt=""></li>
<li><img src="../../images/beijing/2.jpg" alt=""></li>
<li><img src="../../images/beijing/3.jpg" alt=""></li>
<li><img src="../../images/beijing/4.jpg" alt=""></li>
</ul>
<a href="javascript:;" class="leftbtn" id="leftbtn"><img src="../../images/prev_active.png" ></a>
<a href="javascript:;" class="rightbtn" id="rightbtn"><img src="../../images/next_active.png" ></a>
</div>
<script type="text/javascript">
var list = document.getElementById('list');
var leftbtn = document.getElementById('leftbtn');
var rightbtn = document.getElementById('rightbtn');
var lis = list.getElementsByTagName('li');
//记录到了第几个li
var index = 0;
//节流
var lock = true;
rightbtn.onclick = function(){
if(!lock) return;
lock = false;
lis[index].style.opacity = 0;
index++;
if(index>4){
index=0;
}
lis[index].style.opacity = 1;
setTimeout(function(){
lock = true;
},1000);
}
leftbtn.onclick = function(){
if(!lock) return;
lock = false;
lis[index].style.opacity = 0;
if(index==0){
index=5;
}
index--;
lis[index].style.opacity = 1;
setTimeout(function(){
lock = true;
},1000);
}
</script>
</body>
</html>
这个在轮播图特效基础上,将每个li采用绝对定位在相同位置,通过记录当前显示li的位置,改变前后li的透明度来实现呼吸轮播跳转的效果。同样设置节流锁,防止页面切换错误。
康康效果~