博主信息
博文 40
粉丝 0
评论 0
访问量 36546
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
幻灯片--2019-05-29
小人物的博客
原创
1030人浏览过

实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="static/css/bootstrap.css">
    <title>幻灯片/ carousel</title>
</head>
<style>
    .box {
        width:1920px;
        height:500px;
    }
    .box ul {
        padding: 0;
        margin: 0;
    }
    /*初始图片*/
    .box ul:first-of-type li {
        list-style: none;
        display:none;
    }
    .box ul:last-of-type {
        margin-top: -50px;
        text-align: center;
    }

    .box ul:last-of-type li {
        list-style: none;
        display: inline-block;
        width:30px;
        height:30px;
        line-height: 30px;
        margin: 0 5px;
        background-color: black;
        color : white;
        border-radius : 50%;
    }
    .box ul:last-of-type li:hover {
        background-color:  white;
        color: black;
        cursor: pointer;
    }

    .active {
        background-color:  white;
        color: black;
        font-size:2em;
      }

</style>
<body>
<div class="box">
    <ul>
        <li style="display: block" id="active"><img src="static/images/banner1.jpg" alt=""></li>
        <li><img src="static/images/banner2.jpg" alt=""></li>
        <li><img src="static/images/banner3.jpg" alt=""></li>
    </ul>

    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>

</div>
<script src="static/js/jquery-3.4.1.js"></script>
<script>
    var lis = $(".box ul:last-of-type li");
    var currentImg = $('#active img');
     // console.log(currentImg);

    lis.each(function(key) {
        lis[key].addEventListener('click',function () {
            currentImg[0].src ='static/images/banner' + parseInt(key + 1) +'.jpg';
            for (var j=0 ; j<lis.length;j++) {
                lis[j].classList.remove('active');
            }
            this.classList.add('active');
          },false);
    });

    // 用间歇式定时器, 每隔2秒随机切换图片
    setInterval(function () {
        var n = Math.floor(Math.random()*3)+1;
        currentImg[0].src = 'static/images/banner'+parseInt(n) + '.jpg';

        for (var k=0 ; k<lis.length;k++) {
            lis[k].classList.remove('active');
        }
        lis[n - 1].classList.add('active');
    }, 2000);


</script>

<script src="static/js/bootstrap.js"></script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学