第二个位置绝对包装器的行为不像第一个

时间:2019-03-26 02:55:55

标签: html css bootstrap-4

我准备了一个CSS,在一种情况下,相同的样式可以按预期工作,而嵌套导航不再可用。

我的目标是在下拉菜单下进行显示导航。

该应用程序用于引导4,但是导航非常自定义

.nav-center-nestedfirst nested navsecond nested nav一起使用-但第二个相同的style(具有相同的html)则工作< / p>

$(document).ready(function () {
    const toggleSpeed = 50, toggleFade = "slow";

    $('div[data-v]').on('click', function (event) {
        if ($(event.target).prop('tagName') !== 'A') {
            var that = this,
                nav = $('#main-nav'),
                absoluteWrapper = nav.find('div[data-w="' + $(that).data('v') + '"]'),
                allAbsoluteWrappers = nav.find('div[data-w]'),
                list = {};

            if (!$(that).attr('was')) {
                if ($(that).attr('name') == 'main-nav') {
                    nav.find('div[data-p]').each(function () { var that = this; $(that).hide(); });
                    nav.find('div[name="main-nav"]').each(function () { var that = this; $(that).removeAttr('was'); });
                    $(that).attr('was', 'here');

                    if (absoluteWrapper.length != 0) {
                        allAbsoluteWrappers.removeClass('py-3');
                        absoluteWrapper.addClass('py-3');
                    } else {
                        allAbsoluteWrappers.removeClass('py-3');
                    }
                }
            } else {
                $(that).removeAttr('was');
                allAbsoluteWrappers.removeClass('py-3');
            }

            if ($(that).children('[id="nav-btn-childrens-' + $(that).data('v') + '"]').length != 0) {
                list = nav.find('div[id="nav-nested-childrens-' + $(that).data('v') + '"]').children();
            } else {
                list = nav.find('div[data-p=' + $(that).data('v') + ']');
            }

            if (list != 'undefined' || list != null || list.length != 0) {
                list.each(function () {
                    var that = this;
                    if ($(that).css('display') == "none") { // || $(that).attr('name', 'nav-dropdown')
                        $(that).show(toggleSpeed, function () { $(that).fadeIn(toggleFade); });
                        //if ($(that).attr('name', 'nav-dropdown')) {
                        //    $(that).css('display', 'inline')
                        //}
                    } else {
                        $(that).hide(toggleSpeed, function () { $(that).fadeOut(toggleFade); });
                        nav.find('div[id="nav-nested-childrens-' + $(that).data('v') + '"]').children().each(function () {
                            var that = this;
                            $(that).hide(toggleSpeed, function () { $(that).fadeOut(toggleFade); });
                        });
                    }
                });
            }
        }
    });
});
#main-nav a {
    z-index: 1000;
}

.logo-nav {
    position: absolute;
    top: 0;
    right: 95%;
    width: 75px;
    z-index: 999;
}
.nav-center {
    display: flex;
    justify-content: center;
}

.nav-center-nested {
    position: absolute !important;
    top: 100%;
    left: 50%;
    transform: translate(-50%, 0%);
    z-index: 998;
    width: 100%;
}

.nav-btn-clear {
    border-width: 0;
    border-style: none;
    border-color: transparent;
    border-image: none;
    background-color: transparent;
    outline: none !important;
    box-shadow: none !important;
}

.nav-margin {
    margin-left: 25%;
    margin-right: 25%;
}

.nav-padding {
    padding-left: 25%;
    padding-right: 25%;
}

.nav-btn:hover {
    background-color: rgba(255, 255, 255, 0.4); 
}
.nav-btn-margin {
    margin-top: -1rem;
    margin-bottom: -1rem;
    padding-top: 1rem;
    padding-bottom: 1rem;
}

.nav-btn-center {
    text-align: center;
}

.nav-btn-right {
    text-align: right;
}

