首页 > 其他分享 >jQuery-案例-广告显示和隐藏、案例-抽奖

jQuery-案例-广告显示和隐藏、案例-抽奖

时间:2023-02-20 10:11:49浏览次数:31  
标签:jQuery function 定时器 .. img prop 案例 抽奖 广告

jQuery-案例-广告显示

01-广告的自动显示与隐藏.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JQuery案例-广告的自动显示与隐藏</title>
    <style>
        #content{
            width: 100%;
            height: 500px;
        }
    </style>

    <!-- 引入jquery -->
    <script type="text/javascript" src="../js/jquery-3.6.1.min.js"></script>
    <script>
        /*
            需求:
                1.当页面加载完,3秒后。自动显示广告
                2.广告显示5秒后,自动消失。
            分析:
                1.使用定时器来完成。setTimeout(执行一次定时器)
                2.分析发现JQuery的显示和隐藏动画效果其实就是控制display
                3.使用 show/hide方法来完成广告的显示
         */

        // 入口函数,在页面加载完成之后,定义定时器,调用这两个方法
        $(function () {
           // 定义定时器,调用adShow方法 3秒后执行一次
           setTimeout(adShow,3000);
           // 定义定时器,调用adHide方法 8秒后执行一次
           setTimeout(adHide,8000);
        });

        // 显示广告
        function adShow(){
            // 获取广告div,调用显示方法
            $("#ad").show("show");
        }

        // 隐藏广告
        function adHide(){
            // 获取广告div,调用隐藏方法
            $("#ad").hide("show");
        }
    </script>
</head>
<body>
<!-- 整体的DIV -->
<div>
    <!-- 广告DIV -->
    <div id="ad" style="display: none;">
        <img style="width: 100%;" src="../img/man04.jpg">
    </div>

    <!-- 下方正文部分 -->
    <div id="content">
        正文部分
    </div>
</div>

</body>
</html>

jQuery-案例-抽奖

02-抽奖.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery案例-抽奖</title>
    <script type="text/javascript" src="../js/jquery-3.6.1.min.js"></script>

    <script language='javascript' type="text/javascript">
        /*
            分析:
                1.给开始按钮绑定单击事件
                    1.1定义循环定时器
                    1.2切换小相框;的src属性
                        定义数组,存放图片资源路径
                        生成随机数。数组索引
                2.给结束按钮绑定单击事件
                    1.1停止定时器
                    1.2给大相框设置src属性
         */
        var imgs = ["../img/man00.jpg",
                    "../img/man01.jpg",
                    "../img/man02.jpg",
                    "../img/man03.jpg",
                    "../img/man04.jpg",
                    "../img/man05.jpg",
                    "../img/man06.jpg",
                    ];
        var starId;// 开始定时器的id
        var index;// 随机角标
        $(function () {
            // 处理按钮是否可以使用的效果
            $("#startID").prop("disabled",false);
            $("stopID").prop("disabled",true);

            // 1.给开始按钮绑定单击事件
            $("#startID").click(function () {
                // 1.1定义循环定时器 20毫秒执行一次
                startId = setInterval(function () {
                   // 处理按钮可以使用的效果
                    $("#startID").prop("disabled",true);
                    $("stopID").prop("disabled",false);

                    // 1.2生成随机角标 0-6
                    index = Math.floor(Math.random() * 7);
                    // 1.3设置小相框的src属性
                    $("#img1ID").prop("src",imgs[index]);

                },20);
            });

            // 2.给结束按钮绑定单击事件
            $("#stopID").click(function () {
               // 处理按钮是否可以使用的效果
                $("#startID").prop("disabled",false);
                $("stopID").prop("disabled",true);

                // 1.1停止定时器
                clearInterval(starId);
                // 1.2给大相框设置src属性
                $("#img2ID").prop("src",imgs[index]).hide();
                // 显示1秒之后
                $("#img2ID").show(1000);
            });
        });
    </script>
</head>
<body>

<!-- 小相框 -->
<div style="border-style: dotted;width: 160px;height: 100px">
    <img id="img1ID" src="../img/man00.jpg" style="width: 160px;height: 100px">
</div>

<!-- 大相框 -->
<div
    style="border-style: double;width: 800px;height: 500px;position:absolute;left:500px;top:10px">
    <img id="img2ID" src="../img/man00.jpg" width="800px" height="500px">
</div>

<!-- 开始按钮 -->
<input
        id="startID"
        type="button"
        value="点击开始"
        style="width: 150px;height: 150px;font-size: 22px">

<!-- 停止按钮 -->
<input
        id="stopID"
        type="button"
        value="点击停止"
        style="width: 150px;height: 150px;font-size: 22px">
</body>
</html>

标签:jQuery,function,定时器,..,img,prop,案例,抽奖,广告
From: https://www.cnblogs.com/wsfj/p/17136391.html

相关文章