解决 div 下面(屏幕下)内的滚动元素 - 我想在滚动后修复它

发布于 2025-01-06 14:52:12 字数 649 浏览 3 评论 0原文

我不知道如何解决这种情况:

我的 html/css 看起来像这样: 显示我的 css/html 外观以及登陆页面后屏幕上显示的内容的图像:

>当我向下滚动时,我看到绿色元素: 向下滚动-> 在此处输入图像描述

继续向下滚动后,我看到了完整的绿色元素,如果向下滚动,我想要这个元素就像CSS语言中一样:位置固定底部0。见下图: 我已经看到了完整的元素 ->相同的链接,但图像名为 Problem3.png

,然后我滚动到下面,我想将其固定在页面底部,如下图所示:

屏幕上的固定元素 - 我想要什么,但我不知道该怎么做 - >相同的链接,但图像名为 Problem4.png (愚蠢的垃圾邮件预防机制)

是否可以解决这种情况?

总结一下:我有两个 div,一个在上面,第二个在下面,当我向下滚动时,我突然看到另一个元素(绿色 div),当我继续向下滚动时,我想将这个绿色 div 固定在底部页。

当然,当我向上滚动(回到顶部)时,我想将那个绿色 div“停放在”第二个 div 的顶部。

有什么方法可以用 jQuery (Javascript) / html / css 解决这种情况吗?

先感谢您

I do not know how to solve this situation:

I`ve got the html/css looks like this:
Image showing how my css/html looks like and what is displayed on the screen after landing on page: enter image description here

The when I scroll down I see green element:
scrolling down -> enter image description here

After continuing to scrolling down I saw full green element and the if I scroll down I want to have this element like in css language: position fixed bottom 0. See image below:
I ve saw full element -> same link but image called problem3.png

and then I scroll below and I want to have it fixed at the bottom of the page, like on this image:

Fixed element on screen - What I want and I do not know how to do that -> same link but image called problem4.png (stupid spam prevention mechanism)

Is it possible to solve this situation ?

To sum up: I`ve got two divs, one above and second below, Wheen I scroll down I suddenly see another element (green div) and when i continue to scroll down I WANT TO HAVE THIS GREEN DIV FIXED AT THE BOTTOM OF THE PAGE.

Ofcourse, when I scroll up (back on the top) I want to "park" that green div at the top of the second div.

Is there any way to solve this situation with jQuery (Javascript) / html / css ?

Thank you in advance

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

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

发布评论

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

评论(1

怎言笑 2025-01-13 14:52:12

我认为你必须展示一些 html 结构。有很多方法可以达到这种效果。从根本上讲,用 JavaScript 术语来说,您将需要:

  1. 向窗口滚动添加一个事件侦听器,以检查绿色元素是否完全在视图中
  2. 如果在视图中,则添加一个类(或更改其 css)来固定其位置您想要
  3. 更改窗口滚动方法,以便检查红色 div 到屏幕顶部的相对偏移量。如果它低于应固定绿色 div 的位置,请删除您之前添加的类。

这听起来很复杂,但也不算太糟糕。 javascript 类似于:

$(function() {
  $(window).scroll(function() {

    if($(".divToFix").hasClass("fixedAtBase")){
        if(Utils.underView($(".redDiv"), $(".divToFix").height())) $(".divToFix").removeClass("fixedAtBase");
    } else {
        if(Utils.inView($(".divToFix"))) $(".divToFix").addClass("fixedAtBase");
    }

  });


});

Utils = {
    underView: function(element, offset) {
        return (($(window).height() + $(window).scrollTop() - offset) <= element.offset().top);
    },

    aboveView: function(element) {
        return ($(window).scrollTop() >= element.offset().top + element.height());
    },

    inView: function(element) {
        return (Utils.aboveView(element) !== true && Utils.underView(element, element.height()) !== true);
    }
};

请记住,我还没有测试过它或任何东西。

编辑 - 这是一个 演示

I think you'll have to show some of your html structure. There are lots of ways to achieve this kind of effect. Fundamentally, in javascript terms you'll be looking to:

  1. Add an event listener to the window scroll that checks whether the green element is fully in view
  2. If it is in view, add a class (or change it's css) that fixes it's position where you want
  3. Change your window scroll method so that it's checking the relative offset of the red div to the top of the screen. If it goes below the position where the green div should be fixed, remove the class you added earlier.

That sounds complicated, but it's not too bad. The javascript would be something like:

$(function() {
  $(window).scroll(function() {

    if($(".divToFix").hasClass("fixedAtBase")){
        if(Utils.underView($(".redDiv"), $(".divToFix").height())) $(".divToFix").removeClass("fixedAtBase");
    } else {
        if(Utils.inView($(".divToFix"))) $(".divToFix").addClass("fixedAtBase");
    }

  });


});

Utils = {
    underView: function(element, offset) {
        return (($(window).height() + $(window).scrollTop() - offset) <= element.offset().top);
    },

    aboveView: function(element) {
        return ($(window).scrollTop() >= element.offset().top + element.height());
    },

    inView: function(element) {
        return (Utils.aboveView(element) !== true && Utils.underView(element, element.height()) !== true);
    }
};

Bear in mind I've not tested that or anything.

edit - here's a demo

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