简体   繁体   中英

Show <li> dynamically using jQuery hover

I am trying to create a dynamic <ul> such that when I hover of a <li> (the class details), it dynamically creates another <li> (homework for that class) underneath it. I have this working.

However, when I leave the <li> I want the list of homework items to disappear. To accomplish this I decided to remove the <li> of homework items when I leave that <li> . The problem is that if I never hover into that homework list and simply just hover in and out of the class item details, it will not disappear. I am new to jQuery and so I may be missing something simple.

Before any hover

在此处输入图片说明

Hover into first class item

在此处输入图片说明

When I move the mouse out of the item, I want the items to disappear

在此处输入图片说明

But I want to maintain the functionality that when I hover into the homework list, it doesn't disappear:

在此处输入图片说明

And when I hover out of the homework list, it disappears (currently does this)

Here is my code:

jQuery.js

$(document).ready(function() {

    $(".item").hover(
        function(){
            $(this).find('.class-item').find('.top').css("background", "#D0D0D0");
            $(this).find('.class-item ').find('.bottom').css("background", "#B8B8B8");
            $(this).css("border-color", "white");

            $(this).css("margin-bottom", "0");

            $('#classes li:eq(' + $( "li" ).index(this) + ')').after('<li class="hw"><ul><li class="hw-item">Homework 1</li><li class="hw-item">Homework 2</li></ul></li>');

            var parent = $(this);

            $(".hw").hover(
                function(){
                }, 
                function(){
                    parent.css("margin-bottom", "2%");

                    $('#classes li:eq(' + ($( "li" ).index(parent) + 1) + ')').remove();
                }
            );

        }, 
        function(){

            $(this).find(' .class-item ').find(' .top ').css("background", "#B8B8B8");
            $(this).find(' .class-item ').find(' .bottom ').css("background", "#D0D0D0");
            $(this).css("border-color", "#888888");
        }
    );
});

style.css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: "Courier New", monospace;
    font-size: 24px;
}

body {
}

#left {
    float: left;
    height: 100%;
    width: 60%;
    background: black;
}

#right {
    float: left;
    height: 100%;
    width: 40%;
    padding: 1%;
    background: black;
}

#classes {
    height: 100%;
    overflow-y: scroll
}

#classes::-webkit-scrollbar {
    display: none;
}

img {
    width: 100%;
    height: 100%;
    max-height: 100%;
    max-width: 100%;
}

.item {
    height: 150px;
    margin-bottom: 2%;
    border: 5px solid #888888;
}

.class-item {
    height: 100%;
    width: 100%;
    display: inline-block;
}

.top {
    width: 100%;
    height: 50%;
    text-align: center;
    background: #B8B8B8;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.bottom {
    width: 100%;
    height: 50%;
    text-align: center;
    background: #D0D0D0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.hw {
    margin-bottom: 2%;
    padding: 1%;
    width: 100%;
    display: inline-block;
}

.hw-item{
    margin-bottom: 1%;
    padding: 0.25%;
    background: #D0D0D0;
    border: 3px solid white;
}

h3 {
    text-align: center;
}

index.html

<html>
    <head>
        <title>Homework</title>
        <link rel="stylesheet" href="style.css">
        <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script type="text/javascript" src="./jquery.js"></script>
    </head>

    <body>
        <div id="left">
            <img src="me.jpg"/>
        </div>

        <div id="right">
            <ul id="classes">
                <li class = "item">
                    <div class="class-item">
                        <div class="top">
                            <h3>Algorithm Analysis</h3>
                        </div>

                        <div class="bottom">
                            COMPSCI 704
                        </div>
                    </div>
                </li>

                <li class = "item">
                    <div class="class-item">
                        <div class="top">
                            <h3>Programming Language Concepts</h3>
                        </div>

                        <div class="bottom">
                            COMPSCI 431
                        </div>
                    </div>
                </li>
                <li class = "item">
                    <div class="class-item">
                        <div class="top">
                            <h3>Physics 1 with Calculus</h3>
                        </div>

                        <div class="bottom">
                            PHYSICS 209
                        </div>
                    </div>
                </li>
                <li class = "item">
                    <div class="class-item">
                        <div class="top">
                            <h3>Computer Ethics</h3>
                        </div>

                        <div class="bottom">
                            COMPSCI 395
                        </div>
                    </div>
                </li>
                <li class = "item">
                    <div class="class-item">
                        <div class="top">
                            <h3>Computer Architecture</h3>
                        </div>

                        <div class="bottom">
                            COMPSCI 458
                        </div>
                    </div>
                </li>
            </ul>
        </div>
    </body>
</html>

Thanks to the comments on my post, I was able to avoid the javascript and do it solely through css using:

.class-item {
    height: 150px;
    width: 100%;
    display: inline-block;
    border: 5px solid #888888;
}

.homework {
    padding: 1%;
    display: none;
}

.class-item:hover + .homework {
    display: block;
}

.homework:hover {
    display: block;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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