JavaScript网页设计案例:(jquery.js)10例

jquery是一个javascript函数库,它封装了javascript常用的功能代码,提供一种简便的javascript设计模式,优化html文档操作、事件处理、动画设计和ajax交互。

1.下拉框3级联动

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script>
        var data = [
            {
                name: 'AAAAAAA',
                child: [
                    {
                        name: 'a1a1a1a1',
                        grandCd: [
                            { name: '111111111' },
                            { name: '222222222' }
                        ]
                    },
                    {
                        name: 'a2a2a2a2',
                        grandCd: [
                            { name: '3333333333' },
                            { name: '4444444444' }
                        ]
                    }
                ]
            },
            {
                name: 'BBBBBBBB',
                child: [
                    {
                        name: 'b1b1b1b1',
                        grandCd: [
                            { name: '666666666' },
                            { name: '777777777' }
                        ]
                    },
                    {
                        name: 'b2b2b2bb2',
                        grandCd: [
                            { name: '888888888' },
                            { name: '999999999' }
                        ]
                    }
                ]
            },
        ];
        //
        $(function () {
            bindlv1();
        });
        function bindlv1() {
            $("#lv1").append("<option value=''>--请选择--</option>");
            for (var i in data) {
                $("#lv1").append("<option value='" + data[i].name +"'>"+data[i].name+"</option>");
            }
            lv1change();
        }
        function lv1change() {
            $("#lv2").html("");
            $("#lv3").html("");
            var v1 = $("#lv1").val();
            if (v1) {
                var chids = data.find(c => c.name == v1).child
                $("#lv2").append("<option value=''>--请选择--</option>");
                for (var i in chids) {
                    $("#lv2").append("<option value='" + chids[i].name + "'>" + chids[i].name + "</option>");
                }
                lv2change();
            }
        }
        function lv2change() {
            $("#lv3").html("");
            var v1 = $("#lv1").val();
            var v2 = $("#lv2").val();
            if (v1 && v2) {
                var chids = data.find(c => c.name == v1).child
                var grands = chids.find(c => c.name == v2).grandCd
                $("#lv3").append("<option value=''>--请选择--</option>");
                for (var i in grands) {
                    $("#lv3").append("<option value='" + grands[i].name + "'>" + grands[i].name + "</option>");
                }
            }
        }
    </script>
</head>
<body>
    <select id="lv1" onchange="lv1change()" style="width:140px"></select>
    <select id="lv2" onchange="lv2change()" style="width:140px"></select>
    <select id="lv3" style="width:140px"></select>
</body>
</html>

2.table表隔行变色

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script>
        $(function () {
            setcolor()
        })
        function setcolor() {
            $("#mytable").find("tr").each(function (i) {
                if (i % 2 == 0) {
                    $(this).css("background", "#ddf");
                } else {
                    $(this).css("background", "#dfd");
                }
            });
        }
    </script>
</head>
<body>
    <table id="mytable" rules="all" style="width:800px;border-spacing:0">
        <tr><td>aaaaaaaa</td><td>aaaaaaaaaa</td><td>aaaaaa</td><td>aaaaaaaaaa</td></tr>
        <tr><td>bbbbbb</td><td>bbbbb</td><td>bbbb</td><td>bbbbbb</td></tr>
        <tr><td>ccccccccc</td><td>ccc</td><td>cccccccccc</td><td>ccccccccc</td></tr>
        <tr><td>dddd</td><td>dddddddddd</td><td>dddddd</td><td>dddddddddd</td></tr>
    </table>
</body>
</html>

3.全选、全不选

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script>
        function chekallchange() {
            if ($("#all").is(":checked")) {
                $(".all").prop("checked", true);
            } else {
                $(".all").prop("checked", false);
            }
        }
    </script>
