floatdiv 是一款 jQuery 插件,可讓指定層固定在頁面任意位置,滾動時保持位置不變、無閃動。支持預設九大方位及自定義坐標,兼容 IE6+。
{left, top} 坐標// 1. 默認右下角
$('#id').floatdiv();
// 2. 內置位置關鍵字
$('#id').floatdiv('rightbottom'); // 右下角
$('#id').floatdiv('leftbottom'); // 左下角
$('#id').floatdiv('righttop'); // 右上角
$('#id').floatdiv('lefttop'); // 左上角
$('#id').floatdiv('middle'); // 居中
$('#id').floatdiv('middletop'); // 頂部居中
$('#id').floatdiv('middlebottom');// 底部居中
$('#id').floatdiv('leftmiddle'); // 左側居中
$('#id').floatdiv('rightmiddle'); // 右側居中
// 3. 自定義坐標
$('#id').floatdiv({left: '10px', top: '10px'});
jQuery.fn.floatdiv = function(location) {
// 1. 檢測 IE6
// 2. 獲取視口尺寸
// 3. 根據 location 計算目標位置 loc
// 4. 創建包裹層 wrap,設置 position:fixed, z-index:999
// 5. IE6 使用 CSS Expression 模擬 fixed
// 6. 將目標元素移入 wrap
// 7. 返回 this 支持鏈式調用
};
expression 動態計算 top 值跟隨滾動background-attachment:fixed + 透明背景圖防止抖動position:absolute 覆蓋 fixed現代替代: CSS
position: sticky或position: fixed可原生實現大部分需求,無需 JS 插件。