承接 webcito/bs-touchspin 相关项目开发

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

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

webcito/bs-touchspin

Composer 安装命令:

composer require webcito/bs-touchspin

包简介

A jQuery touchspin input plugin requiring Bootstrap 5.

README 文档

README

bsTouchspin is a lightweight, flexible, and customizable jQuery plugin that provides an easy-to-use spinner interface for numeric inputs. It allows for smooth increment and decrement of numeric values through up and down buttons, while supporting custom configurations for appearance and behavior.

img.png

Features

  • Increment and decrement buttons for numeric inputs.
  • Customizable options:
    • Minimum and maximum values.
    • Step size (fixed or dynamic).
    • Input size (sm | lg).
    • Prefix and postfix spans.
    • Custom button styles, icons, and behavior.
  • Built-in number formatting options:
    • Currency.
    • Percent.
    • Number.
  • Locale support for number formatting.
  • Customizable events and callbacks, such as onInit, onStart, and onStop.
  • Utility methods for formatting and handling decimals.
  • Responsiveness and accessibility.
  • Support for dynamic and real-time changes.

Installation

Using a Content Delivery Network (CDN)

Include the following scripts and styles in your HTML file:

<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script src="path/to/bs-touchspin.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css">

Local Installation

  1. Download the bs-touchspin script file and its dependencies (e.g., Bootstrap CSS/JS, Bootstrap Icons).
  2. Include them in your project.

Usage

Basic Initialization

The simplest use case involves turning a standard text input into a touchspin spinner:

<input id="spinner" type="text">
<script>
    $('#spinner').bsTouchspin();
</script>

Options and Configuration

The plugin supports a wide range of customization options, as listed below:

General Configuration

The following global configuration options allow fine-tuning of touchspin behavior:

Option Description Data Type Default
minSpeed The minimum speed (in ms) for holding the increment or decrement button. number 1
startSpeed The initial speed (in ms) when holding the increment or decrement button. number 600
delay Delay (in ms) before triggering the stop callback after releasing a button or leaving focus. number 1000
locale Defines the locale used for number formatting (e.g., 'en-US', 'de-DE', etc.). string 'en-US'
maximumMax The value that is taken for max if max has not been defined. number|null 2.147.483.647
maximumMin The value that is taken for min if min is not defined. number|null -2.147.483.648
inputMinWidth Minimum width in pixels for the input field and formatted output. number 75

Changing Global Configuration

You can change the global configuration dynamically at runtime by using the $.bsTouchspin.setConfig() method:

$.bsTouchspin.setConfig({
    minSpeed: 10,        // Set the minimum holding speed to 10ms
    startSpeed: 500,     // Start speed reduced to 500ms
    delay: 800,          // Delay before onStop callback to 800ms
    locale: 'de-DE'      // Use German locale for number formatting
});

This will update the behavior of all future bsTouchspin instances created after the configuration is set.

Instance-Specific Configuration

Pass configuration options during initialization:

$('#spinner').bsTouchspin({
    size: 'sm',
    step: 0.5,
    min: 10,
    max: 50,
    formatter: 'percent',
    onStart: function (value) {
        console.log('Started with value:', value);
    },
    onStop: function (value, diff) {
        console.log('Stopped with value:', value, 'diff to start: ', diff);
    }
});

Note:

If step, min, or max are set as HTML attributes on the <input>, those attribute values take precedence over plugin options during initialization.

Configuration Options

Below is the full list of default options for the bsTouchspin plugin:

Option Description Data Type Default
size Sets the size of the input. Acceptable values: null, sm, or lg. string or null null
step Defines the step size for increments or decrements. Set to "any" for dynamic step size. number or string "any"
decimals Fixed number of decimal places for value normalization and display. If set, it overrides step-derived decimals. number or null null
min The minimum value allowed for the input. number or null null
max The maximum value allowed for the input. number or null null
prefix Prefix text or symbol shown before the input value. string or null null
postfix Postfix text or symbol shown after the input value. string or null null
allowInput Allows manual input of a value in the input field. boolean true
buttons.up.class CSS classes for the increment ("up") button. string 'btn-secondary rounded-end-pill fw-bold'
buttons.up.icon Icon for the increment ("up") button (uses Bootstrap Icons). string 'bi bi-plus-lg'
buttons.up.iconSetZero Icon for the increment button when the value reaches zero. string 'bi bi-trash'
buttons.down.class CSS classes for the decrement ("down") button. string 'btn-secondary rounded-start-pill fw-bold'
buttons.down.icon Icon for the decrement ("down") button (uses Bootstrap Icons). string 'bi bi-dash-lg'
buttons.down.iconSetZero Icon for the decrement button when the value reaches zero. string 'bi bi-trash'
formatter Formatting style for the input value. Acceptable values: 'number', 'currency', 'percent', or a custom formatter function. Function receives (value, decimals, locale, currency) and should return a display string. string or function 'number'
currency ISO currency code used when formatter: 'currency' is active (for example EUR, USD, GBP). string 'EUR'
onInit Callback function, executed during initialization. function function (value) {}
onStart Callback function, executed when incrementing or decrementing starts. function function (value) {}
onStop Callback function, executed when incrementing or decrementing stops. function function (value, diff) {}

