我有此代码段,但我需要将红色框限制为白色框,而不是转码。
.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
已经是fixed
,因此也要修复您的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>
编辑:
希望将其固定-使用jquery将
red-div
与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/