How to implement auto fixing a div like https://www.yahoo.com(如何实现自动修复像 https://www.yahoo.com 这样的 div)
问题描述
在雅虎网站中,向下滚动时,蓝色部分固定在顶部,而如果不滚动下来,蓝色部分不固定.
In Yahoo website, when scroll down, a blue part is fixed to the top, while if not scroll down, the blue part is not fixed.
如何实现这个?
我可以试试onScroll
功能吗?
推荐答案
在要修复的部分使用$(window).scroll(function()
.
小提琴演示:演示
$(window).scroll(function(){
if ($(window).scrollTop() >= 100) {
$('.sticky-header').addClass('fixed');
}
else {
$('.sticky-header').removeClass('fixed');
}
});
如果你想将固定部分应用到标题中,请替换 $(window).scroll(function(){}):
函数中的类名.
If you want to apply the fixed part to the header replace the class name in the $(window).scroll(function(){}):
function.
滚动时固定标题的示例:Demo-2
Example for fixed Header while scrolling : Demo-2
这篇关于如何实现自动修复像 https://www.yahoo.com 这样的 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何实现自动修复像 https://www.yahoo.com 这样的 div


基础教程推荐
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01