@better-scroll/wheel is a plugin for BetterScroll that implements an iOS Picker-like wheel component for mobile and web applications. It provides smooth scrolling functionality with 3D rotation effects, configurable wheel items, and support for disabled states.
npm install @better-scroll/wheelimport BScroll from '@better-scroll/core';
import Wheel, { WheelOptions, WheelConfig } from '@better-scroll/wheel';For CommonJS:
const BScroll = require('@better-scroll/core');
const { default: Wheel, WheelOptions, WheelConfig } = require('@better-scroll/wheel');import BScroll from '@better-scroll/core';
import Wheel from '@better-scroll/wheel';
// Register the plugin
BScroll.use(Wheel);
// Initialize with wheel options
const bs = new BScroll('.wheel-wrapper', {
wheel: {
selectedIndex: 0,
rotate: 25,
adjustTime: 400,
wheelWrapperClass: 'wheel-scroll',
wheelItemClass: 'wheel-item',
wheelDisabledItemClass: 'wheel-disabled-item'
},
probeType: 3
});
// Use wheel methods
bs.wheelTo(2, 300); // Scroll to index 2 with 300ms animation
const currentIndex = bs.getSelectedIndex(); // Get current selected index
bs.restorePosition(); // Restore position if needed
// Listen for index changes
bs.on('wheelIndexChanged', (index) => {
console.log('Selected index:', index);
});The wheel plugin integrates with BetterScroll through:
BScroll.use(Wheel) to register the pluginwheelTo, getSelectedIndex, restorePosition) directly on BScroll instancesRegister the wheel plugin with BetterScroll to enable wheel functionality.
export default class Wheel implements PluginAPI {
static pluginName: string;
constructor(scroll: BScroll);
}
// Named exports available
export type WheelOptions = Partial<WheelConfig> | true;
export interface WheelConfig;
// External types from dependencies
type BScroll = import('@better-scroll/core').default;
// Easing function type from @better-scroll/shared-utils
type EaseItem = {
style: string;
fn: (t: number) => number;
};Navigate to specific wheel positions with optional animation timing and easing.
/**
* Scroll wheel to specified index position
* @param index - Target index (defaults to 0)
* @param time - Animation duration in milliseconds (defaults to 0)
* @param ease - Easing function for animation
*/
wheelTo(index?: number, time?: number, ease?: EaseItem): void;Get the currently selected wheel index.
/**
* Get the currently selected index
* @returns Current selected index number
*/
getSelectedIndex(): number;Restore wheel to correct position when interrupted during scrolling.
/**
* Restore wheel to correct position if scrolling is interrupted
* Clears any pending animations and snaps to nearest valid position
*/
restorePosition(): void;The wheel plugin emits events when the selected index changes.
// Event name constant
const WHEEL_INDEX_CHANGED_EVENT_NAME = 'wheelIndexChanged';
// Listen for index changes
bs.on('wheelIndexChanged', (index: number) => {
// Handle index change
});Configure wheel behavior through the wheel options object.
type WheelOptions = Partial<WheelConfig> | true;
interface WheelConfig {
/** Initial selected index (default: 0) */
selectedIndex: number;
/** Rotation angle in degrees for 3D effect (default: 25) */
rotate: number;
/** Animation duration for position adjustments in ms (default: 400) */
adjustTime: number;
/** CSS class name for wheel wrapper element (default: 'wheel-scroll') */
wheelWrapperClass: string;
/** CSS class name for wheel item elements (default: 'wheel-item') */
wheelItemClass: string;
/** CSS class name for disabled wheel items (default: 'wheel-disabled-item') */
wheelDisabledItemClass: string;
}Configuration Examples:
// Enable with default options
const bs = new BScroll('.wrapper', {
wheel: true
});
// Custom configuration
const bs = new BScroll('.wrapper', {
wheel: {
selectedIndex: 2, // Start at index 2
rotate: 45, // Stronger 3D rotation
adjustTime: 600, // Slower animations
wheelWrapperClass: 'my-wheel',
wheelItemClass: 'my-item',
wheelDisabledItemClass: 'disabled'
}
});The plugin extends BetterScroll's TypeScript interfaces through module augmentation.
declare module '@better-scroll/core' {
interface CustomOptions {
wheel?: WheelOptions;
}
interface CustomAPI {
wheel: PluginAPI;
}
}
interface PluginAPI {
wheelTo(index?: number, time?: number, ease?: EaseItem): void;
getSelectedIndex(): number;
restorePosition(): void;
}The plugin requires a specific HTML structure with appropriate CSS classes:
<div class="wheel-wrapper">
<div class="wheel-scroll">
<div class="wheel-item">Option 1</div>
<div class="wheel-item">Option 2</div>
<div class="wheel-item wheel-disabled-item">Disabled Option</div>
<div class="wheel-item">Option 4</div>
</div>
</div>The plugin handles several edge cases automatically:
getSelectedIndex() returns -1wheelTo() automatically finds the nearest valid (non-disabled) itemrestorePosition() handles cases where scrolling is interrupted