gpt4 book ai didi

javascript - 修复了与其父级相关的 div

转载 作者:搜寻专家 更新时间:2023-10-31 22:57:17 26 4
gpt4 key购买 nike

我有这个片段,但我需要将红色框限制在不转换的白色框内。

.main {
background-color: black;
width: 100%;
height: 1250px;
position: relative;
}

.container{
display: block;
position: absolute;
width: 250px;
height: 650px;
background: white;
}

.red-box{
background: red;
width: 150px;
height: 140px;
position: fixed;
}
<div class="main">
<div class="container">
<div class="red-box">
</div>
</div>
</div>

最佳答案

因为 red-box 已经修复,所以也修复你的 container - 希望这能解决你的问题。干杯!

body {
margin: 0;
}
.main {
background-color: black;
width: 100%;
height: 1250px;
position: relative;
}
.container {
display: block;
position: fixed;
width: 250px;
height: 650px;
background: white;
}
.red-box {
background: red;
width: 150px;
height: 140px;
position: fixed;
}
<div class="main">
<div class="container">
<div class="red-box">
</div>
</div>
</div>

编辑:

希望这会固定它 - red-div 使用 jquery 与 container 一起滚动:

$(document).scroll(function() {
var wrapper = $('.container');
var box = $('.red-box');

var offset = wrapper.offset().top - $(window).scrollTop() + wrapper.outerHeight() - box.outerHeight();


if (offset >= 0) {
box.css({
'top': 0
});
return;
}

box.offset({
'left': box.offset().left,
'top': $(window).scrollTop() + offset
});


});
body {
margin: 0;
}
.main {
background-color: black;
width: 100%;
height: 1250px;
position: relative;
}
.container {
display: block;
position: absolute;
width: 250px;
height: 650px;
background: white;
}
.red-box {
background: red;
width: 150px;
height: 140px;
position: fixed;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<div class="container">
<div class="red-box">
</div>
</div>
</div>

关于javascript - 修复了与其父级相关的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39334754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com