承接 webcito/jquery-affix 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 offsetTop to 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

  1. Include jQuery in your project (if not already included):
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
  1. Add the affix plugin 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:

  1. Initialization:

    • The plugin processes each element and attaches settings like offsetTop and breakpoint.
    • Registers a ResizeObserver for dynamic recalculations.
  2. 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.
  3. Positioning:

    • Applies position: sticky and adjusts top and z-index to maintain order and avoid overlaps.

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: sticky property. Check Can I Use for browser compatibility.

  • Element not sticking at the correct position: Double-check the offsetTop setting and ensure no margin or transform styles 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 webcito/jquery-affix 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 40
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-14