简体   繁体   English

在下拉菜单中添加div

[英]Add divs in drop down menu

So I want to make a cool type of drop down menu using jquery, but I dont know how to add anything to the place were I want it. 因此,我想使用jquery制作一种很酷的下拉菜单,但是我不知道如何在需要的地方添加任何东西。 Basically, there is a button that you press which expands this other div, and then on double click it goes back up. 基本上,您可以按下一个按钮来展开另一个div,然后双击它会返回。 I want the other div to have other div links in it but I don't know how. 我希望其他div中具有其他div链接,但我不知道如何。

Here's my html: 这是我的html:

<div id = "one"></div>

<div id = "two"></div>

<div id = "three"></div>

<div id = "four"></div>

<div id = "five"></div>

<div id = "six"></div>

<div id = "one1"></div>

<div id = "two2"></div>

<div id = "three3"></div>

<div id = "four4"></div>

<div id = "five5"></div>

<div id = "six6"></div>

Here's my jquery (ignore the comments and arrows): 这是我的jquery(忽略注释和箭头):

$('#one1').click(function(){
    $('#one').animate({height: '200px'}, "fast");
    $('body').append('<div id = "tester"></div>');
});

$('#two2').click(function(){
    $('#two').animate({height: '200px'}, "fast");
});

$('#three3').click(function(){
    $('#three').animate({height: '200px'}, "fast");
});

$('#four4').click(function(){
    $('#four').animate({height: '200px'}, "fast");
});

$('#five5').click(function(){
    $('#five').animate({height: '200px'}, "fast");
});

$('#six6').click(function(){
    $('#six').animate({height: '200px'}, "fast");
});

You need something like this : 您需要这样的东西:

$('div').click(function(){
  $(this).animate({height: '200px'}, "fast");                                                                        
}).dblclick(function(){
  $(this).animate({height: '0px'}, "fast");         
});

Example : https://jsfiddle.net/DinoMyte/vxn3ozzb/6/ 范例: https : //jsfiddle.net/DinoMyte/vxn3ozzb/6/

I created a plain navBar which I think it is what you are trying to do. 我创建了一个简单的navBar,我认为这是您要尝试的操作。

This may help as a jumpstart to add the animation you want: check JsFiddle here 这可以帮助您快速添加所需的动画: 在此处检查JsFiddle

Html HTML

<nav>
    <ul>
        <li><a href="#">Home</a></li>
        <li class="dropdown"><a href="#">About</a>
            <ul>
                <li><a href="#">About us</a></li>
                <li><a href="#">About the company</a></li>
                <li><a href="#">More</a></li>
            </ul>
        </li>
        <li><a href="#">Contact</a></li>
    </ul>
</nav>

CSS 的CSS

nav > ul {
    padding: 0;
    margin: 0;
    float: left;
}

nav > ul > li {
    padding: 0;
    margin: 0;
    list-style: none;
    float: left;
    position: relative;
}

nav > ul > li.dropdown > ul {
    display: none;
    position: absolute;
    top: 100%;
    width: 200px;
    background: #BF0A64;
    margin: 0;
    padding: 0;
}

nav > ul > li.dropdown > ul > li {
    margin: 0;
    padding: 0;
    list-style: none;
}

nav > ul > li.dropdown > ul > li > a {
    display: block;
    padding: 10px;
    color: #FFF;
    text-decoration: none;
}

nav > ul > li.dropdown > ul > li > a:hover {
    background: #FFF;
    color: #BF0A64;
}

nav > ul > li.dropdown.active a {
    background: #BF0A64;
}

nav > ul > li.dropdown.active > ul {
    display: block;
}

nav > ul > li > a {
    padding: 20px;
    color: #FFF;
    background: #E80C7A;
    text-decoration: none;
    display: block;
}

nav > ul > li > a:hover {
    background: #BF0A64;
}

nav:after,
div:after {
    clear: both;
}

Javascript Java脚本

(function() {
    var dropdowns = document.getElementsByClassName('dropdown');

    function init() {

        for (var i = 0; i < dropdowns.length; i++) {
            var item = dropdowns[i];

            item.onclick = showOrHideSubmenu;
        }
    }

    function showOrHideSubmenu() {
        //    hideAllActives(this);
        this.classList.toggle('active');
    }


    function hideAllActives() {
        for (var i = 0; i < dropdowns.length; i++) {
            var item = dropdowns[i];
            if (hasClass(item, 'active')) {
                item.classList.toggle('active');
            }
        }
    }

    function hasClass(element, cls) {
        return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }

    init();

}());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM