简体   繁体   中英

Convert jquery animation to CSS3

HTML:

 <div id="slick-slidetoggle">wxyz</div>           

            <div id="slickbox" >abcd</div>​

JS

// hides the slickbox as soon as the DOM is ready (a little sooner that page load)

         var hoverVariable=false;
        var hoverVariable2=false;

        $('#slickbox').hide();
        $('#slick-slidetoggle').mouseover(function() {
            hoverVariable2=true;
            $('#slickbox').slideToggle(600);
            return false;
        })
        $('#slick-slidetoggle').mouseleave(function() {
            hoverVariable2=false;
            setTimeout(function (){
            if(!hoverVariable && !hoverVariable2){
            $('#slickbox').slideToggle(600);
            return false;}
         }, 1000);
        })
        $('#slickbox').mouseleave(function() {                    
            hoverVariable=false;
            setTimeout(function (){
            if(!hoverVariable && !hoverVariable2){                    
            $('#slickbox').slideToggle(600);
            return false;}
            return false;
           }, 1000); 
        })
        $('#slickbox').mouseover(function() {
             hoverVariable2=false;

            hoverVariable=true;

        })​

CSS

#slickbox {
    background: black;
    width:100px;
    height: 135px;
    display: none; 
    cursor:pointer;
    color:white;
}
#slick-slidetoggle{
 background: yellow;
    width:100px;
    height: 135px;
    cursor:pointer;
    color:black;

}

Now the above functionality is what I want to achieve using purely CSS, which is when I hover over the "wxyz" button "abcd" button should come down and stay visible even is mouse is moved away from "wxyz" for 3 secs.

I tried transition delay with display property but apparently that doesn't work on display property, then I tried position:absolute & visibility & transition delay of visibility, but then the appearance of button got delayed by 3 secs not the hidnig. I want the "abcd" button to hide after 3 secs of moving the button away from "wxyz" using only CSS or CSS3

Here is an Example ( Code here )

(I have written only -webkit , but you could add the other prefixes)

#test2 {
    position:absolute;
    z-index:1;
    background: black;
    width:100px;
    height: 135px;
    opacity: 0; 
    cursor:pointer;
    color:white;
    opacity:0;
    -webkit-animation-duration: 600ms;
    -webkit-animation-timing-function: ease-in-out;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-fill-mode: both;
}

#test {
    position:absolute;
    z-index:2;
    background: yellow;
    width:100px;
    height: 135px;
    cursor:pointer;
    color:black;

}
.container {
  position:relative;
}

.container:hover #test2 {
  opacity:1;
  -webkit-animation-name: slideDown;
}

.container:not(:hover) > #test2 {
  -webkit-animation-delay:1000ms;
  -webkit-animation-name: slideUp;
  opacity:1;
}

@-webkit-keyframes slideDown {
    0% {
        -webkit-transform: translateY(0);
    } 
    100% {
        -webkit-transform: translateY(135px);
    }
}

@-webkit-keyframes slideUp {
    0% {
        -webkit-transform: translateY(135px);
    } 
    100% {
        -webkit-transform: translateY(0);
    }
}

Use the transition to do it as below:

    <head>
        <style>
            #outer {
                width: 100px;
                height: 50px;
                background-color: green;
                position: relative;
            }
            #innerOne {
                width: 100px;
                height: 50px;
                background-color: blue;
            }
            #innerTwo {
                width: 100px;
                height: 50px;
                background-color: red;
                position: absolute;
                top: -150px;
                left: 100px;

            }

            #outer:hover #innerTwo {
                top: 0px;

                -webkit-transition: top 2s ease-out;
                -moz-transition: top 2s ease-out;
                -o-transition: top 2s ease-out;
                transition: top 2s ease-out;
            }
            #innerTwo:not(hover) {
                -webkit-transition: top 1s ease-in 3s;
                -moz-transition: top 1s ease-in 3s;
                -o-transition: top 1s ease-in 3s;
                transition: top 1s ease-in 3s;
            }
        </style>
    </head>
    <body>
        <div id="outer">
            <div id="innerOne">wxyz</div>
            <div id="innerTwo">abcd</div>
        </div>
    </body>
</html>

Here is a cross browser solution:

Tested on OPERA - SAFARI - CHROME - MAXTHON - FIREFOX

HTML:

<div class="container">
    <div id="test">wxyz</div>           
    <div id="test2" >abcd</div>
</div>

CSS:

#test {
    width:100px;
    height:100px;
    background:yellow;
    position:relative;
    z-index:2;
}
#test2 {
    top:-100px;
    width:100px;
    height:100px;
    background:black;
    color:white;
    position:relative;
    z-index:1;
}
.container:hover #test2 {
    top:0px;
    transition-property:top;
    transition-duration:0.2s;
    transition-timing-function: linear;
    /* Firefox 4 */
    -moz-transition-property:top;
    -moz-transition-duration:0.2s;
    -moz-transition-timing-function:linear;
    /* Safari and Chrome */
    -webkit-transition-property:top;
    -webkit-transition-duration:0.2s;
    -webkit-transition-timing-function:linear;
    /* Opera */
    -o-transition-property:top;
    -o-transition-duration:0.2s;
    -o-transition-timing-function:linear;
    /* IE */
    -ms-transition-property:top;
    -ms-transition-duration:0.2s;
    -ms-transition-timing-function:linear;
}
.container:not(:hover) #test2 {
    top:-100px;
    transition-property:top;
    transition-duration:0.2s;
    transition-timing-function: linear;
    transition-delay: 3s;
    /* Firefox 4 */
    -moz-transition-property:top;
    -moz-transition-duration:0.2s;
    -moz-transition-timing-function:linear;
    -moz-transition-delay:3s;
    /* Safari and Chrome */
    -webkit-transition-property:top;
    -webkit-transition-duration:0.2s;
    -webkit-transition-timing-function:linear;
    -webkit-transition-delay:3s;
    /* Opera */
    -o-transition-property:top;
    -o-transition-duration:0.2s;
    -o-transition-timing-function:linear;
    -o-transition-delay:3s;
    /* IE */
    -ms-transition-property:top;
    -ms-transition-duration:0.2s;
    -ms-transition-timing-function:linear;
    -ms-transition-delay:3s;
}

Fiddle: http://jsfiddle.net/BerkerYuceer/2gVLX/

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