简体   繁体   English

动态添加和删除多个div

[英]adding and removing multiple div dynamically

Im a bit stuck. 我有点卡住了。 this is what it suppose to do. 这就是它应该做的。 When you mouse over a menu item it slides out the next menu and adds a new vertical div which must be 5px wide in between the current div and the next but i can't add and remove the div. 当您将鼠标悬停在菜单项上时,它将滑出下一个菜单并添加一个新的垂直div,该垂直div必须在当前div和下一个div之间为5px宽,但是我无法添加和删除div。 i have a condition that checks that the div is not the last on I've commented it in my code if you can help it would be appreciated. 我有一个条件来检查div不是最后一个,如果您能帮助的话,我已经在我的代码中对其进行了评论。 here is a sample jsFiddle 这是一个示例jsFiddle

    $(document).ready(function () {
        $('.menu-item').mouseenter(function () {
            var curr = $(this).closest('.container');
            var next = curr.next('.container');

            next.animate({ 'left': curr.position().left + curr.width() + 5 });

            if(curr.index() < ($('.container').length - 1)){
                //here i need to add and remove a </div class="spacer"></div> to the right side
                of the current(var curr) div
            }

            var $index = curr.index() + 1;
            $('.container:nth-child(' + $index + ')').nextAll().each(function () {

                $(this).animate({ 'left': curr.position().left + curr.width() + 5 });

            });

        });
    });

.spacer
    {
        background-color:Red; width:5px; height:200px; position:absolute; z-index:1000;
    }

<table border="2" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            <div id="menu" style="background-color:Black; width:2000px; height:300px;top:5px; left:50px  ">
                <div class="container" id="1" style="left:0; width:200px; height:220px; z-index:999; position:absolute; background-color:Aqua">
                    <div class="menu-item">Menu Item 1</div>
                    <div class="menu-item">Menu Item 2</div>
                    <div class="menu-item">Menu Item 3</div>
                </div>
                <div class="container" id="2" style="left:0; width:200px; height:300px; z-index:998; position:absolute; background-color:Blue">
                    <div class="menu-item">Menu Item 4</div>
                    <div class="menu-item">Menu Item 5</div>
                    <div class="menu-item">Menu Item 6</div>
                </div>
                <div class="container" id="3" style="left:0; width:200px; height:400px; z-index:997; position:absolute; background-color:Fuchsia">
                    <div class="menu-item">Menu Item 7</div>
                    <div class="menu-item">Menu Item 8</div>
                    <div class="menu-item">Menu Item 9</div>
                </div>
                <div class="container" id="4" style="left:0; width:200px; height:500px; z-index:996; position:absolute; background-color:Green; float:left">
                    <div class="menu-item">Menu Item 10</div>
                    <div class="menu-item">Menu Item 11</div>
                    <div class="menu-item">Menu Item 12</div>
                </div>
                <div class="container" id="5" style="left:0; width:200px; height:600px; z-index:995; position:absolute; background-color:Lime; float:left">
                    <div class="menu-item">Menu Item 10</div>
                    <div class="menu-item">Menu Item 11</div>
                    <div class="menu-item">Menu Item 12</div>
                </div>
            </div>

        </td>
    </tr>
</table>

Sounds like you need accordion. 听起来您需要手风琴。 Why not just use one of the million plugins available for jQuery? 为什么不只使用可用于jQuery的数百万个插件之一?

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

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