.nav-btn-left {
    text-align: left;
    max-width: 50px;
}

.kuguar-sport-color {
    background-color: rgba(227, 30, 36, 1);
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="categories" class="col kuguar-sport-color no-padding no-margin">
<nav id="main-nav">
    <div class="row nav-margin p-3 text-center"><div name="main-nav" class="col nav-btn nav-btn-margin" data-v="138"><button class="nav-btn-clear text-light"> Aktualności</button></div><div name="main-nav" class="col nav-btn nav-btn-margin" data-v="139"><button class="nav-btn-clear text-light"> O nas</button></div><div name="main-nav" class="col nav-btn nav-btn-margin" data-v="140"><button class="nav-btn-clear text-light"> Rowery</button></div></div><div class="position-relative"><div data-w="139" name="nav-first" class="nav-center-nested row no-margin nav-padding text-center kuguar-sport-color"><div style="display: none;" class="nav-btn nav-btn-margin col" data-p="139" data-v="376"><a class="text-light" href="#"> o nas1</a><button id="nav-btn-childrens-376" class="nav-btn-clear text-light"><i style="color: white;" class="fa fa-chevron-circle-down" aria-hidden="true"></i></button></div><div class="position-relative"><div id="nav-nested-childrens-376" class="nav-center-nested row text-center"><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="376" data-v="379"><a class="text-light" href="#"> o nas11</a></div><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="376" data-v="380"><a class="text-light" href="#"> o nas12</a></div></div></div><div style="display: none;" class="nav-btn nav-btn-margin col" data-p="139" data-v="377"><a class="text-light" href="#"> o nas2</a></div><div style="display: none;" class="nav-btn nav-btn-margin col" data-p="139" data-v="378"><a class="text-light" href="#"> o nas3</a></div></div></div><div class="position-relative"><div data-w="140" name="nav-first" class="nav-center-nested row no-margin nav-padding text-center kuguar-sport-color"><div style="display: none;" class="nav-btn nav-btn-margin col" data-p="140" data-v="368"><a class="text-light" href="#"> Górskie</a><button id="nav-btn-childrens-368" class="nav-btn-clear text-light"><i style="color: white;" class="fa fa-chevron-circle-down" aria-hidden="true"></i></button></div><div class="position-relative"><div id="nav-nested-childrens-368" class="nav-center-nested row text-center"><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="368" data-v="371"><a class="text-light" href="#"> testgorskie</a></div><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="371" data-v="372"><a class="text-light" href="#"> testgorskie2</a></div><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="372" data-v="373"><a class="text-light" href="#"> testgorskie3</a></div><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="368" data-v="374"><a class="text-light" href="#"> testgorskie1-1</a></div><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="368" data-v="375"><a class="text-light" href="#"> testgorskie1-2</a></div></div></div><div style="display: none;" class="nav-btn nav-btn-margin col" data-p="140" data-v="369"><a class="text-light" href="#"> Miejskie</a></div><div style="display: none;" class="nav-btn nav-btn-margin col" data-p="140" data-v="370"><a class="text-light" href="#"> Dziecięce</a><button id="nav-btn-childrens-370" class="nav-btn-clear text-light"><i style="color: white;" class="fa fa-chevron-circle-down" aria-hidden="true"></i></button></div><div class="position-relative"><div id="nav-nested-childrens-370" class="nav-center-nested row text-center"><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="370" data-v="381"><a class="text-light" href="#"> Dziecięce1</a></div><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="370" data-v="382"><a class="text-light" href="#"> Dziecięce2</a></div><div style="display: none;" class="col nav-btn nav-btn nav-btn-margin" data-p="382" data-v="383"><a class="text-light" href="#"> Dziecięce21</a></div></div></div></div></div>
</nav>


</div>

问题 当我将kuguar-sport-color添加到嵌套导航中时,如何将嵌套导航从下拉列表的右移至底部-颜色剂量工作

谢谢!

编辑 AIM

1 个答案:

答案 0 :(得分:0)

我已将嵌套的子菜单移至nav-btn-margin内的相对位置。还为子菜单添加了inner-dropdown类。

尝试此解决方案并发表评论以寻求进一步的帮助。

$(document).ready(function() {
    const toggleSpeed = 50,
        toggleFade = "slow";

    $('div[data-v]').on('click', function(event) {
        if ($(event.target).prop('tagName') !== 'A') {
            var that = this,
                nav = $('#main-nav'),
                absoluteWrapper = nav.find('div[data-w="' + $(that).data('v') + '"]'),
                allAbsoluteWrappers = nav.find('div[data-w]'),
                list = {};

            if (!$(that).attr('was')) {
                if ($(that).attr('name') == 'main-nav') {
                    nav.find('div[data-p]').each(function() {
                        var that = this;
                        $(that).hide();
                    });
                    nav.find('div[name="main-nav"]').each(function() {
                        var that = this;
                        $(that).removeAttr('was');
                    });
                    $(that).attr('was', 'here');

                    if (absoluteWrapper.length != 0) {
                        allAbsoluteWrappers.removeClass('py-3');
                        absoluteWrapper.addClass('py-3');
                    } else {
                        allAbsoluteWrappers.removeClass('py-3');
                    }
                }
            } else {
                $(that).removeAttr('was');
                allAbsoluteWrappers.removeClass('py-3');
            }

            if ($(that).children('[id="nav-btn-childrens-' + $(that).data('v') + '"]').length != 0) {
                list = nav.find('div[id="nav-nested-childrens-' + $(that).data('v') + '"]').children();
            } else {
                list = nav.find('div[data-p=' + $(that).data('v') + ']');
            }

            if (list != 'undefined' || list != null || list.length != 0) {
                list.each(function() {
                    var that = this;
                    if ($(that).css('display') == "none") { // || $(that).attr('name', 'nav-dropdown')
                        $(that).show(toggleSpeed, function() {
                            $(that).fadeIn(toggleFade);
                        });
                        //if ($(that).attr('name', 'nav-dropdown')) {
                        //    $(that).css('display', 'inline')
                        //}
                    } else {
                        $(that).hide(toggleSpeed, function() {
                            $(that).fadeOut(toggleFade);
                        });
                        nav.find('div[id="nav-nested-childrens-' + $(that).data('v') + '"]').children().each(function() {
                            var that = this;
                            $(that).hide(toggleSpeed, function() {
                                $(that).fadeOut(toggleFade);
                            });
                        });
                    }
                });
            }
        }
    });
});
* {
    box-sizing: border-box;
}

#main-nav a {
    z-index: 1000;
}

