在悬停时使用CSS转换时闪烁的div

Emi*_*son 4 html css transform hover css3

我正在发布div推文(以及Facebook之类的)按钮.我希望它一旦悬停在div(按钮)上方就会向上移动,这样你就可以按下真正的推文按钮.我尝试了以下内容.

HTML:

<div class="tweet-bttn">Tweet</div>         
<div class="tweet-widget">
    <a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.tweet-bttn{
    position: relative;
    top: -30px;
    left: -10px;
    display:block;
    opacity: 1;
    width: 80px;
    padding: 10px 12px;
    margin:0px;
    z-index:3;}

.tweet-bttn:hover{
    -webkit-animation-name: UpTweet;
    -moz-animation-name: UpTweet;
    -o-animation-name: UpTweet;
    animation-name: UpTweet;
    -webkit-animation-duration:.5s;
    -moz-animation-duration:.5s;
    animation-duration:.5s;
    -webkit-transition: -webkit-transform 200ms ease-in-out;
    -moz-transition: -moz-transform 200ms ease-in-out;
    -o-transition: -o-transform 200ms ease-in-out;
    transition: transform 200ms ease-in-out;}

@-webkit-keyframes UpTweet {
    0% {
        -webkit-transform: translateY(0);
    }   
    80% {
        -webkit-transform: translateY(-55px);
    }
    90% {
        -webkit-transform: translateY(-47px);
    }
    100% {
        -webkit-transform: translateY(-50px);
    }
    ... and all other browser pre-fixes.
}
Run Code Online (Sandbox Code Playgroud)

我不确定出了什么问题.它看起来就像我悬停时一样,它会移动,但是如果我再移动光标一个像素,它就必须进行新的计算,这会导致闪烁.

Mr.*_*ien 6

当你可以简单地实现上述使用时,我不知道你为什么需要动画 transitions

诀窍是在父级悬停上移动子元素

演示

div {
    margin: 100px;
    position: relative;
    border: 1px solid #aaa;
    height: 30px;
}

div span {
    position: absolute;
    left: 0;
    width: 100px;
    background: #fff;
    top: 0;
    -moz-transition: all 1s;
    -webkit-transition: all 1s;
    transition: all 1s;
}

div span:nth-of-type(1) {
/* Just to be sure the element stays above the 
   content to be revealed */
    z-index: 1;
}

div:hover span:nth-of-type(1) { /* Move span on parent hover */
    top: -40px;
}
Run Code Online (Sandbox Code Playgroud)

说明:首先,我们包span的一个里面div是元素position: relative; ,后来我们使用transitionspan,这将有助于我们顺利的流动animation,现在我们使用position: absolute;left: 0;,这将堆栈元素彼此,比我们使用z-index,以确保第一要素覆盖第二个.

最后,我们移动第一个span,我们通过使用选择它nth-of-type(1),这只是嵌套在里面的第一个孩子div,我们分配top: -40px;哪个将在父母div悬停时转移.