简体   繁体   English

滚动div内的固定位置

[英]Fixed position inside scrolling div

I have a div with let's say a height on 400px.我有一个 div,假设高度为 400px。 The content is much longer, so the div can scroll (y-axis).内容更长,因此 div 可以滚动(y 轴)。 In the right side of the div, I need some buttons to have a fixed position inside that div .在 div 的右侧,我需要一些按钮才能在 div 内固定位置。

How would I do that?我该怎么做? jQuery, CSS, whatever - I don't mind. jQuery、CSS 等等——我不介意。

I tried fixTo - but doesn't seem to work for this - and a CSS solution that says "use position:fixed, but no left/right - just margin".我尝试了 fixTo - 但似乎对此不起作用 - 以及一个 CSS 解决方案,上面写着“使用位置:固定,但没有左/右 - 只是边距”。 It works OK, but if the page itself scrolles, the buttons scrolls too - which they shouldn't.它工作正常,但如果页面本身滚动,按钮也会滚动 - 他们不应该滚动。 They should stay fixed inside the div at all time.它们应该始终固定在 div 内。

Example code:示例代码:

<div class="container" style="width: 960px; height: 400px; position: relative;">
    <div class="buttons needToBeFixed"></div>
    <div class="aLotOfContent" style="width: 2000px;"></div>
</div>

You need to have the button positioned absolute inside of the container.您需要将按钮绝对定位在容器内。

Snippest:片段:

 .container { width: 250px; outline: 1px solid blue; width: 300px; height: 150px; position: relative; } .container .aLotOfContent { width: 300px; height: 150px; overflow: auto; background: #e3ecfc; } .container .fixed{ width: 60px; height: 40px; background-color: #e52727; color: white; position: absolute; right: 40px; bottom: 20px; } html, body{ height: 400px; }
 <div class="container"> <button class="fixed"> im fixed! </button> <div class="aLotOfContent"> alotOfContent<br>alotOfContent<br>alotOfContent<br>alotOfContent<br> alotOfContent<br>alotOfContent<br>alotOfContent<br>alotOfContent<br> alotOfContent<br>alotOfContent<br>alotOfContent<br>alotOfContent<br> alotOfContent<br>alotOfContent<br>alotOfContent<br>alotOfContent<br> alotOfContent<br>alotOfContent<br>alotOfContent<br>alotOfContent<br> alotOfContent<br>alotOfContent<br>alotOfContent<br>alotOfContent<br> alotOfContent<br>alotOfContent<br>alotOfContent<br>alotOfContent<br> </div> </div>

You could modify your HTML markup and use a wrapper element with position relative.您可以修改 HTML 标记并使用具有相对位置的包装元素。 Then you could postionning element relative to this parent using position: absolute;然后你可以使用position: absolute;相对于这个父元素position: absolute;元素position: absolute; . .

 body { width: 1500px; height: 1500px; } #wrapper { position: relative; width: 350px; overflow: hidden; } #container { height: 350px; overflow-y: scroll; border: solid #000 1px; } .sticky { position: absolute; } .right { right: 0; } .bottom { bottom: 0; }
 <div id="wrapper"> <input type="button" value="Sticky button" class="sticky top" /> <input type="button" value="Sticky button" class="sticky bottom left" /> <input type="button" value="Sticky button" class="sticky right" /> <input type="button" value="Sticky button" class="sticky bottom right" /> <div id="container"> <div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. <br />Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div> </div> </div>

You can add the object outside of the div in a fixed position,depending on what is inside of your div?您可以将对象添加到 div 外部的固定位置,具体取决于 div 内部的内容? If your div depends on data inside of it, some javascript can aggregate the contents before performing desired actions dependent on the whole.如果您的 div 依赖于其中的数据,则某些 javascript 可以在执行依赖于整体的所需操作之前聚合内容。

I solved it by placing the buttons underneath and then using margin-top: -400px;我通过将按钮放在下面然后使用 margin-top: -400px; 解决了这个问题。

<div style="position: relative">
    <div class="container" style="height: 400px;"></div>
    <div class="buttons" style="margin-top: -400px; position: absolute; right: 0;"></div>
</div>

It seems to work.它似乎工作。 If someone has better solutions they're of course welcome.如果有人有更好的解决方案,他们当然欢迎。 Thanks for all your answers.感谢您的所有回答。

You can fixed your issue in two ways.您可以通过两种方式解决您的问题。

Pure HTML/CSS纯 HTML/CSS

<div class="container relparentwrap" style="width: 960px; height: 400px;">
        <div style="height:100%; width:100%;  position: relative; overflow:auto;">
        <div class="aLotOfContent" style="width: 100%; background:red;padding:20px 0;">
            <div style="height:400px; background:green"></div>
            <div style="height:500px; background:yellow"></div>
            <div style="height:500px; background:purple"></div>
        </div>
        </div>
        <div class="buttons needToBeFixed" style="border:solid 1px #fff;margin-top: -20px;position: relative;z-index: 1;">Helllo</div>
</div>

Since needToBeFixed is out from your scrolling area.由于needToBeFixed超出了您的滚动区域。 It will always stay in end of your div.它会一直留在你的 div 的末尾。

jQuery Solution As you mention you don't have an issue using jQuery below is your code snippet. jQuery 解决方案正如您所提到的,使用 jQuery 没有问题,下面是您的代码片段。 JSFiddle Link JSFiddle 链接

<!DOCTYPE html>
<html>
<head>
    <title>Fixed position inside scrolling div</title>  
</head>
<body style="margin:0;">
    <div class="container relparentwrap" style="width: 960px; height: 400px; position: relative; overflow:auto;">
        <div class="aLotOfContent" style="width: 100%; background:red;padding:20px 0;">
            <div style="height:400px; background:green"></div>
            <div style="height:500px; background:yellow"></div>
            <div style="height:500px; background:purple"></div>
        </div>
        <div class="buttons needToBeFixed" style="position:absolute; left:0; right:0; border:solid 1px #fff;">Helllo</div>
    </div>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
    <script>
    $(function() {
        var p = $(".relparentwrap").offset();
        var tPos = (p.top + $(".relparentwrap").outerHeight()) - $(".needToBeFixed").outerHeight();
        vPos=tPos;
        $(".needToBeFixed").css("top",tPos);
        $(".relparentwrap").scroll(function() {
            tPos = vPos + $(".relparentwrap").scrollTop();
            console.log(tPos);
            $(".needToBeFixed").css("top",tPos);
        });

    });
    </script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM