【原生JS】自动渐变轮播

 

渐变主要是通过CSS3的动画实现。

只需给css中添加transtion动画时间加上JS设置指定图片透明度显示与消失即可实现渐变过程。

 

效果图:

 

HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="css/style3.css" />
        <script type="text/javascript" language="JavaScript" src="js/script3.js"></script>
    </head>
    <body>  
        <div id="box">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </body>
</html>

CSS:

    *{margin:0; padding:0;}
    #box{width:300px; height:165px; border:1px black solid; margin:10px auto;}
    #box ul{list-style:none; position:relative;}
    #box ul li{width:300px; height:165px; position:absolute; transition:all 1s; opacity:0;}
    #box ul li:nth-of-type(1){background:url(../img/img1.jpg); background-size:100%;}
    #box ul li:nth-of-type(2){background:url(../img/img2.jpg); background-size:100%;}
    #box ul li:nth-of-type(3){background:url(../img/img3.jpg); background-size:100%;}
    #box ul li:nth-of-type(4){background:url(../img/img4.jpg); background-size:100%;}

JS:

onload = function(){
    var li = document.getElementById('box').getElementsByTagName('li');
    var index = 0;     
    goto();
    function goto(){
        for(var i=0;i<li.length;i++){li[i].style.opacity = 0;}          
        if(index == li.length) index = 0;                                 
        li[index++].style.opacity = 1;                                    

    }
    setInterval(goto,3000);                                    
}

 

ps:只能帮你到这里了,按钮什么的没有....

转载于:https://www.cnblogs.com/GruntFish/p/6721955.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值