最新消息:文章中包含代码时,请遵守代码高亮规范!

用div模拟select下拉菜单【原创】

HTML+CSS Cindy 120浏览 0评论
select下拉菜单在不同版本的浏览器下显示的样式会有所不同,特别是在ie低版本中,select自带的样式去不掉。以下是用div来模拟select下拉菜单具体代码:
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>div模拟select下拉菜单</title>
   <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
   <div class="wrap">
      <div class="friend-link">
         <div class="nice-select z-index33" name="nice-select">
            <div class="link">校内链接校内链接校内链接校内链接校内链接校内链接</div>
            <div class="select-bg"></div>
            <ul class="link-list">
               <li data-value="1"><a href="javascript:;">办公室办公室办公室办公室办公室办公室办公室办公室</a></li>
               <li data-value="2"><a href="javascript:;">组织部(党校)</a></li>
               <li data-value="3"><a href="javascript:;">宣传部(新闻中心)</a></li>
               <li data-value="4"><a href="javascript:;">统战部(港澳台事务办公室)</a></li>
               <li data-value="5"><a href="javascript:;">人事处</a></li>
               <li data-value="6"><a href="javascript:;">教务处(招办)</a></li>
            </ul>
         </div>
         <div class="clear"></div>
         <div class="nice-select z-index32" name="nice-select">
            <div class="link">常用链接</div>
            <div class="select-bg"></div>
            <ul class="link-list">
               <li data-value="1"><a href="javascript:;">国家环保部</a></li>
               <li data-value="2"><a href="javascript:;">教育部</a></li>
               <li data-value="3"><a href="javascript:;">省环保厅</a></li>
               <li data-value="4"><a href="javascript:;">卫生部</a></li>
            </ul>
         </div>
         <div class="nice-select z-index31" name="nice-select">
            <div class="link">常用链接</div>
            <div class="select-bg"></div>
            <ul class="link-list">
               <li data-value="1"><a href="javascript:;">世界自然基金会</a></li>
               <li data-value="2"><a href="javascript:;">中国固废网</a></li>
               <li data-value="3"><a href="javascript:;">中国环境科学研究院</a></li>
            </ul>
         </div>
      </div>
   </div>
   <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
   <script type="text/javascript">
      $(function() {
         // 点击下拉框
         $('.nice-select').click(function(){
            $(this).siblings('.nice-select').children('ul.link-list').hide();
            if ($(this).hasClass('link_two')) {
               $(this).removeClass('link_two');
               $(this).children('ul.link-list').hide();
            } else {
               $(this).addClass('link_two');
               $(this).children('ul.link-list').show();
            }
         })
         $('[name="nice-select"] li').click(function(e){
            var val = $(this).text();
            $(this).parents('[name="nice-select"]').find('.link').html(val);
            $('[name="nice-select"] ul').hide();
         });
      })
   </script>
</body>
</html>

css样式如下:

*{
    margin: 0;
    padding-top: 0;
}
a {
    text-decoration: none;
}
.friend-link {
    margin: 0 auto;
    margin-top: 100px;
    width: 320px;
    height: 174px;   
    background: #f3fff8;
}
.nice-select {
    position: relative;
    top: 20px;
    margin: 0 auto;
    margin-bottom: 20px;
    width: 240px;
    height: 30px;
}
.select-bg {
    position: absolute;
    top: 0;
    right: -2px;
    width: 30px;
    height: 30px;
    background: url(../images/select_bg.png) no-repeat right;
}
.link {
    display: block;
    padding:0 10px; 
    width: 190px;
    height: 28px;
    font-family: "微软雅黑";
    font-size: 14px;
    line-height: 30px;
    outline: none;
    z-index: -1;
    border:1px solid #e5e5e5;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    background:#ffffff;
}
.nice-select ul {
    display: none;
    position: absolute;
    top: 30px;
    padding: 0;
    width: 240px;
    max-height: 150px;
    overflow-y: auto;
    border: 1px solid #999;
    z-index: 9999;
    overflow: hidden;
    background-color: #fff;
}
.nice-select ul li{
    padding: 0 10px;
    height: 30px;
    line-height: 30px;
    z-index: 9999;
    list-style: none;
    cursor: pointer;
    overflow: hidden;
}
.nice-select ul li.on{
    background-color: #e0e0e0;
}
.nice-select ul li a{
    display: block;
    width: 214px;
    height: 30px;
    font-family: "微软雅黑";
    font-size: 14px;
    color: #000;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}
.nice-select ul {
    height:120px;
    overflow:auto;
}

.z-index33{
    z-index: 33;
}
.z-index32{
    z-index: 32;
}
.z-index31{
    z-index: 31;
}

以上代码可以兼容到ie7,链接超出的部分显示省略号,如果需要完全展示链接文字,可自定义最小宽度。

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/22884.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Cindy的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!