Problem with the display

0

Good here I leave my next code:

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Ejercicio 4.1</title>
    <script
    src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
    integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
    crossorigin="anonymous"></script>
    <style>
        .oculto {
           /* background-color: blue; */
           display: none;
        }

    /*    div {
            width: 100px;
            border: 2px solid black;
            background-color: yellowgreen;
        } */

    </style>
</head>
<body>
    <ul>
        <li>Lista 1:            
            <ul>
                <li>
                    <p>Tortilla</p>
                </li>
                <li>
                    <p>Jamón</p>
                </li>
                <li>
                    <p>Queso</p>
                </li>
            </ul>
        </li>
           <li>Lista 2:
                <ul>
                    <li>
                        <p>Coca Cola</p>
                    </li>
                    <li>
                        <p>Leche</p>
                    </li>
                    <li>
                        <p>Té</p>
                    </li>
                </ul>
           </li>             
    </ul>

 <!--   <div>Aprietame! Duro</div> -->

    <script>
        $("ul li:first-child ul li:first-child").hover(function(){
            $(this).toggleClass("oculto");
        });

    /*    $("div").hover(function(){
            $("ul li:first-child ul li:first-child").addClass("oculto");
        }, function() {
            $("ul li:first-child ul li:first-child").removeClass("oculto");
        }); */

    </script>   
</body>
</html>

How to correct the display effect, appears and disappears very fast. I know that the display can not be controlled by the transition property. Any advice? Thanks

    
asked by Ronald Sanchez 14.01.2018 в 19:14
source

1 answer

2

What you could do is play with size and opacity to be able to give the item a good output effect.

Notice that the height property must be set initially for the effect to be visible, otherwise it will be very sudden despite the transition property.

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Ejercicio 4.1</title>
    <script
    src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
    integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
    crossorigin="anonymous"></script>
    <style>
        p { margin: 0; }
        ul ul li {
           cursor: pointer;
           height: 32px;
           margin-bottom: 8px;
           overflow: hidden;
           transition: 1s ease;
        }
        .oculto {
           height: 0;
           opacity: 0;
        }
    </style>
</head>
<body>
    <ul>
        <li>Lista 1:            
            <ul>
                <li>
                    <p>Tortilla</p>
                </li>
                <li>
                    <p>Jamón</p>
                </li>
                <li>
                    <p>Queso</p>
                </li>
            </ul>
        </li>
        <li>Lista 2:
             <ul>
                 <li>
                     <p>Coca Cola</p>
                 </li>
                 <li>
                     <p>Leche</p>
                 </li>
                 <li>
                     <p>Té</p>
                 </li>
             </ul>
        </li>             
    </ul>

    <script>
        $("ul ul li").click(function(){
            $(this).toggleClass("oculto");
        });
    </script>   
</body>
</html>
  

Note: Change the event to the click (in this case the example with this event is better appreciated)

    
answered by 14.01.2018 / 23:53
source