HTML ui模仿select

HTML ui模仿select

1 HTML

<!DOCTYPE html>
<html>

<head>
    <title>ul模拟select</title>
    <script type="text/javascript" src="./jquery/jquery-3.5.1.js"></script>
    <link rel="stylesheet" href="./css/demo.css">
</head>

<body>

    <div class="select-style-box">
        <input type="text" value="请选择..." readonly="readonly">
        <ul class="select_ul">
            <li>选项一</li>
            <li>选项二</li>
            <li>选项三</li>
            <li>选项四</li>
            <li>选项五</li>
            <li>选项六</li>
            <li>选项七</li>
            <li>选项八</li>
            <li>选项九</li>
        </ul>
    </div>
    
</body>

<script type="text/javascript" src="./demo.js"></script>

</html>

2 CSS3

.select-style-box {
    padding: 0;
    width: 300px;
    margin: 10px;
    font-size: 12px;
    position: relative;
}

.select-style-box input {
    width: 100%;
    height: 25px;
    display: block;
    cursor: pointer;
    line-height: 25px;
    overflow: hidden;
    padding-left: 10px;
    padding-right: 20px;
    border: 1px solid #ccc;
}

.select-style-box ul {
    left: 0px;
    top: 28px;
    margin: 0px;
    padding: 0px;
    display: none;
    width: 330px;
    z-index: 99999;
    overflow: hidden;
    position: absolute;
    background: #fff;
    background: #ebebeb;
    list-style-type: none;
    border: 1px solid #ccc;
}

.select-style-box ul li {
    width: 100%;
    margin: 0px;
    padding: 0px;
    height: 30px;
    display: block;
    cursor: pointer;
    overflow: hidden;
    line-height: 30px;
    padding-left: 5px;
    list-style-type: none;
}

.hover {
    background: #ccc;
}

3 JavaScript

$(document).ready(function() { 
    $(".select-style-box input").click(function() {  
        var input = $(this);  
        var ul = $(this).parent().find("ul");  
        if (ul.css("display") == "none") {   
            if (ul.height() > 200) {
                ul.css({ height: "200" + "px", "overflow-y": "scroll" });
            };   
            ul.show();
            ul.hover(function() {}, function() {
                ul.hide();
            });
            ul.find("li").click(function() {
                input.val($(this).text());
                ul.hide();
            }).hover(function() {
                $(this).addClass("hover");
            }, function() {
                $(this).removeClass("hover");
            });   
        } else {   
            ul.hide();   
        } 
    });
});

4 效果

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值