为了账号安全,请及时绑定邮箱和手机立即绑定

正在回答

1 回答

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title>轮播图</title>
   <style type="text/css">
* {
           margin: 0;
padding: 0;
text-decoration: none;
}

       body {
           padding: 20px;
}

       #container {
           position: relative;
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
}

       #list {
           position: absolute;
z-index: 1;
width: 4200px;
height: 400px;
}

       #list img {
           float: left;
width: 600px;
height: 400px;
}
       #buttons {
           position: absolute;
left: 250px;
bottom: 20px;
z-index: 2;
height: 10px;
width: 100px;
}

       #buttons span {
           float: left;
margin-right: 5px;
width: 10px;
height: 10px;
border: 1px solid #fff;
border-radius: 50%;
background: #333;
cursor: pointer;
}

       #buttons .on {
           background: orangered;
}

       .arrow {
           position: absolute;
top: 180px;
z-index: 2;
display: none;
width: 40px;
height: 40px;
font-size: 36px;
font-weight: bold;
line-height: 39px;
text-align: center;
color: #fff;
background-color: RGBA(0, 0, 0, .3);
cursor: pointer;
}

       .arrow:hover {
           background-color: RGBA(0, 0, 0, .7);
}

       #container:hover .arrow {
           display: block;
}

       #prev {
           left: 20px;
}

       #next {
           right: 20px;
}
   </style>
</head>
<body>
<!--父容器container存放所有内容,
   子容器list存放图片。
   子容器buttons存放按钮小圆点
-->
<div id="container">
      <div id="list" style="left: -600px">
          <img src="img/05.jpg" alt="1">
          <img src="img/01.jpg" alt="1">
          <img src="img/02.jpg" alt="2">
          <img src="img/03.jpg" alt="3">
          <img src="img/04.jpg" alt="4">
          <img src="img/05.jpg" alt="5">
          <img src="img/01.jpg" alt="5">
      </div>
      <div id="buttons">
          <span index="1"></span>
          <span index="2"></span>
          <span index="3"></span>
          <span index="4"></span>
          <span index="5"></span>
      </div>
      <a href="javascript:;" id="prev">&lt;</a>
      <a href="javascript:;" id="next">&gt;</a>
  </div>
<script>
window.onload=function (){
       var  container =document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('buttons').getElementsByTagName('span');//获取5个按钮
var prev=document.getElementById('prev');//上一个
var next=document.getElementById('next');//下一个
var index=1;
var timer;
/*点亮小圆点*/
function showButton(){

           for(var i=0;i<buttons.length;i++){
               if(buttons[i].className=='on'){
                   buttons[i].className='';
break;
}
           }
           buttons[index-1].className='on';
}

       /*箭头点击事件**/
function animate(offset){//封装按钮左右点击,传参offset
var newLeft=parseInt(list.style.left)+offset;
var time=300;//位移总时间
var interval=10;//位移间隔
var speed=offset/(time/interval);//每次位移量


function go(){
               if((speed<0 && parseInt(list.style.left)>newLeft) || (speed >0&&parseInt(list.style.left)<newLeft)){
                   list.style.left=parseInt(list.style.left)+speed+'px';

/*定时器*/
setTimeout(go,interval);
}
               else{

                   list.style.left=newLeft+'px';
/* 判断是否滚动到附属图上,滚动到附属图上归位*/
if(newLeft> -600){
                       list.style.left = -3000 + 'px';
}
                   if(newLeft< -3000){
                       list.style.left = -600 + 'px';
}
               }
           }

               go();

}

       function play(){

           timer=setInterval(function (){
               next.onclick();
},3000);
}
       function stop(){
           clearInterval(timer);
}
       next.onclick=function (){
           if(index==5){
               index=1;
}
           else{
               index +=1;
}

                   showButton();
animate(-600);//点击右箭头-600
}
       prev.onclick=function (){
           if(index==1){
               index=5;
}
           else{
               index -= 1;
}

           showButton();
animate(-600);//点击左箭头600
}

       for(var i=0;i<buttons.length;i++){
           //原始值
buttons[i].onclick=function (){
               if(this.className=='on'){
                   return;
}

               var myIndex=parseInt(this.getAttribute('index'));
var offset=-600 *(myIndex -index);
animate(offset);
index=myIndex;
showButton();
}
       }
   container.onmouseover=stop;
container.onmouseout=play;


}
</script>
</body>
</html>

2 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
焦点图轮播特效
  • 参与学习       65216    人
  • 解答问题       611    个

通过本教程学习您将能掌握非常实用的焦点图轮播特效的制作过程

进入课程

完整的代码

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信