javascript - 如何将 css 转换添加到标题中的 js 滚动?

标签 javascript css css-transitions

当页面向下滚动时,我有一个标题出现。我正在尝试添加 css 转换以使其淡入淡出,因为我读过使用 javascript 进行淡入淡出的效率不高。

.header-wrapper {
    top:0;
    left:0;
    right:0;
    position: fixed;
    display:none;
    height: 60px;
    border-top: 1px solid #000;
    background: red;
    z-index: 1;
}
.header-wrapper.active {
     display:block;   
}
.header {
    background-color:#000;
    height:80px;
}

Here is the js fiddle

$(window).scroll(function () {
    var y = $(window).scrollTop();

    // if above 300 and doesn't have active class yet
    if (y > 300 && !$('.header-wrapper').hasClass('active')) {
        $('.header-wrapper').addClass('active');

    // if below 300 has still has active class
    } else if(y <= 300 && $('.header-wrapper').hasClass('active')) {
        $('.header-wrapper').removeClass('active');
    }
    });

最佳答案

过渡是使用 css3 属性 transition 添加的。

造成混淆的一个常见原因:您只能转换接受数值的属性。因此,您不能在 display: blockdisplay: none 之间转换。

但是您可以opacity: 0opacity: 1 之间转换:

transition: 0.5s 不透明度

看起来像这样:

.bottomMenu {
    ...
    opacity: 0;
    transition: 0.5s opacity;
    ...
}
.bottomMenu.active {
     opacity: 1;   
}

对于您的特殊情况,我可能建议在 060px 之间转换高度。

为此你可以使用:

transition: 0.5s 高度

所以:

.bottomMenu {
    ...
    height: 0;
    transition: 0.5s height;
    ...
}
.bottomMenu.active {
     height: 80px;   
}

关于javascript - 如何将 css 转换添加到标题中的 js 滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25750731/

相关文章:

javascript - 滚动条到水平 div

jquery - css3动画不启动jquery点击事件

ios - iOS 设备 (iPhone/iPad) 上的 CSS3 转换 : applying opacity transition the DOM object flashy

javascript - 在第一次单击时添加类并在第二次单击按钮时向下滚动

javascript - 单击按钮时删除划线并转到下面的行

javascript - jQuery 将每个单词的首字母大写

css - Angular 和 CSS - 在 Chrome 开发者工具中看到背景,但在任何地方都找不到

CSS 下拉菜单不会与父菜单对齐

html - 如何将 -webkit-transform 应用于 div 而不是其中的元素?

jQuery 过渡还是 CSS?