@financial-times/o-viewport

中文文档
5.1.2发布于 2023/10/27MITES Module零依赖近 30 天下载 6329

Utility for attaching debounced listeners to resize, scroll, orientation and visibility events on window

原始使用文档
文档目录18 节
  1. Usage
  2. Methods
  3. o-viewport#listenTo(eventType)
  4. o-viewport#stopListeningTo(eventType)
  5. o-viewport#getOrientation()
  6. o-viewport#getVisibility()
  7. o-viewport#getSize(ignoreScrollbars)
  8. o-viewport#getScrollPosition()
  9. o-viewport#setThrottleInterval(eventType, interval) Product…
  10. o-viewport#debug()
  11. Events
  12. oViewport.resize
  13. oViewport.orientation
  14. oViewport.visibility
  15. oViewport.scroll
  16. Throttling
  17. Migration
  18. Licence

o-viewport MIT licensed

Utility for attaching debounced listeners to resize, scroll, orientation and visibility events on window.

Usage

Check out how to include Origami components in your project to get started with o-viewport.

Note: within the component's API and in the documentation below orientation and visibility are used instead of orientationchange or visibilitychange, but the actual browser event listened to is orientationchange or visibilitychange

Methods

o-viewport#listenTo(eventType)

Attaches a debounced/throttled (as appropriate) listener to events on window [resize, scroll, orientation, visibility or all] which in turn fires events within the oViewport namespace (see Events below).

Note: all will enable all o-viewport events.

import oViewport from '@financial-times/o-viewport';

// Fire for orientation events.
oViewport.listenTo('orientation');

// Listener for debounced orientation events via o-viewport.
document.body.addEventListener('oViewport.orientation', function(event) {
    console.log(event.type); // oViewport.orientation
    console.log(event.viewport); // { height, width }
    console.log(event.orientation); // 'portrait' or 'landscape'
    console.log(event.originalEvent); // the original browser event
});

See events for more examples.

o-viewport#stopListeningTo(eventType)

Remove the attached listener from the window for the named event [resize, scroll, orientation, visibility or all].

Note: all will disable all o-viewport events.

// Stop listening to the orientation event.
oViewport.stopListeningTo('orientation');

o-viewport#getOrientation()

Provides a reasonably reliable way (more so than window.orientation) of obtaining the current orientation of the viewport.

oViewport.getOrientation(); // 'portrait' or 'landscape'

o-viewport#getVisibility()

Provides a reasonably reliable way of obtaining the current visibility of the viewport.

oViewport.getVisibility(); // boolean, true if visible

o-viewport#getSize(ignoreScrollbars)

Provides a reliable way of obtaining the current dimensions of the browser window. Returns an object with the properties width and height.

By default or if no parameters are passed the method will return the size of the viewport inclusive of the scrollbars. However in certain cases (e.g. adverts) you may want to get the size of the viewport without the scroll bars. In such case pass true to the method in order to ignore the scrollbars.

oViewport.getSize(); // {width: 108, height: 100} including scrollbar width
oViewport.getSize(true); // {width: 100, height: 100} without scrollbars

o-viewport#getScrollPosition()

Provides a reliable way of obtaining the current scroll position of the viewport. returns an object with the properties width, height, left and top

oViewport.getScrollPosition(); // {width: 100, height: 100, left: 0, top: 10}

o-viewport#setThrottleInterval(eventType, interval) Product use only

Sets the debounce/throttle interval for a given event [scroll, resize or orientation]. As a shorthand, calling setThrottleInterval with 1 - 3 numbers will set the intervals for scroll, resize and orientation in that order e.g. setThrottleInterval(100, undefined, 300) is equivalent to:

setThrottleInterval('scroll', 100)
setThrottleInterval('resize') // which does nothing
setThrottleInterval('orientation', 300)
setThrottleInterval('visibility', 30)

The default value for each of these is 100ms

o-viewport#debug()

Turns on debug mode (logging event details to the console).

Events

Each of these custom events are fired on document.body. For each custom event event.detail.originalEvent contains a reference to the original browser event and event.detail.viewport the result of o-viewport#getSize(). For example:

import oViewport from '@financial-times/o-viewport';

// Fire for all viewport events.
oViewport.listenTo('all');

// Listener for debounced visibility events via o-viewport.
document.body.addEventListener('oViewport.visibility', function(event) {
    console.log(event.type); // oViewport.resize
    console.log(event.detail.viewport); // { height, width }
    console.log(event.detail.hidden); // boolean
});

Note event.detail.hidden is unique to the oViewport.visibility event. Additional unique properties for o-viewport events are detailed below.

oViewport.resize

  • No additional properties.

oViewport.orientation

  • data.orientation: 'portrait' or 'landscape'

oViewport.visibility

  • data.hidden: true or false

oViewport.scroll

  • data.scrollLeft: unitless px value of scroll position
  • data.scrollTop: unitless px value of scroll position
  • data.scrollHeight: unitless px value of scroll height
  • data.scrollWidth: unitless px value of scroll width

Throttling

  • oViewport.resize, oViewport.orientation and oViewport.visibility are debounced i.e. if the native event fires several times in quick succession the custom event will fire only once n milliseconds after the last event, where n is the throttle interval
  • oViewport.scroll is throttled i.e. if the native scroll event fires several times in quick succession the custom event will fire at most once every n milliseconds, where n is the throttle interval

Use the setThrottleInterval method to customise throttling.

Migration

StateMajor VersionLast Minor ReleaseMigration guide
✨ active5N/Amigrate to v5
⚠ maintained44.0.5migrate to v4
╳ deprecated33.3migrate to v3
╳ deprecated22.3migrate to v2
╳ deprecated11.5N/A

Licence

Copyright (c) 2016 Financial Times Ltd. All rights reserved.

This software is published under the MIT licence.

版本与发布记录

@financial-times/o-viewport 共发布 19 个版本,首次发布于 2019/02/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.1.2latest2023/10/273 年前
5.1.12022/01/134 年前
5.1.02021/11/254 年前
5.0.12021/09/215 年前
5.0.02021/06/225 年前
5.0.0-beta.0prerelease2021/04/295 年前

关于 @financial-times/o-viewport 的常见问题

@financial-times/o-viewport 是什么?

Utility for attaching debounced listeners to resize, scroll, orientation and visibility events on window

如何安装 @financial-times/o-viewport?

在项目目录运行 npm install @financial-times/o-viewport,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@financial-times/o-utils(^2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@financial-times/o-viewport 支持 TypeScript 吗?

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

@financial-times/o-viewport 支持 ESM 还是 CommonJS?

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

@financial-times/o-viewport 可以商用吗?

@financial-times/o-viewport 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@financial-times/o-viewport 的下载量有多少?

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

@financial-times/o-viewport 还在维护吗?

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