如何让固定的“粘性” div 留在视口之外

发布于 2025-01-07 15:30:44 字数 2363 浏览 0 评论 0原文

我正在尝试遵循本教程: http://jqueryfordesigners.com/fixed-floating-elements/< /a>.

问题是,每当将固定定位应用于 div 时,如果浏览器宽度发生更改,则 div 会水平移动以适合视口内。在教程示例中 http://static.jqueryfordesigners.com/demo/fixedfloat.html这不会发生。

这是我的代码(所有内容都在相对定位的#wrapper 中):

CSS:

        #cart {
            position: absolute;
            right: 0;
            width: 270px;
        }

        #target {
            width: 270px;
            height: 200px;

            background-color: blue;
            background-position: 50% 50%;
            background-repeat: no-repeat;

            position: absolute;
            top: 250px;
            right: 0;

            padding: 0;
            border-radius: 15px 15px 0 0 ;
        }

        #drag-here {
            width: 270px;
            height: 0;

            background-image: url(drag-here.png);
            background-repeat: no-repeat;
            display: none;

            position: absolute;
            top: 470px;
            right: 0;
        }

        #cart-list {
            display: none;
            position: absolute;
            top: 430px;
            right: 0;

            list-style-type: none;
        }

        .sticky #target {position: fixed; top: 5px;}
        .sticky #drag-here {position: fixed; top: 225px;}
        .sticky #cart-list {position: fixed; top: 185px;}

HTML:

<section id="cart">
    <div id="target" class="target-listen"></div>
    <div id="drag-here"></div>
    <ul id="cart-list">
    </ul>
</section>

JQuery:

sticky = false;
initialOffset = $("#target").offset().top - 5;
$(window).scroll(function () {
    if ($(this).scrollTop() >= initialOffset) {
        if (!sticky) {
            $("#cart").addClass("sticky");
            sticky = true;
        } 
    }
    else {
        if (sticky) {
            $("#cart").removeClass("sticky");
            sticky = false;
        }
    }
});

您可以在此处查看我的页面:http://www.brandcoffee.it/test/brandapart/imagick.php

I'm trying to follow this tutorial: http://jqueryfordesigners.com/fixed-floating-elements/.

The problem is that whenever the fixed positioning is applied to the div, if the browser width gets changed the div moves horizontally to fit inside the viewport. In the tutorial example http://static.jqueryfordesigners.com/demo/fixedfloat.html this does not happen.

This is my code (everything is inside a relative-positioned #wrapper):

CSS:

        #cart {
            position: absolute;
            right: 0;
            width: 270px;
        }

        #target {
            width: 270px;
            height: 200px;

            background-color: blue;
            background-position: 50% 50%;
            background-repeat: no-repeat;

            position: absolute;
            top: 250px;
            right: 0;

            padding: 0;
            border-radius: 15px 15px 0 0 ;
        }

        #drag-here {
            width: 270px;
            height: 0;

            background-image: url(drag-here.png);
            background-repeat: no-repeat;
            display: none;

            position: absolute;
            top: 470px;
            right: 0;
        }

        #cart-list {
            display: none;
            position: absolute;
            top: 430px;
            right: 0;

            list-style-type: none;
        }

        .sticky #target {position: fixed; top: 5px;}
        .sticky #drag-here {position: fixed; top: 225px;}
        .sticky #cart-list {position: fixed; top: 185px;}

HTML:

<section id="cart">
    <div id="target" class="target-listen"></div>
    <div id="drag-here"></div>
    <ul id="cart-list">
    </ul>
</section>

JQuery:

sticky = false;
initialOffset = $("#target").offset().top - 5;
$(window).scroll(function () {
    if ($(this).scrollTop() >= initialOffset) {
        if (!sticky) {
            $("#cart").addClass("sticky");
            sticky = true;
        } 
    }
    else {
        if (sticky) {
            $("#cart").removeClass("sticky");
            sticky = false;
        }
    }
});

You can see my page here: http://www.brandcoffee.it/test/brandapart/imagick.php

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

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

发布评论

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

评论(2

静待花开 2025-01-14 15:30:44

我认为删除 #target 中的 right : 0; 应该可以。

I think removing right : 0; in #target should do.

瑶笙 2025-01-14 15:30:44

尝试以ratio(或百分比)而不是固定像素值的形式提供width属性。
例如 :

width:25%; //for #target, and
float:right; //make target float right

width:75%; // for other contents on left side

Try with providing the width attribute in ratio (or percent) rather than fixed pixel values.
such as :

width:25%; //for #target, and
float:right; //make target float right

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