inspired by body-scroll-lock
English | 简体中文
tua-body-scroll-lock
解决了所有场景下滚动穿透的问题。
targetElement
时失效targetElement
(即使并不需要)😱BSL 居然不能用?不妨自己试试这个用了 BSL 的 demo
$ npm i -S tua-body-scroll-lock
# OR
$ yarn add tua-body-scroll-lock
UMD(tua-bsl.umd.js
)
x<!-- unpkg -->
<script src="https://unpkg.com/tua-body-scroll-lock"></script>
<!-- jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/tua-body-scroll-lock"></script>
Minified UMD(tua-bsl.umd.min.js
)
xxxxxxxxxx
<!-- unpkg -->
<script src="https://unpkg.com/tua-body-scroll-lock/dist/tua-bsl.umd.min.js"></script>
<!-- jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/tua-body-scroll-lock/dist/tua-bsl.umd.min.js"></script>
ESM in browser(tua-bsl.esm.browser.js
)
xxxxxxxxxx
<!-- 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>
Minified ESM in browser(tua-bsl.esm.browser.min.js
)
xxxxxxxxxx
<!-- 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>
xxxxxxxxxx
import { lock, unlock } from 'tua-body-scroll-lock'
lock()
unlock()
在某些场景下,禁止滚动穿透时,仍然有些元素需要滚动行为,此时传入目标 DOM 元素即可。
xxxxxxxxxx
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
,这样很不友好。
xxxxxxxxxx
<template>
// some element
</template>
<script>
import { lock, unlock, clearBodyLocks } from 'tua-body-scroll-lock';
export default {
name: 'demo',
data () {
return {}
},
methods: {
showDialog () {
// 禁止滑动
lock()
},
hideDialog () {
// 开始滑动
unlock()
}
},
beforeDestroy () {
// 在跳转其他路由下的页面前忘记调用`unlock`,`clearBodyLocks`可以清除所有的副作用。
clearBodyLocks()
}
}
</script>
平台 | 链接 | | - | - gh-pages | https://tuateam.github.io/tua-body-scroll-lock | jsbin | https://jsbin.com/cafiful/edit?output | codepen | https://codepen.io/buptsteve/pen/PvNQjP | jsfiddle | https://jsfiddle.net/buptsteve/6u8g3Lf5/ | codesandbox | https://codesandbox.io/s/o73z4jy5q9 |
Thanks goes to these wonderful people (emoji key):
evinma 💻 📖 🚇 🌍 |
StEve Young 💻 📖 🚇 🌍 |
li2go 💻 🐛 |
songyan,Wang 💻 🐛 |
Даниил Пронин 🐛 |
阿卡琳 🐛 |
falstack 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!