具有CSS过渡效果的jquery悬停功能仅适用于第二个悬停

时间:2015-11-16 21:00:59

标签: javascript jquery css css3

很奇怪。以下代码仅适用于第二次悬停。例如,页面已加载,切换有效,但过渡动画在每次第二次悬停时始终不起作用。

的jQuery

$('.greenBox').hover(function(){
    $(this).addClass('expanded');
    $(this).removeClass('contracted');
}, function(){
    $(this).removeClass('expanded');
    $(this).addClass('contracted');
});

CSS

.greenBox {
    background-image: url("../images/background_bubble.png");
    background-repeat: no-repeat;
    // transition:max-height .5s linear;
    // background: linear-gradient(#69af38, #8fba35);
    transition: .8s; 
    color: #FFF; 
    width: 310px; 
    height: 137px;
    position: relative; 
    overflow:hidden;
    z-index: 2;
    display: block;
    // -moz-transition: background-color 0.5s ease, height 0.5s ease, bottom 0.5s ease;
    // -o-transition: background-color 0.5s ease, height 0.5s ease, bottom 0.5s ease;
    // -webkit-transition: background-color 0.5s ease, height 0.5s ease, bottom 0.5s ease;  /* Chrome 1-25, Safari 3.2+ */
    // transition: background-color 0.5s ease, height 0.5s ease, bottom 0.5s ease;
    overflow: hidden;
    bottom: 7px;
    // -webkit-clip-path: polygon(0% 0%, 100% 0%, 100% 75%, 60% 75%, 49% 86%, 37% 75%, 0% 75%); 
    // clip-path: polygon(0% 0%, 100% 0%, 100% 75%, 60% 75%, 49% 86%, 37% 75%, 0% 75%);
    .noLink {
        color: #FFF;
        &:hover {
            cursor: auto;
            color: #FFF;
            background: linear-gradient(#69af38, #8fba35);
        }
    }
}

.contracted {
    // transition: 11s;
    max-height: 150px;
    overflow:hidden;
}

.expanded { 
    height: auto;
    min-height:150px;
    max-height:750px; 
    // transition: 11s;
    // margin-top:5px;
    // to make the box move up add back the bottom 300px
    // bottom: 300px;
    background: linear-gradient(#812990, #9e248e);
    -webkit-transition-timing-function: linear; 
    transition-timing-function: linear;
    background: -webkit-linear-gradient(#812990, #9E248E);
}

1 个答案:

答案 0 :(得分:2)

您需要确保元素以类contracted开头。在我添加它之前,它也仅适用于我的第二次悬停。

小提琴:http://jsfiddle.net/AtheistP3ace/czrqkorn/

<div class="greenBox contracted"></div>
相关问题