y 滚动时元素的对齐方式

发布于 2024-10-10 23:58:20 字数 320 浏览 4 评论 0原文

我想要的是,当有人使用 y 滚动时,即使在这种情况下,标题(包含日期和日期应该可见。)请检查 屏幕截图

使用位置:固定和z-index等的东西

(我见过的例子:--当你打开堆栈溢出并且你的JavaScript被禁用时,那么在这种情况下会出现一条红色消息即使您向下滚动页面,也会出现并继续出现。)

所以我想做的是: -

最初在页面加载时,这一行将位于页面之间(因为上面还有很多其他内容,一旦用户滚动向下它应该对用户可见,即它粘在页面顶部)

What i want to is when somebody use y scroll even in that case the header (which contains days and dates should be visible.) Please check the screen shot

Something using position:fixed and z-index etc.

(example i have seen :-- when u open stack-overflow and ur JavaScript is disabled, then in that case a red colored message will appear and keep appearing even if u scroll down the page.)

So What i want to do is:--

Initially at page load this row will the be in between the page (as there are lot of other things above and once user scroll down it should be visible to user ie this sticks with the top of the page)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2024-10-17 23:58:20

如果我理解正确的话,您正在寻找一个不在页面顶部但当您向下滚动经过它时粘在顶部的元素?这可以通过 css 和一点 JavaScript 魔法(为了简洁而使用 jQuery)来完成:

/* CSS - you should add width, height, padding as needed */
#your_element { position:relative;}

如果禁用 JS,您的元素将正常运行。否则,以下代码片段将使其动态化:

win = $(window);
element = $('#your_element');

if (element.length) {                // <-- Make sure element exists
    win.scroll(function () {         // <-- When scrolling do the following
        if (win.scrollTop() > 100) { // <-- Adjust number.  Explanation to follow
            element.css({'position':'fixed', 'top':'0px'});
        } else {
            element.css({'position':'relative', 'top':'auto'});
        }
    });
}

现在您只需调整值 100 即可等于页面顶部与元素顶部之间的距离。因此,如果您的元素距顶部 250px,则应使用 250。如果你不确定,你可以尝试一下,直到你得到看起来光滑的东西。

If I understand you correctly, you are looking for an element that is not located at the top of the page, but sticks to the top when you scroll down past it? This can be done with css and a little JavaScript magic (jQuery used for brevity):

/* CSS - you should add width, height, padding as needed */
#your_element { position:relative;}

Your element will behave normally if JS is disabled. Otherwise, the following snippet makes it dynamic:

win = $(window);
element = $('#your_element');

if (element.length) {                // <-- Make sure element exists
    win.scroll(function () {         // <-- When scrolling do the following
        if (win.scrollTop() > 100) { // <-- Adjust number.  Explanation to follow
            element.css({'position':'fixed', 'top':'0px'});
        } else {
            element.css({'position':'relative', 'top':'auto'});
        }
    });
}

Now you just have to adjust the value 100 to equal the distance between the top of the page and the top of your element. So, if your element is 250px from the top, you would use 250. If you are not sure you can play around with it until you get something that looks smooth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文