webcito/jquery-affix
Composer 安装命令:
composer require webcito/jquery-affix
包简介
A lightweight jQuery plugin that provides a flexible sticky behavior with configurable options and breakpoints.
README 文档
README
The jQuery Affix Plugin allows you to create sticky elements on your webpage with configurable offsets and optional breakpoints. It is lightweight, easy to use, and supports multiple affix elements.
Features
- Sticky positioning using CSS
position: sticky. - Configurable
offsetTopto adjust the sticky position. - Optional breakpoints to activate the sticky functionality only at specific screen widths.
- Automatically handles multiple affix elements with proper stacking (
z-index) and offsets. - Compatible with modern web browsers.
Installation
- Include jQuery in your project (if not already included):
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
- Add the
affixplugin script to your project:
<script src="dist/jquery-affix.min.js"></script>
Usage
To use the affix plugin, call the .affix() method on the desired element.
You can pass optional configurations (as described below).
Basic Example
<div id="my-sticky-element"> I am a sticky element! </div> <script> $(document).ready(function () { $('#my-sticky-element').affix(); }); </script>
In this example, the #my-sticky-element will stick to the top of its container when you scroll past it.
Configuration Options
The plugin provides the following configuration options:
| Option | Type | Default | Description |
|---|---|---|---|
offsetTop |
number (pixels) |
0 |
Additional spacing to apply at the top before the sticky styling takes effect. |
breakpoint |
number or string |
null |
Minimum screen width (in pixels or predefined breakpoint) to enable sticky. |
Predefined Breakpoints:
You can use the following values for the breakpoint option:
| Breakpoint Name | Screen Width (px) |
|---|---|
sm |
576 |
md |
768 |
lg |
992 |
xl |
1200 |
xxl |
1400 |
any number |
2000 |
Events
The Affix element fires three events:
$(document) .on('init', '#my-sticky-element', function (){ // I am now part of the Affix Plugin }) .on('affixed', '#my-sticky-element', function (){ // I was pinned }) .on('unaffixed', '#my-sticky-element', function (){ // I was unpinned })
Example with Options
<div id="custom-sticky-element"> I will stick with custom settings! </div> <script> $(document).ready(function () { $('#custom-sticky-element').affix({ offsetTop: 50, // Stick 50px below the viewport top breakpoint: 'md' // Activate sticky only on screens >= 768px wide }); }); </script>
Advanced Usage
Multiple Sticky Elements
The plugin automatically handles multiple sticky elements and ensures that each subsequent element takes into account the height of the previous ones. Here's an example:
<div class="affix-item">Element 1</div> <div class="affix-item">Element 2</div> <script> $(document).ready(function () { $('.affix-item').affix({ offsetTop: 10 // Each element will stick 10px below the previous one }); }); </script>
Directly Specifying Breakpoints
You can pass numerical values as breakpoints instead of predefined strings:
<div id="dynamic-breakpoint"> I stick on screens >= 1000px wide. </div> <script> $(document).ready(function () { $('#dynamic-breakpoint').affix({ breakpoint: 1000 // Activate sticky only on screens >= 1000px wide }); }); </script>
How It Works
Here is a brief overview of the plugin's internal behavior:
-
Initialization:
- The plugin processes each element and attaches settings like
offsetTopandbreakpoint. - Registers a
ResizeObserverfor dynamic recalculations.
- The plugin processes each element and attaches settings like
-
Sticky Logic:
- Calculates offsets for each sticky element, based on its position in the DOM and the height of prior elements.
- Uses predefined or custom breakpoints to enable/disable sticky positioning dynamically.
-
Positioning:
- Applies
position: stickyand adjuststopandz-indexto maintain order and avoid overlaps.
- Applies
Browser Support
This plugin relies on the browser's support for the CSS position: sticky property. Ensure your users have a modern
browser for full functionality.
Tested on:
- Google Chrome (latest versions)
- Mozilla Firefox (latest versions)
- Microsoft Edge
Troubleshooting
-
Sticky not working on older browsers: Ensure the browser supports the CSS
position: stickyproperty. Check Can I Use for browser compatibility. -
Element not sticking at the correct position: Double-check the
offsetTopsetting and ensure nomarginortransformstyles are interfering with the sticky behavior.
License
This plugin is open-source and available under the MIT License.
Author
This plugin was created to simplify sticky functionality for dynamic web interfaces using jQuery.
If you have any feedback or questions, feel free to reach out!
webcito/jquery-affix 适用场景与选型建议
webcito/jquery-affix 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「jquery」 「plugin」 「responsive」 「sticky」 「scroll」 「affix」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webcito/jquery-affix 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webcito/jquery-affix 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webcito/jquery-affix 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
ResponsiveImages Plugin for October CMS
TYPO3 extension for sending responsive email templates
This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 40
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-14