Enables body scroll locking (for iOS Mobile and Tablet, Android, desktop Safari/Chrome/Firefox) without breaking scrolling of a target element (eg. modal/lightbox/flyouts/nav-menus).
Features:
-webkit-overflow-scrolling: touch
still worksAren't the alternative approaches sufficient?
document.body.ontouchmove = (e) => { e.preventDefault(); return false; };
locks thebody scroll, but ALSO locks the scroll of a target element (eg. modal).overflow: hidden
on the body or html elements doesn't work for all browsersposition: fixed
approach causes the body scroll to resetLIGHT Package Size:
$ yarn add body-scroll-lock
or
$ npm install body-scroll-lock
You can also load via a <script src="lib/bodyScrollLock.js"></script>
tag (refer to the lib folder).
// 1. Import the functions
const bodyScrollLock = require('body-scroll-lock');
const disableBodyScroll = bodyScrollLock.disableBodyScroll;
const enableBodyScroll = bodyScrollLock.enableBodyScroll;
// 2. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
// Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
// This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
const targetElement = document.querySelector('#someElementId');
// 3. ...in some event handler after showing the target element...disable body scroll
disableBodyScroll(targetElement);
// 4. ...in some event handler after hiding the target element...
enableBodyScroll(targetElement);
// 1. Import the functions
import { disableBodyScroll, enableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock';
class SomeComponent extends React.Component {
targetElement = null;
componentDidMount() {
// 2. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
// Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
// This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
this.targetElement = document.querySelector('#targetElementId');
}
showTargetElement = () => {
// ... some logic to show target element
// 3. Disable body scroll
disableBodyScroll(this.targetElement);
};
hideTargetElement = () => {
// ... some logic to hide target element
// 4. Re-enable body scroll
enableBodyScroll(this.targetElement);
};
componentWillUnmount() {
// 5. Useful if we have called disableBodyScroll for multiple target elements,
// and we just want a kill-switch to undo all that.
// OR useful for if the `hideTargetElement()` function got circumvented eg. visitor
// clicks a link which takes him/her to a different page within the app.
clearAllBodyScrollLocks();
}
render() {
return <div>some JSX to go here</div>;
}
}
// 1. Import the functions
import { disableBodyScroll, enableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock';
class SomeComponent extends React.Component {
// 2. Initialise your ref and targetElement here
targetRef = React.createRef();
targetElement = null;
componentDidMount() {
// 3. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
// Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
// This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
this.targetElement = this.targetRef.current;
}
showTargetElement = () => {
// ... some logic to show target element
// 4. Disable body scroll
disableBodyScroll(this.targetElement);
};
hideTargetElement = () => {
// ... some logic to hide target element
// 5. Re-enable body scroll
enableBodyScroll(this.targetElement);
};
componentWillUnmount() {
// 5. Useful if we have called disableBodyScroll for multiple target elements,
// and we just want a kill-switch to undo all that.
// OR useful for if the `hideTargetElement()` function got circumvented eg. visitor
// clicks a link which takes him/her to a different page within the app.
clearAllBodyScrollLocks();
}
render() {
return (
// 6. Pass your ref with the reference to the targetElement to SomeOtherComponent
<SomeOtherComponent ref={this.targetRef}>some JSX to go here</SomeOtherComponent>
);
}
}
// 7. SomeOtherComponent needs to be a Class component to receive the ref (unless Hooks - https://reactjs.org/docs/hooks-faq.html#can-i-make-a-ref-to-a-function-component - are used).
class SomeOtherComponent extends React.Component {
componentDidMount() {
// Your logic on mount goes here
}
// 8. BSL will be applied to div below in SomeOtherComponent and persist scrolling for the container
render() {
return <div>some JSX to go here</div>;
}
}
import { Component, ElementRef, OnDestroy, ViewChild } from "@angular/core";
// 1. Import the functions
import {
disableBodyScroll,
enableBodyScroll,
clearAllBodyScrollLocks
} from "body-scroll-lock";
@Component({
selector: "app-scroll-block",
templateUrl: "./scroll-block.component.html",
styleUrls: ["./scroll-block.component.css"]
})
export class SomeComponent implements OnDestroy {
// 2. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
// Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
// This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
@ViewChild("scrollTarget") scrollTarget: ElementRef;
showTargetElement() {
// ... some logic to show target element
// 3. Disable body scroll
disableBodyScroll(this.scrollTarget.nativeElement);
}
hideTargetElement() {
// ... some logic to hide target element
// 4. Re-enable body scroll
enableBodyScroll(this.scrollTarget.nativeElement);
}
ngOnDestroy() {
// 5. Useful if we have called disableBodyScroll for multiple target elements,
// and we just want a kill-switch to undo all that.
// OR useful for if the `hideTargetElement()` function got circumvented eg. visitor
// clicks a link which takes him/her to a different page within the app.
clearAllBodyScrollLocks();
}
}
In the html:
<head>
<script src="some-path-where-you-dump-the-javascript-libraries/lib/bodyScrollLock.js"></script>
</head>
Then in the javascript:
// 1. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
// Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
// This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
const targetElement = document.querySelector('#someElementId');
// 2. ...in some event handler after showing the target element...disable body scroll
bodyScrollLock.disableBodyScroll(targetElement);
// 3. ...in some event handler after hiding the target element...
bodyScrollLock.enableBodyScroll(targetElement);
// 4. Useful if we have called disableBodyScroll for multiple target elements,
// and we just want a kill-switch to undo all that.
bodyScrollLock.clearAllBodyScrollLocks();
Check out the demo, powered by Vercel.
Function | Arguments | Return | Description |
---|---|---|---|
disableBodyScroll |
targetElement: HTMLElement options: BodyScrollOptions |
void |
Disables body scroll while enabling scroll on target element |
enableBodyScroll |
targetElement: HTMLElement |
void |
Enables body scroll and removing listeners on target element |
clearAllBodyScrollLocks |
null |
void |
Clears all scroll locks |
optional, default: false
If the overflow property of the body is set to hidden, the body widens by the width of the scrollbar. This produces anunpleasant flickering effect, especially on websites with centered content. If the reserveScrollBarGap
option is set,this gap is filled by a padding-right
on the body element. If disableBodyScroll
is called for the last target element,or clearAllBodyScrollLocks
is called, the padding-right
is automatically reset to the previous value.
import { disableBodyScroll } from 'body-scroll-lock';
import type { BodyScrollOptions } from 'body-scroll-lock';
const options: BodyScrollOptions = {
reserveScrollBarGap: true,
};
disableBodyScroll(targetElement, options);
optional, default: undefined
To disable scrolling on iOS, disableBodyScroll
prevents touchmove
events.However, there are cases where you have called disableBodyScroll
on anelement, but its children still require touchmove
events to function.
See below for 2 use cases:
disableBodyScroll(container, {
allowTouchMove: el => el.tagName === 'TEXTAREA',
});
Javascript:
disableBodyScroll(container, {
allowTouchMove: el => {
while (el && el !== document.body) {
if (el.getAttribute('body-scroll-lock-ignore') !== null) {
return true;
}
el = el.parentElement;
}
},
});
Html:
<div id="container">
<div id="scrolling-map" body-scroll-lock-ignore>
...
</div>
</div>
https://medium.com/jsdownunder/locking-body-scroll-for-all-devices-22def9615177https://stackoverflow.com/questions/41594997/ios-10-safari-prevent-scrolling-behind-a-fixed-overlay-and-maintain-scroll-posi
Refer to the releases page.
前言 不久前,遇到这样的一个APP需求,头部一个tab,下面是一个列表,要实现上拉加载更多,下拉刷新。在度娘里找到了better-scroll,参照better-scroll的demo,实现了一个scroll组件 scroll组件 html <div class="mux-scroll-wrapper" :style="scrollStyle"> <div class="mux-scroll-c
他们停止了维修项目问题。我用新版本的 typeScript 以同样的API完成,修复这些问题供大家使用。并添加react hooks、vue3示例,0迁移工作量 npm uninstall body-scroll-lock @types/body-scroll-lock npm i body-scroll-lock-upgrade 修复日志,参考发布页面。
他们停止了维修。我必须自己用新版本的 typeScript 以同样的方式完成。并修复这些问题供大家使用。添加react hooks、vue3示例 npm uninstall body-scroll-lock @types/body-scroll-lock npm i body-scroll-lock-upgrade 修复日志,参考发布页面。
1. 为什么要添加append-to-body属性 在写弹框嵌套时我们需要添加append-to-body属性,否则后弹出的框就会有遮盖层遮挡,如下图 2.为什么会修改无效 我们想改变某个弹窗的原来样式,通常是在该弹框外包一个div,然后修改该div下的dialog样式,但是添加append-to-body属性为true后,Dialog 将插入 body 元素上,也就是说它并不在该div元素之下了
正在为您搜索:vue项目、element UI框架、el-dialog组件、:append-to-body属性 在 Vue 项目中使用 Element UI 框架,el-dialog 组件中的 :append-to-body="true" 属性仍然表示将对话框插入到文档的 <body> 元素中。其作用与之前回答中描述的一致。 但是,如果不使用该属性,而是将其设置为默认值 false,则会将 el-
tua-body-scroll-lock inspired by body-scroll-lock English | 简体中文 Introduction tua-body-scroll-lock enables body scroll locking for everything. Why not body-scroll-lock(BSL)? Doesn't work on Android we
<ion-scroll scrollX="true"> </ion-scroll> <ion-scroll scrollY="true"> </ion-scroll> <ion-scroll scrollX="true" scrollY="true"> </ion-scroll> 属性 属性名称 类型 描述 scrollX boolean 是否启用x轴滚动 scrollY boolean 是否启用
滚动列表,提供了优质的原生滚动体验,便捷的配置项和事件,是一个基于better-scroll进行封装的组件。 滚动原理 由于 better-scroll 的滚动原理为:在滚动方向上,第一个子元素的长度超过了容器的长度。 那么对于 Scroll 组件,其实就是内容元素.cube-scroll-content在滚动方向上的长度必须大于容器元素 .cube-scroll-wrapper。根据滚动方向的不
Google Body是去年Google实验室的工程师利用“20%自由时间”开发的一款人类身体结构浏览器,可以帮助学习美术、解剖和其他医学科目的学生更好的了解 人体结构,同时也可以作为医生和科研工作者的专业研究工具。1月9日,Google宣布将Google Body Viewer开源。在去年7月,Google宣布关闭实验室项目,Google Body也随之过继给Zygote Media,现在的名字
无限滚动指令。 引入 import { InfiniteScroll } from 'mint-ui'; Vue.use(InfiniteScroll); 例子 为 HTML 元素添加 v-infinite-scroll 指令即可使用无限滚动。滚动该元素,当其底部与被滚动元素底部的距离小于给定的阈值(通过 infinite-scroll-distance 设置)时,绑定到 v-infinite
描述 (Description) 您可以通过添加.scroll类来水平滚动表。 例子 (Example) 以下示例演示了在Foundation中使用scroll table - <!doctype html> <head> <meta charset = "utf-8" /> <meta http-equiv = "x-ua-compatible" content =