下拉菜单进入宽度网站

时间:2014-12-06 16:12:14

标签: javascript drop-down-menu

网站http://shop.jstudio-pro.com/

超出网站宽度的下拉菜单“Категория - 2”。

如何制作下拉菜单一直在网站的宽度范围内?

菜单结构示例:

<ul class="mg-main-menu">
    <li><a href="#"></a>
        <ul class="submenu">
            <li><a href="#"></a>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
            </li>
        </ul>
    </li>   

    <li><a href="#"></a>    
        <ul class="submenu">
            <li><a href="#"></a>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
            </li>
        </ul>
    </li>   

    <li><a href="#"></a>
        <ul class="submenu">
            <li><a href="#"></a>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
            </li>
        </ul>
    </li>

    <li><a href="#"></a>
        <ul class="submenu">
            <li><a href="#"></a>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
                <ul>
                    <li><a href="#"></a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

CSS:

.mg-main-menu {
    display: block;
    position: absolute;
    right: 0;
    top: 45px;
    max-width: 1000px;
    text-align: right;
}

.mg-main-menu li {
    display: inline-block;
    vertical-align: top;
    position: relative;
}

.mg-main-menu li a {
    display: block;
    padding: 5px 5px 10px 5px;
    color: #666;
    font-weight: 500;
    text-transform: uppercase;
    text-shadow: 0 1px 0 rgba(255,255,255,1);
}

.mg-main-menu li a:after {
    content: "";
    display: inline-block;
    vertical-align: middle;
    margin-left: 10px;
    width: 1px;
    height: 44px;
    background: url(../images-template/border-right-menu.png);
}

.mg-main-menu li:last-child a:after {
    background: none;
    margin-left: 0;
}

.mg-main-menu li a:hover, .active-menu {
    color: #9F84CA !important;
}

.mg-main-menu li .submenu {
    display: none;
    position: absolute;
    top: 56px;
    right: 0;
    min-width: 180px;
    text-align: left;
    padding: 10px;
    border: 1px solid #fff;
    border-top: none;
    background: #F5F5F5;
    background: -moz-linear-gradient(top, #F5F5F5 0%,#F8F8F8 100%);
    background: -webkit-linear-gradient(top, #F5F5F5 0%, #F8F8F8 100%);
    background: -o-linear-gradient(top, #F5F5F5 0%,#F8F8F8 100%);
    background: -ms-linear-gradient(top, #F5F5F5 0%, #F8F8F8 100%);
    box-shadow: 0 5px 5px rgba(0,0,0,0.4);
    -moz-box-shadow: 0 5px 5px rgba(0,0,0,0.4);
    -webkit-box-shadow: 0 5px 5px rgba(0,0,0,0.4); 
}

.mg-main-menu li .submenu:before {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    content: "";
    background: #D3D3D3;
    z-index: 1;
}

.mg-main-menu li:hover .submenu {
    display: block;
}

.mg-main-menu li .js_test:last-child {
    margin-right: 0 !important;
}

.mg-main-menu li .submenu li {
    display: block;
    width: 180px;
}

.mg-main-menu li .submenu.js_inline-block li {
    display: inline-block;
}

.mg-main-menu .js_arrow:after {
    content: "";
    display: none;
    position: relative;
    margin-left: 40%;
    margin-top: -12px;
    z-index: 9;
    width: 15px;
    height: 10px;
    background: url(../images-template/submenu-arrow.png);
}

.mg-main-menu .js_arrow:hover:after {
    display: block !important;
}

.mg-main-menu li .submenu li a {
    padding: 5px 5px 5px 10px;
    font-size: 13px;
    border: 1px solid transparent;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
}

.mg-main-menu li .submenu li a:after {
    display: none;
}

.mg-main-menu li .submenu li.js_float {
    display: block !important;
    float: left;
    margin-right: 5px;  
}

.mg-main-menu li .submenu li.js_float:nth-child(4n) {
    margin-right: 0;
}

.mg-main-menu li .submenu li ul li a {
    font-size: 12px;
    text-transform: inherit !important;
}

.mg-main-menu li .submenu li a:hover,
.mg-main-menu li .submenu li ul li a:hover,
.mg-main-menu li .submenu li a.active-menu {
    color: #fff !important;
    text-shadow: none;
    background-image: -webkit-gradient(
    linear,
    left top,
    left bottom,
    color-stop(0, #9251B0),
    color-stop(1, #C963CE)
    );
    background-image: -o-linear-gradient(bottom, #9251B0 0%, #C963CE 100%);
    background-image: -moz-linear-gradient(bottom, #9251B0 0%, #C963CE 100%);
    background-image: -webkit-linear-gradient(bottom, #9251B0 0%, #C963CE 100%);
    background-image: -ms-linear-gradient(bottom, #9251B0 0%, #C963CE 100%);
    background-image: linear-gradient(to bottom, #9251B0 0%, #C963CE 100%);
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-box-shadow: 0px 1px 2px 0px rgba(50, 50, 50, 0.5);
    -moz-box-shadow:    0px 1px 2px 0px rgba(50, 50, 50, 0.5);
    box-shadow:         0px 1px 2px 0px rgba(50, 50, 50, 0.5);
}

JavaScript的:

$('.mg-main-menu li .submenu').each(function () {
    $(this).css({
        width: (185 * Math.min($('li ul', this).length, 4)) + 'px'
    });
});

0 个答案:

没有答案