</head>
<body>
    <input id="all" type="checkbox" onchange="chekallchange()" /><label for="all" style="font-weight:700">全选</label> <br />
    <input id="AAAAA" type="checkbox" class="all" /><label for="AAAAA">AAAAA</label> <br />
    <input id="BBBBB" type="checkbox" class="all" /><label for="BBBBB">BBBBB</label> <br />
    <input id="CCCCC" type="checkbox" class="all" /><label for="CCCCC">CCCCC</label> <br />
</body>
</html>

4.鼠标拖动进度条滑块

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }
        body {background:#fafafa;
        }
        .sbidLineBox {
            width:500px;
            height:400px;
            margin:100px auto;
            background:#fff;
            box-shadow:0 0 10px 0 #ccc;
            position:absolute;
            top:20px;
            left:20px;
            border:solid 10px #fff;
        }
        .sbidLine_L {
            width: 400px;
            height: 10px;
            background: #ecebeb;
            border: solid 1px #3ac15e;
            border-radius: 10px;
            position: absolute;
            top: 20px;
            left: 20px;
            z-index: 0;
            overflow: hidden;
            box-shadow: 0 0 10px 0 #ccc;
        }
        .sbidLine_Ct {
            width: 400px;
            height: 10px;
            background: #3ac15e;
            position: absolute;
            left: -400px;
            top: 0px;
        }
        .sbidLine_Dt {
            width: 50px;
            height: 50px;
            border-radius: 50px;
            line-height: 48px;
            position: absolute;
            left: 0;
            top: 0;
            background: #3ac15e;
            box-shadow: 0 0 5px 0 #ccc;
            text-align:center;
            color:#fff;
            cursor:pointer;
        }
            .sbidLine_Dt:hover {
                box-shadow: 0 0 8px 0 #3ac15e;
            }
    </style>
    <script src="js/jquery-1.11.2.min.js"></script>
    <script>
        $(function () {
            initsbid();
            sbidTo(85);
        });
        function sbidTo(v) {
            var w = $(".sbidLine_Ct").width();
            var tw = w * 85 / 100;
            var lw = -(w - tw) + 20;
            $(".sbidLine_Dt").html("«"+v+"»");
            $(".sbidLine_Ct").animate({ left: lw + "px" }, 600);
            $(".sbidLine_Dt").animate({ left: tw + "px" }, 600);
        }
        function initsbid() {
            var canMove = false;
            var ox, oy;
            $(".sbidLine_Dt").mousedown(function (e)//e鼠标事件
            {
                canMove = true;
                //$(this).css("cursor", "move");//改变鼠标指针的形状
                var offset = $(this).offset();//DIV在页面的位置
                ox = e.pageX - offset.left;//获得鼠标指针离DIV元素左边界的距离
                oy = e.pageY - offset.top;//获得鼠标指针离DIV元素上边界的距离

            });
            $(document).mousemove(function (ev)//绑定鼠标的移动事件,因为光标在DIV元素外面也要有效果,所以要用doucment的事件,而不用DIV元素的事件
            {
                var w = $(".sbidLine_Ct").width();
                if (canMove) {
                    $(".sbidLine_Dt").stop();//加上这个之后

                    var _x = ev.pageX - ox;//获得X轴方向移动的值
                    var _y = ev.pageY - oy;//获得Y轴方向移动的值
                    if (_x < 20) { _x = 20; }
                    if (_x > 20 + w) { _x = 20 + w;}
                    var ctl = w - (_x - 20);
                    if (_x != 0) {
                        $(".sbidLine_Dt").animate({ left: (_x - 20) + "px" }, 10);
                        $(".sbidLine_Ct").css("left", -ctl + "px");
                    }
                    var v = parseInt((_x - 20) * 100 / w);
                    $(".sbidLine_Dt").html("«" + v + "»");
                }
            }).mouseup(function () {
                canMove = false;
                //$(".sbidLine_Dt").css("cursor", "default");
            });
        }
    </script>
</head>
<body>
    <div class="sbidLineBox">
        <div class="sbidLine_L">
            <div class="sbidLine_Ct"></div>
        </div>
        <div class="sbidLine_Dt">«0»</div>
    </div>
