简体中文 | English
Monitor changes to nodes on a specified DOM tree or changes to the size of element box models and allow processing of changes.
Install dependencies
npm i @cailiao/watch-dom
the patch function
import patch from '@cailiao/watch-dom'
// Choosing to execute the patch function at the right time will create two prototype methods $watch and $watchBox on the global class Element.
patch()
// Or import only the specified methods as needed, watch is equivalent to the $watch method on the prototype, and watchBox is equivalent to the $watchBox method on the prototype.
import { watch, watchBox } from '@cailiao/watch-dom'
- patch function
// Monitor changes to child nodes on the DOM tree. targetElement is an instance of any built-in Element class, i.e., a DOM element.
const unwatch = targetElement.$watch(record => { console.log(record) }, { subtree: true, childList: true })
// Cancel monitoring
unwatch()
// Monitor changes to the size of element box models. targetElement is an instance of any built-in Element class, i.e., a DOM element.
const unwatch = targetElement.$watchBox(record => { console.log(record) }, { subtree: true, childList: true })
// Cancel monitoring
unwatch()
// Pause monitoring
unwatch.pause()
// Resume monitoring
unwatch.resume()
- import only needed
// Monitor changes to child nodes on the DOM tree. targetElement is an instance of any built-in Element class, i.e., a DOM element.
const unwatch = watch(targetElement, record => { console.log(record) }, { subtree: true, childList: true })
// Cancel monitoring
unwatch()
// Monitor changes to the size of element box models. targetElement is an instance of any built-in Element class, i.e., a DOM element.
const unwatch = watchBox(targetElement, record => { console.log(record) }, { subtree: true, childList: true })
// Cancel monitoring
unwatch()
// Pause monitoring
unwatch.pause()
// Resume monitoring
unwatch.resume()
The patch()
method inserts two methods, $watch
and $watchBox
, on the prototype of Element
. The directly imported watch
is equivalent to the $watch
method on the prototype, and watchBox
is equivalent to the $watchBox
method on the prototype, with only slight differences in the parameters passed in.
-
callback
: Required.Function
type. Pass in one parameter,-
record
is an array of MutationRecord. It contains information about the modification of the DOM. -
observer
: A reference to ResizeObserver itself.
-
-
options
: Required.Object
type. It is the options parameter of MutationObserver.observe(),Options are as follows:
-
subtree
OptionalSet to
true
to extend monitoring to the entire subtree of nodes rooted attarget
. All of the other properties are then extended to all of the nodes in the subtree instead of applying solely to thetarget
node. The default value isfalse
. -
childList
OptionalSet to
true
to monitor the target node (and, ifsubtree
istrue
, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value isfalse
. -
attributes
OptionalSet to
true
to watch for changes to the value of attributes on the node or nodes being monitored. The default value istrue
if either ofattributeFilter
orattributeOldValue
is specified, otherwise the default value isfalse
. -
attributeFilter
OptionalAn array of specific attribute names to be monitored. If this property isn't included, changes to all attributes cause mutation notifications.
-
attributeOldValue
OptionalSet to
true
to record the previous value of any attribute that changes when monitoring the node or nodes for attribute changes; See Monitoring attribute values for details on watching for attribute changes and value recording. The default value isfalse
. -
characterData
OptionalSet to
true
to monitor the specified target node (and, ifsubtree
istrue
, its descendants) for changes to the character data contained within the node or nodes. The default value istrue
ifcharacterDataOldValue
is specified, otherwise the default value isfalse
. -
characterDataOldValue
OptionalSet to
true
to record the previous value of a node's text whenever the text changes on nodes being monitored. The default value isfalse
.
-
Returns a function to cancel the monitor.
-
callback
: Required.Function
type. Pass in two parameters,-
record is an array of ResizeObserverEntry. It contains information about the modified size of elements.
-
observer: A reference to ResizeObserver itself.
-
-
options
: Optional.Object
type. It is the options parameter of ResizeObserver.observe(),The properties of options are as follows:-
box
Sets which box model the observer will observe changes to. Possible values are:-
content-box
(the default)Size of the content area as defined in CSS.
-
border-box
Size of the box border area as defined in CSS.
-
device-pixel-content-box
The size of the content area as defined in CSS, in device pixels, before applying any CSS transforms on the element or its ancestors.
-
-
Returns a function to cancel the monitor.
-
pause:
Function
type, causes the current monitor to pause monitoring. -
resume:
Function
type, resumes monitoring to the current monitor. Should be called only afterpause()
has been called.
-
target
:Required.HTMLAllCollection
type. -
callback
: Required.Function
type. Pass in one parameter,-
record is an array of MutationRecord. It contains information about the modification of the DOM.
-
observer: A reference to ResizeObserver itself.
-
-
options
: Required.Object
type. It is the options parameter of MutationObserver.observe(),-
Options are as follows:
-
subtree
OptionalSet to
true
to extend monitoring to the entire subtree of nodes rooted attarget
. All of the other properties are then extended to all of the nodes in the subtree instead of applying solely to thetarget
node. The default value isfalse
. -
childList
OptionalSet to
true
to monitor the target node (and, ifsubtree
istrue
, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value isfalse
. -
attributes
OptionalSet to
true
to watch for changes to the value of attributes on the node or nodes being monitored. The default value istrue
if either ofattributeFilter
orattributeOldValue
is specified, otherwise the default value isfalse
. -
attributeFilter
OptionalAn array of specific attribute names to be monitored. If this property isn't included, changes to all attributes cause mutation notifications.
-
attributeOldValue
OptionalSet to
true
to record the previous value of any attribute that changes when monitoring the node or nodes for attribute changes; See Monitoring attribute values for details on watching for attribute changes and value recording. The default value isfalse
. -
characterData
OptionalSet to
true
to monitor the specified target node (and, ifsubtree
istrue
, its descendants) for changes to the character data contained within the node or nodes. The default value istrue
ifcharacterDataOldValue
is specified, otherwise the default value isfalse
. -
characterDataOldValue
OptionalSet to
true
to record the previous value of a node's text whenever the text changes on nodes being monitored. The default value isfalse
.
-
Returns a function to cancel the monitor.
-
target
:Required.HTMLAllCollection
type. -
callback
: Required.Function
type. Pass in two parameters,-
record is an array of ResizeObserverEntry. It contains information about the modified size of elements.
-
observer: A reference to ResizeObserver itself.
-
-
options
: Optional.Object
type. It is the options parameter of ResizeObserver.observe(),The properties of options are as follows:-
box
Sets which box model the observer will observe changes to. Possible values are:-
content-box
(the default)Size of the content area as defined in CSS.
-
border-box
Size of the box border area as defined in CSS.
-
device-pixel-content-box
The size of the content area as defined in CSS, in device pixels, before applying any CSS transforms on the element or its ancestors.
-
-
Returns a function to cancel the monitor.
-
pause:
Function
type, causes the current monitor to pause monitoring. -
resume:
Function
type, resumes monitoring to the current monitor. Should be called only afterpause()
has been called.