自定义select 框

无聊时写的自定义select框,原生的select的文字内容是真的难居中,所以自己写了一个。缺点:option过长会导致整个html被拉长。所以不建议过长,因为现在本人无法知道怎么将框可以像原生的那样超越窗口显示。
下面源码:(下拉框图片没有传,在下方链接中)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }
        .selbody {
            width: 130px;
            height: 31px;
            margin-top: 20px;
            margin-left: 30px;
            background-color: #dfdfdf;
            text-align: center;
            position: relative;
            line-height: 31px;
            border: 0.5px solid gray;
        }
        .seled {

        }
        .arrow {
            position: absolute;
            cursor: pointer;
            top: 10px;
            right: 2px;
            height: 11px;
            width: 14px;
            background:url("triangle.png") center center no-repeat;
        }
        .selItems {
            width: 130px;
            display: none;
            margin-top: 0;
            position: absolute;
            top: 31px;
            left: -0.5px;
            background-color: rgba(238,238,238,0.9);
            border-left: 0.5px solid gray;
            border-right: 0.5px solid gray; 
            border-bottom: 0.5px solid gray;
        }
        .selItem {
            height: 23px;
            width: 130px;
            text-align: center;
            line-height: 23px;
        }
    </style>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $(".arrow").click(function(){
                $(".selItems").toggle();
                if($(".selItems").is(":visible")) {
                    var text = $(".seled").text();
                    $(".selItem").each(function(index){
                        if(text == $(this).text()) {
                            $(".selItem").css("background-color","");
                            $(this).css("background-color","blue");
                        }
                    });
                    var selItemsHeight = $(".selItems").height();
                    var windowScrollTop = $(window).scrollTop();
                    var selBodyOffsetTop = $(".selbody").offset().top;
                    var selBodyTop = selBodyOffsetTop + $(".selbody").height() - windowScrollTop;
                    if((selBodyTop + selItemsHeight > $(window).height()) && (selBodyOffsetTop - windowScrollTop > selItemsHeight))  {
                        $(".selItems").css("top",0 - $(".selItems").height()).css("border-top","0.5px solid gray");
                    } else {
                        $(".selItems").css("top",31);
                    }
                }
            });
            $(".selbody").mouseleave(function(){
                $(".selItems").hide();
            })
            $(".selItem").hover(function(){
                    $(this).siblings().css("background-color","");
                    $(this).css("background-color","blue").css("cursor","pointer");
            });
            $(".selItem").click(function(){
                    $(".seled").text($(this).text());
                    $(".selItems").hide();
            });
        })
    </script>
</head>
<body>
    <div class="selbody">
        <div class="seled">宁波市</div>
        <div class="arrow"></div>
        <div class="selItems">
            <div class="selItem">宁波市</div>
            <div class="selItem">慈溪市</div>
            <div class="selItem">鄞州区</div>
            <div class="selItem">象山县</div>
        </div>
    </div>
</body>
</html>

效果展示:

自定义select.gif

完整源码见:https://github.com/hzhqk/html/tree/master/custom_select

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,041评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • 晌午 太阳 火辣辣的太阳,仿佛要把大地烤熟,偶尔有一丝风吹过来,但整个风都是热的,吹在人身上,非但一点也不凉快,反...
    飞城阅读 910评论 2 1
  • 因“霾锁中原”,昨天节后上班第一天雾霾继续,并且开启了历史首次:中央气象局发布史上首个霾橙色预警。 图1 各地早已...
    lezhibo阅读 793评论 0 0
  • 想象中的田园 田园设计,为您带来不一样的家居享受。 田园装修设计以纯净的象牙白为色调,附以幽雅的实木雕花, 宁静中...
    一简书生阅读 1,605评论 0 0