</body>
</html>

5.图片轮播

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>       
        * {margin:0;padding:0;
        }
        #slidCt img {display:block;width:400px;height:250px;float:left;
        }
    </style>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script>
        $(function () {
            setInterval(function () {slidfuc()},2000);
        });
        function slidfuc() {
            var l = -parseInt($("#slidCt").css("margin-left").replace("px", ""));
            if (l / 400 > 1) { l = 0 }
            else { l += 400; }
            $("#slidCt").animate({marginLeft:-l+'px'},600);
        }
    </script>
</head>
<body>
    <div style="width: 400px; height: 250px; overflow: hidden; margin: 20px 0 0 20px;">
        <div id="slidCt" style="height: 250px; width: 1200px">
            <img src="https://www.uchuanbo.com/ueditor/php/upload/image/20210623/1624415469570968.jpeg" />
            <img src="https://img2.baidu.com/it/u=3129675182,215518988&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313"  />
            <img src="https://bpic.588ku.com/back_list_pic/22/12/16/3c5a45337fb53059789a4f19bfbd6f41.jpg%21/fh/300/quality/90/unsharp/true/compress/true"  />
        </div>
    </div>
</body>
</html>

6.回到顶部

<script src="js/jquery-3.5.1.min.js"></script>
    <script>
        function toTop() {
            $('body').animate({ scrollTop: 0 }, 50);
        }
    </script>

7.dialog弹窗

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script>
        function showDialog(tit) {
            $("#mDialog").css("display","block");
        }
        function closeDialog() {
            $("#mDialog").css("display", "none");
        }
    </script>
</head>
<body>
    <input type="button" value="弹窗" onclick="showDialog('标题')" />

    <div id="mDialog" style="width:800px;height:400px;border:solid 1px #ddd;border-radius:10px;margin:100px auto;background:#fff;display:none;">
        <div id="mDialogTit" style="width:100%;height:40px;background:#25aaf3;color:#f0f0f0;"></div>
        <div id="mDialogCot" style="text-align:center;">
            <br />
            <br />
            <br />
            <input type="button" value="关闭弹窗" onclick="closeDialog();" />
        </div>
    </div>
</body>
</html>

8.表单验证

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script>
        function checkdata() {
            var tel = $.trim($("#tel").val());
            if (ismobile(tel)) {
                alert("验证成功");
            } else {
                alert("验证失败");
            }
        }

        function ismobile(v) {//手机号
            if (/^1[3-9]\d{9}$/.test(v)) {
                return true;
            } else { return false; }
        }
    </script>
</head>
<body>
    <input id="tel" type="text" style="width:200px;padding:4px;" />
    <input type="button" value="提交" onclick="checkdata();" />
</body>
</html>

9.ajax请求

$.ajax({
            type: "GET",
            url: "https://*******.com/api/***",
            data: {},
            dataType: "json",
            //beforeSend: function (xhr) {
            //    xhr.setRequestHeader('Authorization', $.cookie("SZTtoken"));
            //},
            success: function (d) {
                console.log(d);
            },
            error: function (xhr, status, error) {
                console.log(xhr)
            }
        })

10.表单数据绑定

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script>
        $(function () { binddata() });

        function binddata() {
            $("#tel").val("测试测试测试测试");
            $("#ckTst").prop("checked", true);
            $("#msg").html("清风道长测试");
            $("#sTst").append("<option value='清风道长'>清风道长<option>");
            $("#sTst").append("<option value='白云道长'>白云道长<option>");
        }
    </script>
</head>
<body>
    <input id="tel" type="text" value="" style="width:200px;padding:4px" /><br />
    <input id="ckTst" type="checkbox" value="aaaa" /><label for="ckTst">测试</label><br />
    <span id="msg"></span><br />
    <select id="sTst"></select><br />
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风道长白云外

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值