鱼C论坛

 找回密码
 立即注册
查看: 2409|回复: 7

[已解决]自动轮播图問题

[复制链接]
发表于 2019-6-19 18:21:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下列轮播图代码可以运行,页面加载之后就自动更换图片,
按stop能停下来,
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>LBT04_2</title>
<style type="text/css">
.wrap{width:400px;
height:300px;
position:relative;}

.list{width:400px;
height:300px;
list-style:none;
padding-left:0;
position:relative;}

.item{position:absolute;
width:100%;
height:100%;
color:white;
font-size:50px;}

.item:nth-child(1){background-color:none;}
.item:nth-child(2){background-color:red;}
.item:nth-child(3){background-color:yellow;}
.item:nth-child(4){background-color:blue;}
.item:nth-child(5){background-color:green;}

.btn{width:50px;
height:50px;
position:absolute;
top:100px;
z-index:100;}
#stoply{left:0;}
#startly{right:0;}

.item.active{z-index:10;}
</style>
</head>
<body>
<div class="wrap">
  <ul class="list">
    <img class="item" src="LDY01.jpg" alt=""/>
        <img class="item" src="LDY02.jpg" alt=""/>
        <img class="item" src="LDY03.jpg" alt=""/>
        <img class="item" src="LDY04.jpg" alt=""/>
        <img class="item" src="LDY05.jpg" alt=""/>
  </ul>  

<button type="button" class="btn" id="stoply" onclick="StopPlay()">stop</button>
<button type="button" class="btn" id="startly" onclick="setInterval()">start</button>   
   
</div>       
<script type="text/javascript">
    var items=document.getElementsByClassName('item');      
    var startlyBtn=document.getElementById('startly');   
    var index=0;//表示第几张图片显示,有active类名   
        var clearActive = function(){
            for(var i=0; i<items.length; i++){
                   items[i].className='item';
                }
          }
          
        var goIndex=function(){         
          clearActive();                            
              items[index].className='item active';
        }
       
        var startly = function(){
           if(index < 4){
             index ++;
           }else{
              index=0;
           }                     
           goIndex();
        }
               
        startlyBtn.addEventListener('click',function(){
           startly();
        })
               
        var autop=setInterval(function(){
             startly();
        },1000)
               
        function StopPlay() {
    clearInterval(autop);
        }
       
</script>
</body>
</html>
最佳答案
2019-6-20 10:48:41
1、鱼油需求:
Snip20190620_11.png


即按 start 就自动播放图片。

2、将定时器加到 startly() 中,用按键 onclick 方法触发即可:
Jun-20-2019 10-48-00.gif

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  5.     <title>LBT04_2</title>
  6.     <style type="text/css">
  7.         .wrap{width:400px;
  8.             height:300px;
  9.             position:relative;}

  10.         .list{width:400px;
  11.             height:300px;
  12.             list-style:none;
  13.             padding-left:0;
  14.             position:relative;}

  15.         .item{position:absolute;
  16.             width:100%;
  17.             height:100%;
  18.             color:white;
  19.             font-size:50px;}

  20.         .item:nth-child(1){background-color:none;}
  21.         .item:nth-child(2){background-color:red;}
  22.         .item:nth-child(3){background-color:yellow;}
  23.         .item:nth-child(4){background-color:blue;}
  24.         .item:nth-child(5){background-color:green;}

  25.         .btn{width:50px;
  26.             height:50px;
  27.             position:absolute;
  28.             top:100px;
  29.             z-index:100;}
  30.         #stoply{left:0;}
  31.         #startly{right:0;}

  32.         .item.active{z-index:10;}
  33.     </style>
  34. </head>
  35. <body>
  36. <div class="wrap">
  37.     <ul class="list">
  38.         <img class="item" src="LDY01.jpg" alt=""/>
  39.         <img class="item" src="LDY02.jpg" alt=""/>
  40.         <img class="item" src="LDY03.jpg" alt=""/>
  41.         <img class="item" src="LDY04.jpg" alt=""/>
  42.         <img class="item" src="LDY05.jpg" alt=""/>
  43.     </ul>

  44.     <button type="button" class="btn" id="stoply" onclick="StopPlay()">stop</button>
  45.     <button type="button" class="btn" id="startly" onclick="startly()">start</button>

  46. </div>
  47. <script type="text/javascript">



  48.     var items=document.getElementsByClassName('item');
  49.     var startlyBtn=document.getElementById('startly');
  50.     var index=0;//表示第几张图片显示,有active类名
  51.     var clearActive = function(){
  52.         for(var i=0; i<items.length; i++){
  53.             items[i].className='item';
  54.         }
  55.     }

  56.     var goIndex=function(){
  57.         clearActive();
  58.         items[index].className='item active';
  59.     }

  60.     var startly = function(){
  61.         var autop=setInterval(function(){
  62.             if(index < 4){
  63.             index ++;
  64.         }else{
  65.             index=0;
  66.         }
  67.         goIndex();
  68.         },1000)
  69.     }

  70.     startlyBtn.addEventListener('click',function(){
  71.         startly();
  72.     })

  73.     // var autop=setInterval(function(){
  74.     //     startly();
  75.     // },1000)

  76.     function StopPlay() {
  77.         clearInterval(autop);
  78.     }

  79. </script>
  80. </body>
  81. </html>
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-6-19 18:23:42 | 显示全部楼层
我现在要求按start就才自动播放图片,刚加载页面时不会自动播放图片。
怎么改代码?请大侠指导,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-19 18:24:31 | 显示全部楼层
所以这位朋友你想问什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-19 18:27:25 | 显示全部楼层
要求按start就才自动播放图片,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-19 18:29:52 | 显示全部楼层
设定一个布尔类型的变量
默认为False
当为Flase的时候不进行轮播。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-19 18:38:26 | 显示全部楼层
可以把代码展示一下吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-19 18:57:52 | 显示全部楼层
本帖最后由 newu 于 2019-6-19 19:09 编辑

