如果向下滚动则向上滑动标题,反之亦然

时间:2013-09-04 01:27:39

标签: javascript jquery header scroll slide

更新

我在GitHub上做了一个回购:

https://github.com/yckart/Veil.js

非常感谢Sargo DaryaEdward J Payton


我要创建一个标题,如果向下滚动则向上滑动,反之亦然。问题是,它会跳转(如果你在diff - 范围在0-128之间)。

我无法弄清问题所在。知道如何让它正常工作吗?

这是我到目前为止所做的:http://jsfiddle.net/yckart/rKW3f/

// something simple to get the current scroll direction
// false === down | true === up
var scrollDir = (function (oldOffset, lastOffset, oldDir) {
    return function (offset) {
        var dir = offset < oldOffset;
        if (dir !== oldDir) lastOffset = offset;
        oldOffset = offset;
        oldDir = dir;
        return {dir: dir, last: lastOffset};
    };
}());

var header = document.querySelector('header');
var height = header.clientHeight;
addEventListener('scroll', function () {
    var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
    var dir = scrollDir(scrollY);
    var diff = dir.last-scrollY;

    var max = Math.max(-height, Math.min(height, diff));
    header.style.top = (dir.dir ? max-height : max) + 'px';
});

另一个问题是,如果第一次更改滚动方向,则没有任何反应。但是,这可以用间隔来修复,或者。

3 个答案:

答案 0 :(得分:3)

我相信这正是你想要的:

var header = $('header'),
headerHeight = header.height(),
treshold = 0,
lastScroll = 0;

$(document).on('scroll', function (evt) {
    var newScroll = $(document).scrollTop(),
        diff = newScroll-lastScroll;

    // normalize treshold range
    treshold = (treshold+diff>headerHeight) ? headerHeight : treshold+diff;
    treshold = (treshold < 0) ? 0 : treshold;

    header.css('top', (-treshold)+'px');

    lastScroll = newScroll;
});

演示:http://jsfiddle.net/yDpeb/

答案 1 :(得分:1)

试试这个: - http://jsfiddle.net/adiioo7/rKW3f/7/

<强> JS: -

var scrollDir = (function (oldOffset, lastOffset, oldDir) {
    return function (offset) {

        var dir = offset < oldOffset;
        if (dir !== oldDir) {
            lastOffset = offset;
        } else {
            offset = offset - height;
        }
        oldOffset = offset;
        oldDir = dir;
        return {
            dir: dir,
            last: lastOffset
        };
    };
}());

var header = document.querySelector('header');
var height = header.clientHeight;

$(window).scroll(function () {
    var scrollY = $(window).scrollTop();
    var dir = scrollDir(scrollY);
    var diff = dir.last - scrollY;

    var max = Math.max(-height, Math.min(height, diff));

    max = (dir.dir ? max - height : max); 
    max = scrollY<height?0:max;


    $('header').data('size', 'small');
    $('header').stop().animate({
        top: max
    }, 600);


});

答案 2 :(得分:0)

Sargo Darya上面的回答是我正在寻找的确切但我发现Webkits惯性滚动的错误所以我做了一个修复:

// Scrolling Header
var header = $('header'),
headerHeight = header.height(),
offset = 0,
lastPos = 0;

$(document).on('scroll', function(e) {
var newPos = $(document).scrollTop(),
    pos = newPos-lastPos;

if (offset+pos>headerHeight) { 
    offset = headerHeight;
} else if (newPos < 0){ // webkit inertia scroll fix
    offset = 0;
} else {
    offset = offset+pos;
};
if (offset < 0) {
    offset = 0;
} else {
    offset = offset;
};

header.css('top', (-offset)+'px');

lastPos = newPos;
});

添加一行修复它。如果 - 如果滚动位置低于0,则将标题偏移设置为0。

基于Sargo Darya的演示 - http://jsfiddle.net/edwardomni/D58vx/4/