需求

页面向下滚动时,需要将顶部的搜索栏信息和导航菜单吸顶,并且,搜索栏信息和导航菜单之间可以切换。

效果

https://www.iguopin.com/index.php?m=&c=index&a=index

代码实现

HTML

	<div class="nav-search">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<img alt="Brand" src="../public/images/igplogo2.png" class="header-brand">
</a>
</div>
<div class="right_part noprint">
<form class="ip-group">
<div class="ip-box">
<div class="type">
<div class="text"></div>
<i class="layui-icon layui-icon-down" style="font-size: 18px;cursor: pointer;"></i>
<div class="dpdown">
<div class="option" id="1">社会招聘</div>
<div class="option" id="2">校园招聘</div>
<div class="option" id="3">高端职位</div>
<div class="option" id="4">实习职位</div>
</div>
</div>
<input
type="text"
name="jobkey"
id="top_search_input"
value=""
placeholder="请输入职位或企业名称"/>
</div>
<input type="submit" class="ip-btn" value="搜索">
<input type="hidden" name="act" id="top_search_type" value="QS_jobslist"/>
</form>
<div class="nav-btn">
<span class="nav-text">导航</span>
<span class="iconfont icon-other"></span>
</div>
<div class="qrcodes">
<img class="qrcode qr1" src="../public/images/index/gp_qrcode.png"></img>
<img class="qrcode qr2" src="../public/images/index/gpfb_qrcode.png"></img>
<div class="qrcode-tip-box">
<span class="tip1 active">关注最新招聘资讯</span>
<span class="tip2">获取简历投递状态</span>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
...
...
...
<div class="link-nav">
<div class="container">
<ul class="nav navbar-nav site_link">
<li><a target="_blank" href="www.iguopin.com">国聘行动</a></li>
<li><a target="_blank" href="https://zhaogong.iguopin.com/">线上招工</a></li>
<li><a target="_blank" href="https://zhaogong.iguopin.com/abroad">一带一路</a></li>
</ul>
<div class="clearfix"></div>
</div>
</div>

css

.nav-search {
width: 100%;
background-color: #fff;
z-index: 1000;
}
.navbar .container .navbar-header {
margin-right: 0;
float: none;
margin-left: 0;
width: 20%;
}
.navbar .container .right_part {
align-items: center;
width: 80%;
}
.nav-btn {
float: right;
display: none;
width: 70px;
margin-left: 40px;
font-size: 16px;
color: #c90808;
line-height: 85px;
cursor: pointer;
}
.nav-search-fixed {
position: fixed;
top: 0;
-webkit-box-shadow: 2px 2px 10px #ccc;
box-shadow: 2px 2px 10px #ccc;
}
.nav-search-fixed .navbar-header {
display: none;
}
.navbar .nav-search-fixed .container .right_part {
width: 100%;
}
.navbar .nav-search-fixed .container .right_part .ip-group {
margin-left: 100px;
}
.nav-search-fixed .nav-btn {
display: inline-block;
position: relative;
top: 2px;
}
...
...
...
.link-nav {
width: 100%;
border-bottom: 2px solid #c90808;
background: #fff;
box-sizing: border-box;
height: 72px;
.container {
width: 1200px;
margin: 0 auto;
height: 72px;
box-sizing: border-box; .navbar-nav {
width: 100%;
height: 72px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-pack: distribute;
justify-content: space-around;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
margin: 0;
padding: 0;
list-style: none; > li {
float: left;
position: relative;
display: block;
padding: 0;
margin: 0;
line-height: 72px;
list-style-type: none;
box-sizing: border-box;
}
} > .avatar-li {
&:hover {
.dropdown-menu {
display: block;
}
}
}
}
}
}
.container-fixed {
position: fixed;
z-index: 1000;
width: 1070px;
top: 0;
.navbar-nav {
height: 120px;
line-height: 120px;
> li {
line-height: 120px;
&.active, &:hover {
height: 120px;
}
}
}
}
.clearfix {
zoom: 1;
display: block;
}
}
.link-nav-fixed {
width: 1200px;
margin: 0 auto;
}

JS

        $(function () {
// 导航吸顶
fixedNav();
$(window).scroll(function () {
fixedNav();
})
});
// 页面滚动到一定高度,固定顶部导航
var flag = false;
function fixedNav() {
var top = $(document).scrollTop();
if (top >= 212) {
$('.nav-search').addClass('nav-search-fixed');
navChange();
} else {
$('.nav-search').removeClass('nav-search-fixed');
$('.ip-group').show();
$('.qrcodes').show();
$('.container').removeClass('container-fixed');
$('.link-nav').removeClass('link-nav-fixed');
}
};
// 定位导航内容切换
$('.nav-btn').click(function () {
flag = !flag;
navChange();
});
function navChange() {
if (flag) {
$('.nav-text').text("搜索");
$('.nav-btn').children('.iconfont').removeClass('icon-other').addClass('icon-search');
$('.ip-group').hide();
$('.qrcodes').hide();
$('.link-nav').addClass('link-nav-fixed');
$('.container').addClass('container-fixed');
} else {
$('.nav-text').text("导航");
$('.nav-btn').children('.iconfont').removeClass('icon-search').addClass('icon-other');
$('.ip-group').show();
$('.qrcodes').show();
$('.link-nav').removeClass('link-nav-fixed');
$('.container').removeClass('container-fixed');
}
}

