http://www.cnblogs.com/lhj588/archive/2013/04/02/2994639.html

————————————————————————————————————————————————————————————

jQuery&CSS 顶部和底部固定浮动工具栏 兼容IE6

http://www.bluesdream.com

18

December

2012

现在常常能看到一些网站(如:新浪微博和花瓣)导航条或工具栏固定在网页的顶部或其他地方。这样的布局方式,能便于用户点击和“曝光率”,不用每次都要把网页拖动到某个特定位置才能点击或看到。

其实这样的布局方式很早就有,只是没有那么个契机推广开吧。做起来也不复杂,只要设置一个小小的属性“position:fixed”便能完成,最关键的无不呼在于要兼容IE6而已。

首先我们来看HTML代码,是不是超简单?这里提供的只是一个简单的框架,实际应用的时候,只要把想要的元素添加东西就在这区域内加就行。

HTML CODE:

<div id="header">Default header. No absolute and fixed.</div>
<div id="topToolbar">Fixed at the top of the Toolbar.</div>
<div id="content">
    <p>此处省略1000字...</p>
</div>
<div id="bottomToolbar">Fixed at the bottom of the Toolbar. By Bluesdream.com</div>

CSS CODE:

<style type="text/css">
/* 全局CSS*/
*{margin:0pxpadding:0px;}
a{ text-decoration:noneoutline:none;}
a:hover{text-decoration:underline;}
 
/* 实例CSS */
html{ _background:url(about:blank);} /*阻止闪动 in IE6 , 把空文件换成about:blank , 减少请求*/
body{ font-size:12pxfont-family:Arial,Tahoma,sans-serifcolor:#EEEEEEtext-align:centerbackground:#E2E2E2;}
#topToolbar{
    _display:none;
    width:100%height:40pxline-height:40px;
    background:#101010border-bottom:2px solid #409F89;
    position:fixedtop:-40pxleft:0;
    _position:absolute_top:0;
}
 
#bottomToolbar{
    width:100%height:40pxline-height:40px;
    background:#101010border-top:2px solid #409F89;
    position:fixedbottom:0left:0;
    _position:absolute_top:expression(documentElement.scrollTop+documentElement.clientHeight-this.offsetHeight);
    /*
        document.body.scrollTop 网页滚动的距离
        document.body.clientHeight 网页可见区域高
        this.offsetHeight 当前元素的高度
    */
}
#bottomToolbar a{ color:#FFF;}
 
#header{
    width:100%height:80pxline-height:80px;
    background:#101010border-top:2px solid #409F89;
}
 
#content{
    width:880pxheight:1390pxline-height:18pxtext-align:left;
    margin:40px auto 80px autopadding:30px 50px;
    background:#FFF url(images/scaleplate.png) no-repeatborder:1px solid #CCC;
}
</style>

JAVASCRIPT CODE:

<script type="text/javascript">
$(function(){
    $(window).scroll(function() {
        var topToolbar = $("#topToolbar");
        var headerH = $("#header").outerHeight();
        var scrollTop = $(document).scrollTop();
        //IE6 Expression方法和jquery animate方法同事使用会有问题,所以加个判断,简化下IE6下的显示方式.
        if ($.browser.msie && ($.browser.version == "6.0") && !$.support.style) {
            if( scrollTop >= headerH ){
                topToolbar.show();
            }else if( scrollTop < headerH ){
                topToolbar.hide();
            }
        }else{
            if( scrollTop >= headerH ){
                topToolbar.animate({ 'top':0 });
            }else if( scrollTop < headerH ){
                topToolbar.animate({ 'top':-40 });
            }
        };
    });
});
</script>

DemoDownload

如果要像新浪微博那样,始终浮动固定在顶部,那JS部分就可以无视了,有没有都无所谓,直接改CSS就行。

#topToolbar{
    width:100%height:40pxline-height:40px;
    background:#101010border-bottom:2px solid #409F89;
    position:fixedtop:0left:0;
    _position:absolute_top:expression(documentElement.scrollTop);
}

