English | 简体中文
tua-body-scroll-lock
解决了所有场景下滚动穿透的问题。
pnpm i tua-body-scroll-lock
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
的元素。
import { lock } from 'tua-body-scroll-lock'
lock(targetElement, { overflowType: 'clip' })
可选,默认值: false
是否为页面上所有 BSL 实例共享 lockState
的状态。在你的页面上有多个 BSL 实例时很有用。
在某些场景下,禁止滚动穿透时,仍然有些元素需要滚动行为,此时传入目标 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。
在单页应用中,如果调用过lock
,但是在跳转其他路由下的页面前忘记调用unlock
,这是很糟糕的。因为对页面的操作都没有恢复,比如ios中禁止了touchmove
;clearBodyLocks
就是用来清除所有的副作用。当前你也可以调用unlock
,但是如果之前调用过多次lock
,那么就必须要调用多次unlock
,这样很不友好。
请查看这些示例项目
Thanks goes to these wonderful people (emoji key):
evinma 💻 📖 🚇 🌍 |
StEve Young 💻 📖 🚇 🌍 |
li2go 💻 🐛 |
songyan,Wang 💻 🐛 |
Даниил Пронин 🐛 |
阿卡琳 🐛 |
falstack 💻 |
David Q. Jin 🐛 |
This project follows the all-contributors specification. Contributions of any kind welcome!
inspired by body-scroll-lock