Skip to content

Latest commit

 

History

History
214 lines (156 loc) · 8.71 KB

README-zh_CN.md

File metadata and controls

214 lines (156 loc) · 8.71 KB

tua-body-scroll-lock

dependencies

Downloads per month Version Next Version npm bundle size License

English | 简体中文

介绍

tua-body-scroll-lock 解决了所有场景下滚动穿透的问题。

安装

Node Package Manager(recommended)

pnpm i tua-body-scroll-lock

CDN

UMD(`tua-bsl.umd.js`)
<!-- unpkg -->
<script src="https://unpkg.com/tua-body-scroll-lock/dist/tua-bsl.umd.js"></script>

<!-- jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/tua-body-scroll-lock/dist/tua-bsl.umd.js"></script>
压缩版本的 UMD(`tua-bsl.umd.min.js`)
<!-- unpkg -->
<script src="https://unpkg.com/tua-body-scroll-lock"></script>

<!-- jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/tua-body-scroll-lock"></script>
ESM in browser(`tua-bsl.esm.browser.js`)
<!-- unpkg -->
<script type="module">
  import { lock, unlock } from 'https://unpkg.com/tua-body-scroll-lock/dist/tua-bsl.esm.browser.js'

  lock()
  unlock()
</script>

<!-- jsdelivr -->
<script type="module">
  import { lock, unlock } from 'https://cdn.jsdelivr.net/npm/tua-body-scroll-lock/dist/tua-bsl.esm.browser.js'

  lock()
  unlock()
</script>
压缩版本的 ESM in browser(`tua-bsl.esm.browser.min.js`)
<!-- unpkg -->
<script type="module">
  import { lock, unlock } from 'https://unpkg.com/tua-body-scroll-lock/dist/tua-bsl.esm.browser.min.js'

  lock()
  unlock()
</script>

<!-- jsdelivr -->
<script type="module">
  import { lock, unlock } from 'https://cdn.jsdelivr.net/npm/tua-body-scroll-lock/dist/tua-bsl.esm.browser.min.js'

  lock()
  unlock()
</script>

使用

常规操作

import { lock, unlock } from 'tua-body-scroll-lock'

lock()
unlock()

选项

overflowType: 'hidden' | 'clip'

可选,默认值: 'hidden'

clip 适合在高版本浏览器中(Chrome 90+)适配 position: sticky 的元素。

https://caniuse.com/mdn-css_types_overflow_clip

import { lock } from 'tua-body-scroll-lock'

lock(targetElement, { overflowType: 'clip' })

useGlobalLockState: boolean

可选,默认值: false

是否为页面上所有 BSL 实例共享 lockState 的状态。在你的页面上有多个 BSL 实例时很有用。

目标元素需要滚动(iOS only)

在某些场景下,禁止滚动穿透时,仍然有些元素需要滚动行为,此时传入目标 DOM 元素即可。

import { lock, unlock } from 'tua-body-scroll-lock'
const elementOne = document.querySelector('#elementOne')
const elementTwo = document.querySelector('#elementTwo')

// 一个目标元素
const targetElement = elementOne
// 多个目标元素
const targetElements = [elementOne, elementTwo]

lock(targetElement)
lock(targetElements)
unlock(targetElement)
unlock(targetElements)

PC 端和安卓端不需要传 targetElement。

clearBodyLocks

在单页应用中,如果调用过lock,但是在跳转其他路由下的页面前忘记调用unlock,这是很糟糕的。因为对页面的操作都没有恢复,比如ios中禁止了touchmoveclearBodyLocks就是用来清除所有的副作用。当前你也可以调用unlock,但是如果之前调用过多次lock,那么就必须要调用多次unlock,这样很不友好。

示例

bodyScrollLock

请查看这些示例项目

Contributors

Thanks goes to these wonderful people (emoji key):

evinma
evinma

💻 📖 🚇 🌍
StEve Young
StEve Young

💻 📖 🚇 🌍
li2go
li2go

💻 🐛
songyan,Wang
songyan,Wang

💻 🐛
Даниил Пронин
Даниил Пронин

🐛
阿卡琳
阿卡琳

🐛
falstack
falstack

💻
David Q. Jin
David Q. Jin

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

inspired by body-scroll-lock