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

标签 javascript jquery css

很奇怪。以下代码仅适用于 second 悬停。例如,页面已加载,切换有效,但过渡动画直到第二次悬停才起作用,每次都是一致的。

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

最佳答案

您需要确保元素以 contracted 类开头。在我补充说它也只适用于我的第二次悬停之前。

fiddle :http://jsfiddle.net/AtheistP3ace/czrqkorn/

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

关于javascript - 具有 CSS 过渡效果的 jquery 悬停功能仅适用于第二次悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33744555/

相关文章:

jquery - SSL CORS 不适用于 Zend Guard

css - 如何在 ExtJs 5 中添加自定义 CSS 文件?

jquery - 如何去除当前页面下的背景图片

html - 在 React 中添加组件会导致水平滚动条

javascript - 在 Chrome 71+ 上激活触摸事件

javascript - 使用 CSS 定位嵌套 div

javascript - 使用 jQuery 切换复杂弹出式样式 css 菜单的可见性

javascript - 在 Mongoose 的 forEach 循环中添加属性

javascript - 键 "device-width;"的视口(viewport)参数值 "width"无效,已被忽略。注意

javascript - 如何隐藏属性与输入值不匹配的元素?