本插件借鉴了一些网上的资料,实际运用可以自行修改。
/*! * jQuery scrollsPosition plugin * jquery滚动保持定位层 * Copyright (c) 2012 Geetaku * * @author JSCZXY2 * */ $.fn.scrollsPosition = function() { var position = function(element) { element.css("z-index","999"); var top = element.position().top, pos = element.css("position"); $(window).scroll(function() { var scrolls = $(this).scrollTop(); if (scrolls > top) { if (window.XMLHttpRequest) { element.css({ position: "fixed", top: 0 }); } else { element.css({ top: scrolls }); } }else { if(pos == "relative"){ element.css({ position: pos, }); }else{ element.css({ position: pos, top: top }); } } }); }; return $(this).each(function() { position($(this)); }); };