javascript - .bind ('scroll' ), 添加动画或减少 "bounce"

标签 javascript jquery css scroll

这是我用来在用户滚动页面时固定菜单的 jQuery 脚本。 <强> This 是现场演示。

<script>
var num = 170; //number of pixels before modifying styles

$(window).bind('scroll', function () {
    if ($(window).scrollTop() > num) {
        $('#header').addClass('fixed');
    } else {
        $('#header').removeClass('fixed');
    }
});
</script>

<style>
.fixed {
    position:fixed;
    top:0;
    background-color:#e4e4e4;
    width:100% !important;
    background-image: url("images/logo_small.png") !important;
    background-size:20px auto !important;
    margin:0 auto !important;
    padding:20px 0 10px !important;
    background-position:90px center !important;
    z-index:1;
}


#header {
    background-image: url("images/logo.png");
    background-position: 30px 5px;
    background-repeat: no-repeat;
    background-size: 152px auto;
    font-size: 13px;
    margin: 18px auto 0;
    padding: 60px 0 87px 100px;
    width: 780px;
}
</style>

问题是,如您所见,当“小”菜单出现时会“跳转”,内容会突然上升。

最佳答案

http://jsfiddle.net/andaywells/jVy5L/64/embedded/result/

var num = 170; //number of pixels before modifying styles

$(window).bind('scroll', function () {
if ($(window).scrollTop() > num) {

    $('#header').addClass('fixed');


} else {
    $('#header').removeClass('fixed');

}
});

我删除了淡出,并添加了 position: fixed;到标题,所以使用 css 转换,滚动更加流畅。

关于javascript - .bind ('scroll' ), 添加动画或减少 "bounce",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18556799/

相关文章:

html - 页面布局在不同的计算机上变得困惑,自动调整问题

javascript - 使用 d3.js 制作分组条形图

javascript - Chrome 命令行 API 中的 table() 是什么

javascript - angularjs/signalR Bootstrap 进度条不随 Controller 更改而更新

jquery - 使用 jQuery DataTables 以编程方式搜索表

javascript - 谷歌翻译将英语设置为默认,以便翻译回来

html - 无法在移动设备和桌面设备上将照片居中

javascript - 如何使用 html、css、javascript 获取隐藏内容以显示在光标上?

javascript - 触发点击 angular.js 中的 html 元素

javascript - 在 IE 上带有滚动条渲染问题的 Handsontable 固定标题