总结

主要是scroll, scrollTop(), click, show(), hide方法和position: fixed等属性的理解和使用。需要注意的是,一开始就调用fixedNav(),是用于当页面刷新且未滚动,但页面已不在顶部的位置时(也就是刷新页面后看不到顶部导航的位置),出现吸顶的导航的效果。

jQuery实现页面导航内容定位效果,并支持内容切换的更多相关文章

  1. jQuery实现页面元素智能定位

    实现过程 Js侦听滚动事件,当页面滚动的距离(页面滚动的高度)超出了对象(要滚动的层)距离页面顶部的高度,即要滚动的层到达了浏览器窗口上边缘时,立即将对象定位属性position值改成fixed(固定 ...

  2. jQuery实现页面锚点滚动效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. jquery实现页面控件拖动效果js代码

    ;(function($) { var DragPanelId = "divContext"; var _idiffx = 0; var _idiffy = 0; var _Div ...

  4. 基于jquery实现页面loading加载效果

    实现loading 加载提示 ······ 透明遮罩 居中效果 具体代码如下: CSS样式部分: <style type="text/css"> .background ...

  5. JQuery 实现页面无刷新

    对于JQuery实现页面无刷新的效果,即:应用这个JQuery这个组件,可以实现在页面上加载数据库中的数据信息,但是并没有给用户页面刷新的感觉,这样既可以有效的进行数据交互,也可以不妨碍用户的其他操作 ...

  6. 简单实现弹出弹框页面背景半透明灰,弹框内容可滚动原页面内容不可滚动的效果(JQuery)

    弹出弹框 效果展示 实现原理 html结构比较简单,即: <div>遮罩层 <div>弹框</div> </div> 先写覆盖显示窗口的遮罩层div.b ...

  7. Vue 事件监听实现导航栏吸顶效果(页面滚动后定位)

    Vue 事件监听实现导航栏吸顶效果(页面滚动后定位) Howie126313 关注 2017.11.19 15:05* 字数 100 阅读 3154评论 0喜欢 0 所说的吸顶效果就是在页面没有滑动之 ...

  8. jquery mobile页面切换效果(Flip toggle switch)(注:jQuery移动使用的数据属性的列表。 )

    1.页面切换(data-transition)

  9. jquery的固定定位效果

    今天做了个固定定位的效果.比如对导航需要进行固定定位效果: 当没有滚动到导航下面,导航正常显示. 当滚动到导航下面,导航就固定到顶部. 这个效果使用了jquery的方法实现,具体思路为: 1)首先获取 ...

随机推荐

  1. 难道你还不知道Spring之事务的回滚和提交的原理吗,这篇文章带你走进源码级别的解读。

    上一篇文章讲解了获取事务,并通过获取的connection设置只读,隔离级别等:这篇文章讲事务剩下的回滚和提交. 事务的回滚处理 之前已经完成了目标方法运行前的事务准备工作.而这些准备工作的最大目的无 ...

  2. NOIP2020 自爆记

    Day -4 - 2459184 本学期第 14 周终于到来了,NOIP 只剩 5 周了. djq 进国集了,先以膜为敬. 晚上上 hb,hb 让我们记了几点要求: 认真读题,要一字一句读题,包括输入 ...

  3. Assemblytics鉴定基因组间SV

    Assemblytics, 发表在Bioinformaticshttp://www.ncbi.nlm.nih.gov/pubmed/27318204,鉴定基因组间SV. Githup,https:// ...

  4. 【机器学习与R语言】3-概率学习朴素贝叶斯(NB)

    目录 1.理解朴素贝叶斯 1)基本概念 2)朴素贝叶斯算法 2.朴素贝斯分类应用 1)收集数据 2)探索和准备数据 3)训练模型 4)评估模型性能 5)提升模型性能 1.理解朴素贝叶斯 1)基本概念 ...

  5. kubernetes整个基础环境的准备

    1.三台centos7,用CentOS-7-x86_64-Minimal-1708.iso安装的,记得统一选好时区,这三台会有etcd集群,其中一台做kubernetes服务端(也可以做三台服务端做负 ...

  6. linux系统中安装MySQL

    linux系统中安装MySQL 检查原来linux系统中安装的版本 rpm -qa | grep mysql 将其卸载掉 以 mysql-libs-5.1.71-1.el6.x86_64 版本为例 r ...

  7. promise.all的应用场景举例

    Promise.all方法 简而言之:Promise.all( ).then( )适用于处理多个异步任务,且所有的异步任务都得到结果时的情况. 比如:用户点击按钮,会弹出一个弹出对话框,对话框中有两部 ...

  8. angular中路由跳转并传值四种方式

    一.路由传值 步骤1 路由传递参数 注意 一定是要传递 索引值 let key = index 这种情况是在浏览器中可以显示对应的参数 这种的是问号 localhost:8080/news?id=2& ...

  9. 【leetocode】55. Jump Game

    You are given an integer array nums. You are initially positioned at the array's first index, and ea ...

  10. 容器的分类与各种测试(二)——vector部分用法

    向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器. 与string相同, vector 同属于STL(Standard Template Library, 标准 ...