.logo-nav {
    position: absolute;
    top: 0;
    right: 95%;
    width: 75px;
    z-index: 999;
}

.nav-center {
    display: flex;
    justify-content: center;
}

.nav-center-nested {
    position: absolute !important;
    top: 100%;
    left: 50%;
    transform: translate(-50%, 0%);
    z-index: 998;
    width: 100%;
}

.nav-btn-clear {
    border-width: 0;
    border-style: none;
    border-color: transparent;
    border-image: none;
    background-color: transparent;
    outline: none !important;
    box-shadow: none !important;
}

.nav-margin {
    margin-left: 25%;
    margin-right: 25%;
}

.nav-padding {
    padding-left: 25%;
    padding-right: 25%;
}

.nav-btn:hover {
    background-color: rgba(255, 255, 255, 0.4);
}

.nav-btn-margin {
    margin-top: -1rem;
    margin-bottom: -1rem;
    padding-top: 1rem;
    padding-bottom: 1rem;
}

.nav-btn-center {
    text-align: center;
}

.nav-btn-right {
    text-align: right;
}

.nav-btn-left {
    text-align: left;
    max-width: 50px;
}

.kuguar-sport-color {
    background-color: rgba(227, 30, 36, 1);
}

.inner-dropdown {
    position: absolute;
    top: 100%;
    z-index: 1000;
    left: 0;
    background: #e31e24;
    width: 100%;
    max-width: 180px;
    margin: 0 auto;
    right: 0;
}