这个js一停止就不能自动播了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-20 10:48:41 | 显示全部楼层    本楼为最佳答案   
1、鱼油需求:
Snip20190620_11.png


即按 start 就自动播放图片。

2、将定时器加到 startly() 中,用按键 onclick 方法触发即可:
Jun-20-2019 10-48-00.gif

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  5.     <title>LBT04_2</title>
  6.     <style type="text/css">
  7.         .wrap{width:400px;
  8.             height:300px;
  9.             position:relative;}

  10.         .list{width:400px;
  11.             height:300px;
  12.             list-style:none;
  13.             padding-left:0;
  14.             position:relative;}

  15.         .item{position:absolute;
  16.             width:100%;
  17.             height:100%;
  18.             color:white;
  19.             font-size:50px;}

  20.         .item:nth-child(1){background-color:none;}
  21.         .item:nth-child(2){background-color:red;}
  22.         .item:nth-child(3){background-color:yellow;}
  23.         .item:nth-child(4){background-color:blue;}
  24.         .item:nth-child(5){background-color:green;}

  25.         .btn{width:50px;
  26.             height:50px;
  27.             position:absolute;
  28.             top:100px;
  29.             z-index:100;}
  30.         #stoply{left:0;}
  31.         #startly{right:0;}

  32.         .item.active{z-index:10;}
  33.     </style>
  34. </head>
  35. <body>
  36. <div class="wrap">
  37.     <ul class="list">
  38.         <img class="item" src="LDY01.jpg" alt=""/>
  39.         <img class="item" src="LDY02.jpg" alt=""/>
  40.         <img class="item" src="LDY03.jpg" alt=""/>
  41.         <img class="item" src="LDY04.jpg" alt=""/>
  42.         <img class="item" src="LDY05.jpg" alt=""/>
  43.     </ul>

  44.     <button type="button" class="btn" id="stoply" onclick="StopPlay()">stop</button>
  45.     <button type="button" class="btn" id="startly" onclick="startly()">start</button>

  46. </div>
  47. <script type="text/javascript">



  48.     var items=document.getElementsByClassName('item');
  49.     var startlyBtn=document.getElementById('startly');
  50.     var index=0;//表示第几张图片显示,有active类名
  51.     var clearActive = function(){
  52.         for(var i=0; i<items.length; i++){
  53.             items[i].className='item';
  54.         }
  55.     }

  56.     var goIndex=function(){
  57.         clearActive();
  58.         items[index].className='item active';
  59.     }

  60.     var startly = function(){
  61.         var autop=setInterval(function(){
  62.             if(index < 4){
  63.             index ++;
  64.         }else{
  65.             index=0;
  66.         }
  67.         goIndex();
  68.         },1000)
  69.     }

  70.     startlyBtn.addEventListener('click',function(){
  71.         startly();
  72.     })

  73.     // var autop=setInterval(function(){
  74.     //     startly();
  75.     // },1000)

  76.     function StopPlay() {
  77.         clearInterval(autop);
  78.     }

  79. </script>
  80. </body>
  81. </html>
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 16:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表