@flourish/slider

中文文档
3.1.0发布于 2025/05/30BSD-3-ClauseES Module近 30 天下载 2035

Slider UI component

原始使用文档
文档目录19 节
  1. In the draw() function
  2. In the update() function
  3. Constructor
  4. Methods
  5. .draw()
  6. .update()
  7. .value()
  8. .value(value)
  9. .on(event, handler)
  10. Events
  11. change
  12. Settings
  13. Core settings
  14. height
  15. Style settings
  16. Axis options
  17. Label options
  18. Properties
  19. version

Flourish slider component

The Slider component provides a configurable slider control. We developed it for use in Flourish templates, but it’s open source and you can use it for anything.

import Slider from "@flourish/slider";

var slider = Slider("#year-slider")
    .domain([2000, 2017])
    .startLabel("2000").endLabel("2017")
    .snap(true)
    .on("change", function(year) {
        console.log("Year set to", year);
    })
    .draw();

It’s developed as an ES6 module, and you can use it with rollup.js and rollup-plugin-node-resolve, or another ES6-compatible module bundler. Or you can include the file directly from the web:

<script src="https://cdn.flourish.rocks/slider-v1.full.min.js"></script>

Slider uses D3. If you’re already using D3 on your page then you can include a much smaller file that uses the d3 object you already have rather than including D3 code in the file.

<script src="https://cdn.flourish.rocks/slider-v1.min.js"></script>

If you’re not using D3 for other purposes, it’s more efficient to use the -full bundle, because it’s built using rollup to only include the elements of D3 that it actually uses.

Interactive example

Example on codepen

Using Slider in a Flourish template

The typical way to use Slider in a Flourish template is to create a module-level variable in your template source for each slider you use, and a state property for its value:

var year_slider;
export var state = {
    …
    year: 2000
};

In the draw() function

Create the slider, set its value from the state, and draw it:

slider = Slider("#slider")
    .domain([2000, 2017])
    .startLabel("2000").endLabel("2017")
    .snap(true)
    .value(state.year)
    .on("change", function(year) {
        // If the user drags the slider, update the state and the graphic
        state.year = year;
        update();
    })
    .draw();

In the update() function

Update the slider from the state:

slider.value(state.year).update();

That’s it! If you follow this model, your slider will work correctly in both the visualisation editor and the story editor.

Full reference

Constructor

Slider(selector) creates a slider that will draw into the DOM element specified by selector. This can be an HTML element or an SVG <g>.

Methods

.draw()

Draw the slider.

.update()

Update the slider based on settings you’ve changed. If the slider hasn’t been drawn yet then calling .update() will draw it.

.value()

Retrieves the current position of the slider, in units determined by the domain.

.value(value)

Sets the position of the slider. The graphic will not update till you call .update(), so to move the slider to position 12 you call slider.value(12).update().

.on(event, handler)

Attach an event handler. The only supported event is change. You can attach multiple handlers for the same event, and all of them will fire when appropriate.

Events

change

The change event fires when the user moves the slider by dragging the handle or clicking in the channel. The event handler is passed the new value of the slider as an argument, and its this object is the Slider instance.

Settings

Settings are applied by calling the method of the same name on the Slider object with the new value as an argument. These methods return the Slider object, so you can chain them. For example, you can set the dimensions to 200×50 with

slider.width(200).height(50);

You can retrieve the current value of a setting by calling the method with no arguments, e.g.

var handle_radius = slider.handleRadius();

Core settings

domain

An array of two numbers specifying the range of values the slider can take. Defaults to [0, 1].

snap

If true, snap to the nearest whole number. If a sorted array, snap to the nearest value in the array. Defaults to false, which gives a continuous slider.

width

The width of the slider element. If this is not specified, the slider fills the whole container.

If the slider element is not rendered at the time the slider is drawn, for example if it is styled display: none, then the width and height must be specified

height

The height of the slider element. Should be supplied if and only if width is also supplied.

Style settings

margin

This should be an object with properties top, left, and right. If any of these properties is omitted or null, the library will try to choose a sensible margin based on the other parameters. (It doesn’t always get this right if you’re using start and end labels.)

Defaults to { top: null, left: null, right: null }.

handleRadius

The radius of the circular slider handle. Defaults to 15 if not supplied.

handleFill

The color of the handle. Defaults to black. The handle can also be styled in CSS using the .slider-handle class.

channelHeight

The height of the slider channel. Defaults to 15 if not supplied.

channelRadius

The radius of the corners of the channel. Defaults to null, which means half the channelHeight, giving the channel rounded ends.

channelFill

The color of the channel. Defaults to #eee. The channel also be styled in CSS using the .slider-channel class.

Axis options

axis

If true, show an axis below the slider. If a d3-axis object, use that to define the axis. Defaults to false, which means no axis.

The next few options affect only the axis:

scale

ticks

tickFormat

tickSize

These correspond to the options of the same names in d3-axis. They’re only really useful in conjunction with .axis(true): if you’re constructing your own axis object, you can set these options directly on the axis object before you pass it to Slider.

Label options

label

Label text to draw above the slider. Defaults to null, which means no label.

labelSize

Font size for label. Defaults to 18. The label can also be styled in CSS using the .slider-label class.

startLabel

Text to draw to the left of the slider. Defaults to null. You’ll probably need to adjust the margins to make room for this label.

startLabelBelow

Whether to draw the start label below the slider. Defaults to false, which draws it to the left.

endLabel

Text to draw to the right of the slider. Defaults to null. You’ll probably need to adjust the margins to make room for this label.

endLabelBelow

Whether to draw the end label below the slider. Defaults to false, which draws it to the right.

startEndLabelSize

Font size for start and end labels. Defaults to 16. The start and end labels can also be styled in CSS using the .slider-end-labels class.

Properties

version

Slider.version is the version number of this library, using Semantic Versioning.

版本与发布记录

@flourish/slider 共发布 18 个版本,首次发布于 2017/01/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.0latest2025/05/301.5 年前
3.0.12025/01/101.8 年前
3.0.02024/07/312 年前
2.0.02023/01/183 年前
1.4.12021/05/135 年前
1.4.02018/11/237 年前

关于 @flourish/slider 的常见问题

@flourish/slider 是什么?

Slider UI component

如何安装 @flourish/slider?

在项目目录运行 npm install @flourish/slider,或使用 pnpm add / yarn add / bun add。

@flourish/slider 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/flourish__slider 是否存在。

@flourish/slider 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@flourish/slider 可以商用吗?

@flourish/slider 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@flourish/slider 的下载量有多少?

根据 npm 官方下载统计,@flourish/slider 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,035 次,日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flourish/slider 还在维护吗?

最新版本 3.1.0 发布于 2025/05/30(1.5 年前);至今共发布 18 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/01/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。