位置固定div,当向上滚动和位置绝对,当向下滚动:google现在搜索栏类型行为

问题描述:

我想创建一个div的行为非常类似于google现在的搜索栏控件。

Im trying to create a div behavior very similar to google now's search bar control.

当页面加载时,你得到一个位于特定位置的div,当您开始向下滚动div将保持在其相对/绝对位置(与滚动一起滑动,直到它消失)。一旦你开始向上滚动(或向上翻页),div将从顶部(与内容一起)滑入,直到它到达固定位置并停留在那里。

When the page loads initially, you get a div located in a specific position, as you start scrolling down the div will remain in its relative/absolute position(slide along with the scroll until it disappears). Once you start scrolling up (or page up) the div would slide in from the top(along with the content) until it reaches its fixed position and stay there.

现在我想出了一个非常粗糙/丑陋的例子,我想实现和它的作品,但我肯定有一个更好的方法一般来说。

Now Ive come up with a very crude/ugly example of what Im trying to achieve and it sort of works, but Im sure there's a far better approach in general to this.

http://jsfiddle.net/9654gd6c/ 3 /

var lastScrollTop = 0,
    scrH = 40,
    marginTop = 40,
    ch = {};
$(window).scroll(function (event) {
    var st = $(this).scrollTop();

    var elements = $('#scroller');

    if (st > lastScrollTop) {
        elements.css({
            'position': 'absolute'
        });
        ch.dir = 'down';

    } else {

        if (ch.dir === 'down') ch.at = lastScrollTop;
        if (ch.at - st - marginTop > scrH) {
            elements.css({
                'position': 'absolute',
                    'top': st + marginTop
            });
        } else {
            elements.css({
                'position': 'absolute',
                    'top': ch.at - scrH
            });
        }
        ch.dir = 'up';

    }
    lastScrollTop = st;
});

哦,并且删除样式按钮应该作为立即显示div按钮,固定的地方。非常感谢您的帮助

oh and the remove style button should act as immediate show div button, that brings back the div to its fixed place. Thanks for your help in advance

更新2 :感谢David Karam这里的一个例子,不切换位置

http://jsfiddle.net/rmx50du5/2/

Update 2: Thanks to David Karam here's an example without switching the position
http://jsfiddle.net/rmx50du5/2/

实现的最大缺点是你在位置固定和绝对之间切换。这意味着你不能使用CSS动画,可以在你的例子中真正的方便。

The biggest downside to your implementation is the fact that you are switching between position fixed and absolute. This means you cannot make use of CSS animations which can come real handy in your example.

这里是一个修改版本的位置相对允许使用转换(我使用不透明度,但您可能想要在顶部进行转换,这将需要一些额外的行来抵消滚动)

Here's a modified version with position relative allowing for use of transitions (I use opacity but you probably want to transition on top which will require a couple of extra lines to offset the scrolling)

http://jsfiddle.net/o16bebtu/

这是一种更简单的方式,通过使位置relative,然后使用getBoundingClientRect,它考虑到滚动位置。

It is a neater way of achieving what you want by making the position relative and then making use of getBoundingClientRect which accounts for scrolling position.

var nodeScrollTop= ((Math.ceil(rect.top))|0);
var nodeTop= (!!(_node.style.top)) ? parseInt(_node.style.top) : 0;
var newNodeTop= nodeTop - nodeScrollTop;

从MDN, https://developer.mozilla.org/en-US/docs/Web/API/Element.getBoundingClientRect


在计算
时,将视口区域(或
任何其他可滚动元素)的滚动量考虑在内边界矩形。这意味着,一旦滚动位置改变,顶部和左边的属性就改变
的值(所以它们的
值是相对于视口而不是绝对的)。如果这不是
所需的行为,只需将当前滚动位置添加到
top和left属性(通过window.scrollX和window.scrollY),以获得
独立于当前滚动的常量值位置。

The amount of scrolling that has been done of the viewport area (or any other scrollable element) is taken into account when computing the bounding rectangle. This means that the top and left property change their values as soon as the scrolling position changes (so their values are relative to the viewport and not absolute). If this is not the desired behaviour just add the current scrolling position to the top and left property (via window.scrollX and window.scrollY) to get constant values independent from the current scrolling position.