Jquery ul下拉框效果

来源:互联网 发布:冒险岛062版本大数据 编辑:程序博客网 时间:2024/06/16 18:45

 html select下拉框样式在网站开发时很难控制,网上找的模仿下拉框效果代码太长而且和Jquery有这严重的冲突,于是自己写了一个ul下拉框效果,代码很短也很简单。

 

  select.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="Css/DropDownStyle.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Js/DropDown.js" type="text/javascript"></script>
</head>
<body>
 <ul id="uboxstyle" style=" float:left; margin-top:6px; position:relative;">
              <li class="uboxstyle_Selected">查商品</li>
              <li class="uboxstyle5">查商品</li>
              <li class="uboxstyle5">查商家</li>
              <li class="uboxstyle5">查服务</li>
              <li class="uboxstyle4">查人员</li>
              </ul>
</body>
</html>

 

 

  DropDown.js

/// <reference path="../Scripts/jquery-1.4.1-vsdoc.js" />

 

//缺少鼠标离开,下拉框自动合拢写法

//搜索下列
$(function () {
    li_hide();
    //鼠标移到移开交替显示
    $("#uboxstyle>li").first().hover(function () {
        DropDownStyle($(this), "uboxstyle_Selecting");
    }, function () {
        DropDownStyle($(this), "uboxstyle_Selected");
    });

    //展开下拉控件的样式
    $("#uboxstyle>li").first().click(function () {
        DropDownStyle($(this), "uboxstyle_Select");
        li_ctrl();
    });

    //在下拉控件中操作
    $("#uboxstyle>li").each(function () {
        if ($("#uboxstyle>li").index($(this)) > 0) {
            //鼠标移动
            $(this).hover(function () {
                DropDownStyle($(this), "uboxstyle6");
            }, function () {
                DropDownStyle($(this), "uboxstyle5");
            });
            //鼠标点击
            $(this).click(function () {
                $("#uboxstyle>li:eq(0)").html($(this).html());
                li_hide();
            });
        }
    });

    //为控件设置样式
    function DropDownStyle($ctrl, styleName) {
        $ctrl.attr("className", styleName);
    }

    //显示
    function li_hide() {
        $("#uboxstyle>li:gt(0)").hide();
    }

    //控制显示隐藏
    function li_ctrl() {
        if ($("#uboxstyle>li:eq(1)").is(":hidden")) {
            li_show();
        }
        else {
            li_hide();
            DropDownStyle($("#uboxstyle>li:eq(0)"), "uboxstyle_Selecting");
        }
    }

    //隐藏
    function li_show() {
        $("#uboxstyle>li:gt(0)").show();
    }
});

 

  DropDownStyle.css

body
{
    padding:0px;
    margin:0px;
}

 

/* 选项卡搜索 开始*/
#uboxstyle{width:118px; height:30px}
.uboxstyle_Selected{
    display:block;
    color:#447004;
    width:98px;
    height:30px;
    padding:0px 10px;
    line-height:30px;
    background-color: transparent;
    background-image: url(../Images/home_search1.gif);
    background-repeat: no-repeat;
    background-position: 0 0;
     text-align:center;
     line-height:30px;
     cursor:hand;
}
.uboxstyle_Selecting{
    display:block;
    color:#447004;
    width:98px;
    height:30px;
    padding:0px 10px;
    line-height:30px;
    font-size:14px;
    background-color: transparent;
    background-image: url(../Images/home_search1.gif);
    background-repeat: no-repeat;
    background-position: 0px -30px;
     text-align:center;
     line-height:30px;
     cursor:hand;
}
.uboxstyle_Select{
    display:block;
    color:#447004;
    width:98px;
    padding:0 10px;
    line-height:30px;
    font-size:14px;
    background-image: url(../Images/home_search1.gif);
    background-repeat: no-repeat;
    background-position: 0 0;
     text-align:center;
     line-height:30px;
}

.uboxstyle5:hover {
    display:block;
    width:96px;
    height:30px;
    padding:0 10px;
    text-decoration:none;
    line-height:30px;
    font-size:14px;
    color:#FFF;
    background-color: #88C302;
    text-align:center;
    line-height:30px;
    cursor:hand;
    border-right-width: 1px;
    border-left-width: 1px;
    border-right-style: solid;
    border-left-style: solid;
    border-right-color: #d0d0d0;
    border-left-color: #d0d0d0;
}
.uboxstyle4{
    display:block;
    width:116px;
    height:30px;
    text-decoration:none;
    line-height:30px;
    font-size:14px;
    color:#447004;
    background-color: #ffffff;
    text-align:center;
    line-height:30px;
    cursor:hand;
    border-right-width: 1px;
    border-left-width: 1px;
    border-right-style: solid;
    border-left-style: solid;
    border-right-color: #d0d0d0;
    border-left-color: #d0d0d0;
    border-bottom-width: 1px;
    border-bottom-style: solid;
    border-bottom-color: #d0d0d0;
}

.uboxstyle4:hover {
    display:block;
    width:96px;
    height:30px;
    padding:0 10px;
    text-decoration:none;
    line-height:30px;
    font-size:14px;
    color:#FFF;
    background-color: #88C302;
    text-align:center;
    line-height:30px;
    cursor:hand;
    border-right-width: 1px;
    border-left-width: 1px;
    border-right-style: solid;
    border-left-style: solid;
    border-right-color: #d0d0d0;
    border-left-color: #d0d0d0;
}
.uboxstyle5{
    display:block;
    width:96px;
    height:30px;
    padding:0 10px;
    text-decoration:none;
    line-height:30px;
    font-size:14px;
    color:#447004;
    background-color: #ffffff;
    text-align:center;
    line-height:30px;
    cursor:hand;
    border-right-width: 1px;
    border-left-width: 1px;
    border-right-style: solid;
    border-left-style: solid;
    border-right-color: #d0d0d0;
    border-left-color: #d0d0d0;
}
.uboxstyle6{
    color:#ffffff;
    background-color: transparent;
    background-image: url(../Images/home_search1.gif);
    background-repeat: no-repeat;
        background-position: 0px -90px;
     width:118px;
     height:30px;
     text-align:center;
     line-height:32px;
     cursor:hand;
}
.uboxstyle7{
    background-image: url(../Images/home_search1.gif);
    background-repeat: no-repeat;
        background-position: 0px -120px;
    color:#fff
}

/* 选项卡搜索 结束*/

 

不足地方:

  1样式是从网上找加上一部分美工修改,但部分样式是没用的

  2当鼠标离开ul,下拉框无法合拢,试了许多方法也没法解决,等待高手帮助