大家好,我试图修复产品页面上的粘性div,但当它到达页脚时,元素与我的内容重叠。我正在使用CSS和js来修复我的div
<div id="carousel-listing" class="slide post-carusel">
<slider></slider>
</div>
<footer>
footer content
</footer>
用于css
.sticky {
position: fixed;
top: 75px;
width: 100%;
}
还有js
window.onscroll = function() {myFunction()};
var master_header = document.getElementById("carousel-listing");
var sticky = master_header.offsetTop;
function myFunction() {
if (window.pageYOffset > sticky) {
master_header.classList.add("sticky");
} else {
master_header.classList.remove("sticky");
}
}
当滚动到页脚时,有没有办法停止滚动?
您当前正在基于一个条件应用sticky
类:
窗口。页面偏移
如果要在滚动浏览内容后删除该类,首先必须选择页脚(最简单的方法是给它一个唯一的id,例如:
。my footer
,然后用document.getElementById
选择它)。一旦有了元素,就可以将函数application/removingsticky
class更改为header,以便在窗口时不再应用它。页面偏移量
大于页脚。offsetTop-header。远视
因此,条件变为:
window.pageYOffset > header.offsetTop
&& window.pageYOffset < footer.offsetTop - header.offsetHeight
看到它工作了吗
window.onscroll = onScrollHandler;
function onScrollHandler() {
const header = document.getElementById("myHeader");
const footer = document.getElementById('myFooter');
if (
window.pageYOffset > header.offsetTop
&& window.pageYOffset < footer.offsetTop - header.offsetHeight
) {
header.classList.add("sticky");
} else {
header.classList.remove("sticky");
}
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.top-container {
background-color: #f1f1f1;
padding: 30px;
text-align: center;
}
.header {
padding: 10px 16px;
background: #555;
color: #f1f1f1;
}
.content, #myFooter {
padding: 16px;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.sticky + .content {
padding-top: 102px;
}
#myFooter {
height: 200vh;
}
<div class="top-container">
<h1>Scroll Down</h1>
<p>Scroll down to see the sticky effect.</p>
</div>
<div class="header" id="myHeader">
<h2>My Header</h2>
</div>
<div class="content">
<h3>On Scroll Sticky Header</h3>
<p>The header will stick to the top when you reach its scroll position.</p>
<p>Scroll back up to remove the sticky effect.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p><p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p><p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p><p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p><p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>
<div id="myFooter">
<h1>Footer container</h1>
<p>Header should not be sticky anymore...</p>
</div>
我想要我的.课程内容粘性的div在桌面视口,不要在页脚以下。我写的位置粘,但它仍然不工作。我做错了什么?这是我的jsfiddle
问题内容: 我正在尝试创建一个包含三个div的页面:页眉,页脚和内容区域。这些应该占据屏幕的100%。 页眉和页脚很小,不会改变,内容区域可以是任何大小,因此我添加了使其变大时滚动的功能。 我正在使用以下CSS设置html和正文高度,因此容器上的技巧将起作用: 我的文档结构为: 问题答案: 方法1-Flexbox 它适用于已知和未知的高度元素。确保设置外股利和重置默认的。 方法2-CSS表 对于已
我希望我的引导页面的页脚总是在页面的底部,我找到的解决方案对我不起作用。这里没有提出任何解决方案 把页脚刷到页面底部,推特引导对我不起作用。由于书页的高度不同,这很棘手,对吗?不管我怎么做,页脚下总是有空白。既然我不想要一个固定的页脚,我应该如何实现这一点,因为我的引导网站是响应性的?
我已经使用twitter bootstrap框架很长一段时间了,它们最近更新到了版本3! 我很难让粘脚粘到底,我使用了twitter引导网站提供的入门模板,但仍然没有运气,有什么想法吗?
问题内容: 我正在寻找一种解决常见问题的方法,即在页面的页脚处停止固定对象。 我基本上在屏幕的左下角有一个固定的“共享”框,我不希望它在页脚上滚动,因此我需要它在页脚上方停止。 我在这里以及其他地方都看过其他问题。但是我无法使其适合我的情况。 这是共享框的html: …和CSS: 页脚是并且它没有固定的高度(如果有任何区别)。 如果有人可以帮助我为此创建一个简单的jQuery解决方案,我将不胜感激
我试图使div内容不流过页脚,我希望内容div随着页面的展开而展开,但是当文本流过页脚时,它会导致页脚在页面上向上跳转 html,body{margin:0;/top,right,bottom,left/padding:0;/top,right,bottom,left/height:100%;}