随着滚动条下拉,导航栏置顶

想做一个这样的效果,当我们下拉滚动条时,写的导航栏置顶。
这是效果图

先学习用到的基本知识

jQuery CSS 操作 - scrollTop() 方法

scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置。
scroll top offset 指的是滚动条相对于其顶部的偏移。
如果该方法未设置参数,则返回以像素计的相对滚动条顶部的偏移。

jQuery CSS 操作 - offset() 方法

offset() 方法返回或设置匹配元素相对于文档的偏移(位置)。

jQuery 属性操作 - attr() 方法

attr() 方法设置或返回被选元素的属性值。

var top=$("#aaa").offset().top;
意思是:获取id是aaa元素的top坐标。

$("#aaa").attr(“style”,“position:fixed;top:0;left:0;zindex:10;”);
意思是:给id是aaa的元素增加了几个样式,position:fixed;给元素定位,top值为0,left值为0。

下面是我写的例子

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"> <title></title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<style type="text/css">*{margin: 0; padding: 0;}a{cursor: pointer;}#first{width: 100%;height: 44px;background: red;}#second{width: 100%;height: 300px;background: ;}#top{width: 100%;height: 55px;}#bottom{width: 100%;height: 300px;padding-top: 100px;}.left{width: 70%;height: 55px;float: left;padding-left: 20px;display: ;background: ;}.left li{height: 55px;margin-right: 20px;float: left;list-style: none;padding-top: 10px;}.right{width: 30%;height: 45px;display: ;float: right;margin-top: -55px;padding-top: 10px;background: ;}#ccc{height: 300px;background: ;display: block;}.aaa{margin-top: 100px;}.bbb{}#three{width: 100%;height: 4000px;}
</style>
</head>
<body><div id="first"><span>菜单</span><span>腾讯云</span><span>备案</span><span>控制台</span></div><div id="second"><div id="top"><div class="left"><li><a>云社区</a></li><li><a>专栏</a></li><li><a>问答</a></li><li><a>沙龙</a></li><li><a>快讯</a></li><li><a>团队主页</a></li><li><a>开发者手册</a></li><li><a>云学院</a></li></div><input type="hidden" name="search"><div class="right"><span class="ban"><a>写文章</a><span><span class="bbn"><a>提问</a><span><span class="bcn"><a>登录</a><span><span class="bcn"><a>注册</a><span></div></div><div id="bottom"><div id="ccc"><span class="aaa">专栏</span><span class="bbb">分享技术干货,专注打造技术影响力</span><p>This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.</p></div></div></div><div id="three"></div>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">var top=$("#top").offset().top;$(window).scroll(function () {if ($(window).scrollTop() >= 33) {$("#top").attr("style", "position:fixed;top:0;left:0;z-index:10;background:black;color:white;");} else {$("#top").attr("style", "");}}).trigger("scroll");
</script>
</body>
</html>

关于导航置顶的代码是这部分

<script type="text/javascript">var top=$("#top").offset().top;$(window).scroll(function () {if ($(window).scrollTop() >= 33) {$("#top").attr("style", "position:fixed;top:0;left:0;z-index:10;background:black;color:white;");} else {$("#top").attr("style", "");}}).trigger("scroll");
</script>

//获取#top的top值
//当滚动导航栏时定义函数
//当导航栏距顶部33px时改变#top的style,因为定义的#first div的高度是33px
//修改#top样式
//否则就不修改

这是网页刚打开的时候

第一行红色部分就是#first盒子,height是33px
第二行就是需要浏览置顶的#top盒子
第三行是方便看清楚效果用的#three盒子

当我们下滑滚轮时,可以看到文字是在#second下面的,因为我设置了#top的z-index值。
可以看到这样的效果

