简体   繁体   English

具有CSS过渡效果的jquery悬停功能仅在第二个悬停上有效

[英]jquery hover function with CSS transition effect ONLY working on second hover

Very odd. 很奇怪。 The below code ONLY works on second hover. 下面的代码仅适用于第二次悬停。 For instance the page is loaded, toggle works, but transition animation does not work until second hover each time, consistently. 例如,页面已加载,切换有效,但过渡动画直到每次两次都始终保持一致才起作用

jQuery : jQuery的

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

CSS : 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);
        }
    }
}

and. 和。

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

and. 和。

.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);
}

You need to make sure the element starts with the class contracted . 您需要确保元素以contracted类开头。 Before I added that it only worked on second hover for me as well. 在我补充说之前,它对我也只适用于第二次悬停。

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

<div class="greenBox contracted"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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