.inner-dropdown a {
    display: block;
    padding: 4px 10px 5px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="categories" class="col kuguar-sport-color no-padding no-margin">
    <nav id="main-nav">
        <div class="row nav-margin p-3 text-center">
            <div name="main-nav" class="col nav-btn nav-btn-margin" data-v="138">
                <button class="nav-btn-clear text-light"> Aktualności</button>
            </div>
            <div name="main-nav" class="col nav-btn nav-btn-margin" data-v="139">
                <button class="nav-btn-clear text-light"> O nas</button>
            </div>
            <div name="main-nav" class="col nav-btn nav-btn-margin" data-v="140">
                <button class="nav-btn-clear text-light"> Rowery</button>
            </div>
        </div>
        <div class="position-relative">
            <div data-w="139" name="nav-first" class="nav-center-nested row no-margin nav-padding text-center kuguar-sport-color">
                <div style="display: none;" class="nav-btn nav-btn-margin col position-relative" data-p="139" data-v="376"><a class="text-light" href="#"> o nas1</a>
                    <button id="nav-btn-childrens-376" class="nav-btn-clear text-light"><i style="color: white;" class="fa fa-chevron-circle-down" aria-hidden="true"></i></button>
                    <div id="nav-nested-childrens-376" class="text-left inner-dropdown">
                        <div style="display: none;" class="" data-p="376" data-v="379"><a class="text-light" href="#"> o nas11</a></div>
                        <div style="display: none;" class="" data-p="376" data-v="380"><a class="text-light" href="#"> o nas12</a></div>
                    </div>
                </div>
                <div style="display: none;" class="nav-btn nav-btn-margin col position-relative" data-p="139" data-v="377"><a class="text-light" href="#"> Górskie</a>
                    <button id="nav-btn-childrens-377" class="nav-btn-clear text-light"><i style="color: white;" class="fa fa-chevron-circle-down" aria-hidden="true"></i></button>
                    <div id="nav-nested-childrens-377" class="inner-dropdown text-left">
                        <div style="display: none;" class="" data-p="377" data-v="371"><a class="text-light" href="#"> testgorskie</a></div>
                        <div style="display: none;" class="" data-p="377" data-v="372"><a class="text-light" href="#"> testgorskie2</a></div>
                        <div style="display: none;" class="" data-p="377" data-v="373"><a class="text-light" href="#"> testgorskie3</a></div>
                        <div style="display: none;" class="" data-p="377" data-v="374"><a class="text-light" href="#"> testgorskie1-1</a></div>
                        <div style="display: none;" class="" data-p="377" data-v="375"><a class="text-light" href="#"> testgorskie1-2</a></div>
                    </div>
                </div>
                <div style="display: none;" class="nav-btn nav-btn-margin col position-relative" data-p="139" data-v="378"><a class="text-light" href="#"> Dziecięce</a>
                    <button id="nav-btn-childrens-378" class="nav-btn-clear text-light"><i style="color: white;" class="fa fa-chevron-circle-down" aria-hidden="true"></i></button>
                    <div id="nav-nested-childrens-378" class="inner-dropdown text-left">
                        <div style="display: none;" class="" data-p="378" data-v="381"><a class="text-light" href="#"> Dziecięce1</a></div>
                        <div style="display: none;" class="" data-p="378" data-v="382"><a class="text-light" href="#"> Dziecięce2</a></div>
                        <div style="display: none;" class="" data-p="378" data-v="383"><a class="text-light" href="#"> Dziecięce21</a></div>
                    </div>
                </div>
            </div>
        </div>
    </nav>
</div>