随着滚动条下拉,导航栏置顶相关推荐

  1. html css 导航 左右滑动效果代码,HTML+CSS/CSS3实现滑动下拉导航栏

    HTML+CSS/CSS3实现滑动下拉导航栏 纯css实现的滑动下拉导航栏,超详细代码,以及实现中遇到的小问题 首先创建一个列表以及一些标签的全局样式 html代码 css代码 * { margin: ...

  2. 怎么在html5中制作下拉导航栏,在PPT中制作下拉式导航菜单效果的方法

    为了提高PPT演示文稿的观赏性,用户可能希望在PPT幻灯片中添加下拉式导航菜单效果,通过该导航菜单在不同幻灯片间进行导航(如图1所示),制作下拉式导航菜单的具体操作步骤如下. 图1 下拉式导航菜单 ( ...

  3. html 下拉导航栏源码,html导航栏下拉菜单怎么制作?这里有详细的代码实例

    元素来包裹这些元素,并使用CSS来设置下拉内容的样式. html导航栏菜单的CSS部分: .dropdown类使用position:relative,这将设置下拉菜单的内容放置在下拉按钮(使用posi ...

  4. 使用 CSS 创建下拉导航栏

    下拉菜单有一个可以通过单击选择的项目列表.我们可以使用 HTML 元素和 CSS 创建一个下拉列表.所以,让我们学习创建一个下拉菜单. 创建下拉列表 可以使用<div>容器和<but ...

  5. vue 滑动置顶功能_VUE 实现滚动监听 导航栏置顶的方法_蜡烛_前端开发者

    非重点的代码,比如样式啥的,我就不放上来了,一笔带过 简略的写一下 1. data () data () { paddingBottom: '1.5rem', // 给最外层div一个padding- ...

  6. 【Android UI设计与开发】第11期:顶部标题栏(二)ActionBar实现Tab选项卡和下拉导航列表

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9050573  在上一篇文章中,我们只是大概的了解了一下关于ActionBar ...

  7. html导航图片滚动条,CSS实现导航栏底部动态滚动条效果

    预习了CSS3部分的新知识,想着在不使用JS的情况下实现导航栏滚动条效果,如下: 实现滚动条效果,其实就是在 标签中让元素的宽度由0变化到100%,代码很简单,如下所示: CSS样式部分: ul.na ...

  8. mui 组件:“div下拉导航”【scroll固定位置】- 案例篇

    (含代码.效果图)侧滑导航:"div下拉导航" 截图如下: 重要代码 · 如下: //下拉菜单定位:动态锁定 window.onscroll = function(){let sc ...

  9. Vue实现导航栏吸顶效果

    <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" ...

最新文章

  1. WebLogic 数据源密码加密
  2. MXNet学习:试用卷积-训练CIFAR-10数据集
  3. Firefox不支持event解决方法
  4. 软件稳定性测试的测试点
  5. HDU - 5451 Best Solver(循环群+矩阵快速幂)
  6. osg动态加载模型不显示_OSG仿真案例(8)——读取FBX格式文件并显示(无动画)...
  7. java猜数字代码,我写的猜数字游戏的java代码!
  8. 正交试验设计例题及答案_2020年一级建造师《市政工程》模拟试题及答案(5)...
  9. python mss_Python实现的连接mssql数据库操作示例
  10. linux6.8屏幕黑屏,centos6.8笔记本关盖就黑屏怎样设置不黑屏?
  11. CSS从大图片上截取小图标的使用
  12. 基于Springboot的医院药品管理系统的设计与实现.zip(论文+项目源码)
  13. 海外:国外最受欢迎的5个电子邮件服务网站
  14. 计算机月考分析报告,月考分析总结500字(通用7篇)
  15. 14215-97-5,1-O-Acetyl-2,3,5-tri-O-benzoyl-D-ribofuranose,1-O-乙酰基-2,3,5-三-O-苯甲酰基-D-呋喃核糖
  16. 手把手教你设置Typora的图床-gitee
  17. python知识点智能问答_基于检索的智能问答
  18. 12V转5V,3.3V和24V转5V,3.3V的芯片功能介绍
  19. OpenGL中常用的 GLUT 函数
  20. JAVA实现简单“伪植物大战僵尸“游戏

热门文章

  1. 红米K40进入adb模式
  2. 智能家居终于起风了,三派玩家谁能收割市场?
  3. python倾向匹配得分_临床研究的最后一道防线(四):倾向性评分匹配PSM在Python的实现...
  4. Ubuntu上安装ffmpeg
  5. iPhone商店9大免费APP!省钱省心、穷人必备
  6. petshop详解之六:PetShop之表示层设计
  7. JavaScript-表格隔行换色·鼠标上移换色·合并显示
  8. 2021金山训练营作业第一周作业
  9. 发现一个相当好的食物热量计算器!
  10. U3DSPACESHOOT个人重制版。代码分享(五)其他代码部分