代码主体及说明

Javascript部分:

/**
* @函数名:flexScroll
* @功能:滚动超出一定高度,指定元素悬浮
* @两个参数:target_id:目标元素id;topEle:限定滚动高度,超过之后,导航条悬浮
* @调用方式:e.g.:flexScroll('nav',80);
* @author:Kevin 2015/8/14
*/ function flexScroll(target_id,topEle){ window.onscroll=function(){ var scrollTop=document.documentElement.scrollTop || document.body.scrollTop;
var scrollNav=document.getElementById(target_id);
if(scrollTop>topEle){
scrollNav.classList.add('slideDown');
scrollNav.style.position="fixed";
scrollNav.style.top="0";
}else{
scrollNav.classList.remove('slideDown');
scrollNav.style.position="static";
} }
}

  

Css动画部分:

  .slideDown {
-webkit-animation: slideDown .5s linear;
animation: slideDown .5s linear;
-webkit-animation-fill-mode: both;
-moz-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
@-webkit-keyframes slideDown{
0%{-webkit-transform:translateY(-2rem)}
100%{-webkit-transform:translateY(0)}
}

  

应用示例

在线演示:http://codepen.io/anon/pen/VLNBgN

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>JavaScript:浮动的导航条</title>
<style type="text/css">
*
{
margin: 0;
padding: 0;
}
.content
{
width: 640px;
height: 1800px;
margin: 0 auto;
background-color: #f5f5f5;
}
.nav
{
line-height: 40px;
width: 640px;
height: 40px;
background-color: #333;
}
.nav li
{
position: relative;
float: left;
width: 25%;
list-style: none;
text-align: center;
color: #fff;
}
.nav li:after
{
content: '';
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
width: 1px;
height: 20px;
margin: auto;
background-color: #999;
}
.nav li:last-child:after
{
display: none;
}
.placehold
{
width: 100%;
height: 80px; background-color: red;
} /*动画效果*/
.slideDown
{
-webkit-animation: slideDown .5s linear;
animation: slideDown .5s linear;
-webkit-animation-fill-mode: both;
-moz-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
@-webkit-keyframes slideDown
{
0%
{
-webkit-transform: translateY(-2rem);
}
100%
{
-webkit-transform: translateY(0);
}
} </style>
</head>
<body> <div class="content">
<!--占位-->
<div class="placehold"></div> <!--代码主体-->
<div class="nav" id="nav">
<ul>
<li>足球</li>
<li>篮球</li>
<li>苹果</li>
<li>芒果</li>
</ul>
</div> </div> <script>
function flexScroll(target_id,topEle){ window.onscroll=function(){ var scrollTop=document.documentElement.scrollTop || document.body.scrollTop;
var scrollNav=document.getElementById(target_id);
if(scrollTop>topEle){
scrollNav.classList.add('slideDown');
scrollNav.style.position="fixed";
scrollNav.style.top="0";
}else{
scrollNav.classList.remove('slideDown');
scrollNav.style.position="static";
} } }
//调用
flexScroll('nav',80); </script>
</body>
</html>

  

Javascript:浮动的导航条的更多相关文章

  1. JQuery Mobile+JS实现智能浮动定位导航条

    实现原理 主要用到几个知识点: 什么是scrollTop? CSS position定位 判断是否为IE6浏览器 元素相对于窗口的距离 原理:1,浏览器向下滚动时,当document的scrollTo ...

  2. Bootstrap系列 -- 41. 带表单的导航条

    有的导航条中会带有搜索表单,在Bootstrap框架中提供了一个“navbar-form”,使用方法很简单,在navbar容器中放置一个带有navbar-form类名的表单.navbar-left”让 ...

  3. Bootstrap_导航条

    一.基础导航条 在制作一个基础导航条时,主要分以下几步: 第一步:首先在制作导航的列表(<ul class=”nav”>)基础上添加类名“navbar-nav” 第二步:在列表外部添加一个 ...

  4. Bootstrap每天必学之导航条

    http://www.jb51.net/article/75534.htm Bootstrap每天必学之导航条,本文向大家讲解了多种多样的导航条,以及导航条中元素的实现方法,感兴趣的小伙伴们可以参考一 ...

  5. 【CSS】 一个简单的导航条

    今天来做一个导航条! 首先写一个坯子: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &quo ...

  6. 玩转Bootstrap(基础) -- (6.导航条基础)

    1.导航条样例 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...

  7. bootstrap基础学习【导航条、分页导航】(五)

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

  8. Bootstrap之导航条

    基本导航条 <!-- navbar-inverse相反颜色风格 --> <!-- navbar-static-top去除圆角 --> <!-- navbar-fixed- ...

  9. css制作漂亮彩带导航条菜单

    点击这里查看效果:http://keleyi.com/keleyi/phtml/divcss/17.htm 效果图: 以下是源代码: <!DOCTYPE html PUBLIC "-/ ...

随机推荐

  1. JSP验证码

    ImageServlet.java package cn.hist.test.servlet; import java.awt.Color; import java.awt.Font; import ...

  2. css布局之负margin妙用及其他实现

    相信大家在项目的开发中都遇到过这样的需求,一行放X(X>1)个块且相邻块之间的间距相同. 大概就是上面这个样子,下面介绍几种实现的方式. 1.负margin大法 设置好元素的宽度和留白占满父级的 ...

  3. css3 3D变换和动画

    3D变换和动画 建立3D空间,transform-style: preserve-3d perspective: 100px; 景深 perspective-origin:center center ...

  4. Nginx配置文件nginx.conf详解(转)

    #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...

  5. ajax上传文件进度条

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. intellij idea 热部署失效,需要手动编译类

    从网上看到的解决方案,做一下备忘: spring boot项目中遇到jrebel类需要手动编译才会触发热部署的问题(spring boot devtools一样的问题) 1.ctl + shift + ...

  7. C#中英文混合字符串过长截断

    /// <summary> /// 截断字符串 /// </summary> /// <param name="maxLength">最大长度& ...

  8. 转 常用JQuery插件整理

    虽然自己也写过插件,但JQuery插件种类的繁多,大多时候,我还是使用别人写好的插件,这些都是我用了同类插件里较为不错的一些,今天就整理一下公开放出来. UI: jquery.HooRay(哈哈,自己 ...

  9. ubuntu下 使用AB做压力测试

    1最近刚开始接触apache大数据下数据优化,讲一下apache 下ab压力测试工具. 程序“ab”尚未安装. 您可以使用以下命令安装: apt-get install apache2-utils 以 ...

  10. linux下实现rm()函数删除文件或目录

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600866.html 在linux下有两个函数可以用来删除文件: #include <unistd.h ...