jQuery&CSS 顶部和底部固定浮动工具栏 兼容IE6的更多相关文章

  1. jquery返回顶部和底部插件和解决ie6下fixed插件

    (function($){ //返回顶部和底部插件 $.fn.extend({ goTopBootom:function (options){ //默认参数 var defaults = { &quo ...

  2. jquery垂直滚动插件一个参数用于设置速度,兼容ie6

    利用外层的块级元素负外边距来滚动 1.使用 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://ww ...

  3. jQuery 顶部导航尾随滚动,固定浮动在顶部

    jQuery 顶部导航尾随滚动.固定浮动在顶部 演示 XML/HTML Code <section> <article class="left"> < ...

  4. html中header,footer分别固定在顶部和底部

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>page01</title> 5 <styl ...

  5. div footer标签css实现位于页面底部固定

    Web页面的“footer”部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,让你的页面看上去很不好看,特别是现在宽屏越来越多,这种现象更是常见,本文将介绍两种解决方案,需要了解的朋友可以 ...

  6. 页面元素固定在页面底部的纯css代码(兼容IE6)

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

  7. 微信小程序之顶部固定和底部固定

    顶部固定 <view style="position:fixed;top:0;"> ...... </view> 底部固定 <view style=& ...

  8. bootstrap让footer固定在顶部和底部

    一.原理 使用fixed进行固定定位,相对于浏览器窗口进行定位,然后再设置其z-index的值即可. 二.Bootstrap中使用的类 1.顶部固定:navbar-fixed-top 2.底部固定:n ...

  9. [jquery]判断页面滚动到顶部和底部(适用于手机web加载)

    //判断页面滚动到顶部和底部 $(window).scroll(function(){ var doc_height = $(document).height(); var scroll_top = ...

随机推荐

  1. 微信小程序-实现分享(带参数)

    微信小程序分享功能的实现方法有两种: 第一种 在page.js中实现onShareAppMessage,便可在小程序右上角选择分享该页面 onShareAppMessage: function () ...

  2. 在linux中配置环境变量

    JDK下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 根据Linux ...

  3. 牛腩学用MUI做手机APP

    斗鱼直播间直播学习撸码,最终目标是用MUI做一个手机APP(暂定android平台,攒钱买IPHONE 7SE!!!),直播内容含整个软件APP的制作过程(含后台接口的制作,放到自己买的阿里云服务器, ...

  4. PS辅助工具Assistor PS

    Assistor PS是一个功能强大的PS辅助工具,它可以切图.标坐标.尺寸.文字样式注释.画参考线等功能,可以为设计师节省很多时间.该PS工具原本需要每月付费$9.99美元,但在今年6月开始,将免费 ...

  5. zabbix 对服务器的负载做监控

    # cat /etc/zabbix/zabbix_agentd.d/average.conf UserParameter=average[*],uptime|awk '{print $NF}' 自定义 ...

  6. 源码安装mysql5.6x启动报错:[ERROR] Can't find messagefile '/data/mysqldata/3306/english/errmsg.sys'

    :: mysqld_safe Starting mysqld daemon with databases from /data/mysqldata//data -- :: [Warning] The ...

  7. 菜鸟调错(三)——Jboss与jdk版本不兼容导致WebService调用出错

    环境: jdk1.6 Jboss 5.1.0.GA 问题描述: EJB发布webserivce已经成功,并且能够成功访问wsdl文件: 使用axis1自带的sample/client下的类Dynami ...

  8. centos安装man中文手册

    第一步下载man中文手册压缩包 //下载 wget http://pkgs.fedoraproject.org/repo/pkgs/man-pages-zh-CN/manpages-zh-1.5.1. ...

  9. C++中的static 成员变量的一些注意点

    C++中的static成员变量主要用来为多个对象共享数据 例: #include <iostream> using namespace std; class Student{ public ...

  10. 一文读懂Redis持久化

    Redis 是一个开源( BSD 许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件.它支持的数据类型很丰富,如字符串.链表.集合.以及散列等,并且还支持多种排序功能. 什么叫持久 ...