吸顶灯是各站点常用的一个功能,它有两个特性
div可能是一个分类菜单,也可能是一个文章导航。如

实现思路如下
.fixed {
position: fixed;
top: 0;
z-index: 100;
-position: absolute;
-top: expression(eval(document.documentElement.scrollTop))
}
jQuery插件的实现代码如下
/*
* 吸顶灯
* option {
* fixCls: className,默认 “fixed”
* fixedFunc: 吸顶时回调函数
* resetFunc: 不吸顶时回调函数
* }
*/
$.fn.topSuction = function(option) {
option = option || {}
var fixCls = option.fixCls || ‘fixed‘
var fixedFunc = option.fixedFunc
var resetFunc = option.resetFunc
var $self = this
var $win = $(window)
if (!$self.length) return
var offset = $self.offset()
var fTop = offset.top
var fLeft = offset.left
// 暂存
$self.data(‘def‘, offset)
$win.resize(function() {
$self.data(‘def‘, $self.offset())
})
$win.scroll(function() {
var dTop = $(document).scrollTop()
if (fTop < dTop) {
$self.addClass(fixCls)
if (fixedFunc) {
fixedFunc.call($self, fTop)
}
} else {
$self.removeClass(fixCls)
if (resetFunc) {
resetFunc.call($self, fTop)
}
}
})
};
这里分别提供了两个回调,fixedFunc在fixed后调用,resetFunc在恢复到初始状态时调用。
原文:http://www.cnblogs.com/snandy/p/3994713.html