Methods

val

The val method allows you to set the spinner's value programmatically.

Usage:

$('#example-spinner').bsTouchspin('val', value);
  • value: The numeric value you want to set for the spinner.

Examples:

  1. Set the value of the spinner:
$('#example-spinner').bsTouchspin('val', 42); // Sets the spinner's value to 42.

Notes:

  • Validation: The provided value is validated against the configured min, max, and step properties. If the value exceeds the defined limits, it will be automatically adjusted:
$('#example-spinner').bsTouchspin({
    min: 0,
    max: 100
});

$('#example-spinner').bsTouchspin('val', 150); // The value will be set to 100 since it exceeds the `max`.
  • The applied value respects any configured formatting via options like formatter (e.g., currency or percentage).
  • Direct external value assignments like $input.val('1') or $input.prop('value', '1') are not automatically processed by the plugin UI lifecycle. To keep validation, formatting, button states, and width updates in sync, use $('#example-spinner').bsTouchspin('val', 1).
  • For fixed precision independent from step size, set decimals explicitly. Example: step: 0.01 with decimals: 4 increments by 0.01 and displays values like 1.2300.

setPrefix

Updates the prefix text at runtime.

$('#example-spinner').bsTouchspin('setPrefix', '€');

setPostfix

Updates the postfix text at runtime.

$('#example-spinner').bsTouchspin('setPostfix', 'kg');

destroy

Removes plugin markup, event handlers, and plugin data from the input and restores original input attributes/state.

$('#example-spinner').bsTouchspin('destroy');

Events

The plugin emits the following events that can be listened to:

  • init.bs.touchspin: Triggered on initialization.
  • start.bs.touchspin: Triggered when the spinner interaction starts.
  • stop.bs.touchspin: Triggered when the spinner interaction stops.

Example Usage:

$('#spinner').on('init.bs.touchspin', function (event, startValue) {
    console.log('Touchspin initialized:', startValue);
});

$('#spinner').on('stop.bs.touchspin', function (event, stopValue, diff) {
    console.log('Touchspin stopped. Final value:', stopValue, 'Difference:', diff);
});

Event payloads:

  • init.bs.touchspin => (event, startValue)
  • start.bs.touchspin => (event, startValue)
  • stop.bs.touchspin => (event, stopValue, diff)

Global API

The plugin namespace also exposes global defaults/config helpers:

  • $.bsTouchspin.setDefaults(options)
  • $.bsTouchspin.getDefaults()
  • $.bsTouchspin.setConfig(options)
  • $.bsTouchspin.getConfig()

Example:

$.bsTouchspin.setDefaults({
    buttons: {
        up: {class: 'btn-primary'},
        down: {class: 'btn-primary'}
    }
});

Utility Functions

The plugin provides useful utility functions for working with numeric values:

formatNumber(number, decimalPlaces, locale)

Formats a number with the desired decimal places and locale.

const formatted = $.bsTouchspin.utils.formatNumber(1234.56, 2, 'de-DE');
console.log(formatted); // "1.234,56"

formatCurrency(value, decimals, locale, currency)

Formats a value as currency.

const currency = $.bsTouchspin.utils.formatCurrency(200, 2, 'en-US', 'USD');
console.log(currency); // "$200.00"

formatPercent(value, decimals, locale)

Formats a value as a percentage.

const percent = $.bsTouchspin.utils.formatPercent(0.45, 2, 'de-DE');
console.log(percent); // "45,00%"

Example Integration

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <script src="path/to/bs-touchspin.min.js"></script>
</head>
<body>
<div class="container">
    <label for="example-spinner">Example Spinner</label>
    <input id="example-spinner" type="text"/>
</div>

<script>
    $('#example-spinner').bsTouchspin({
        step: 1,
        min: 0,
        max: 100,
        formatter: 'currency'
    });
</script>
</body>
</html>

Contributing

Feel free to fork the repository, open issues, or submit pull requests to improve the plugin. Contributions, feedback, and suggestions are highly welcome!

License

Licensed under the MIT License.

Author

This plugin was developed to enhance user interactions with input fields and provide a modern interface for numeric controls in web applications.

webcito/bs-touchspin 适用场景与选型建议

webcito/bs-touchspin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「jquery」 「ui」 「bootstrap」 「input」 「widget」 「spinner」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 webcito/bs-touchspin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 webcito/bs-touchspin 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 39
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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