Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/spectPublic

Observable selectors in DOM

License

NotificationsYou must be signed in to change notification settings

dy/spect

Repository files navigation

spect spect   npm bundle sizenpm

Observe selectors in DOM.

spect( container=document, selector, handler? )

Observesselector incontainer, invokeshandler any time matching elements appear.
Handler can return a teardown function, called for unmatched elements.
Returns live collection of elements.

importspectfrom'spect';// assign aspectconstfoos=spect('.foo',el=>{console.log('connected');return()=>console.log('disconnected');});// modify DOMconstfoo=document.createElement('div');foo.className='foo';document.body.append(foo);// ... "connected"foo.remove();// ... "disconnected"

spect(element[s], handler)

Listens for connected/disconnected events for the list of elements. (alternative tofast-on-load)

constnodes=[...document.querySelectorAll('.foo'),document.createElement('div')];// assign listenerspect(nodes,el=>{console.log("connected");return()=>console.log("disconnected");});document.body.appendChild(nodes.at(-1))// ... "connected"nodes.at(-1).remove()// ... "disconnected"

Live Collection

Spect creates live collection of elements matching the selector. Collection extends Array and implements Set / HTMLColection interfaces.

constfoos=spect(`.foo`);// live collectionfoos[idx],foos.at(idx)// Arrayfoos.has(el),foos.add(el),foos.delete(el)// Setfoos.item(idx),foos.namedItem(elementId)// HTMLCollectionfoos.dispose()// destroy selector observer / unsubscribe

Technique

It combines selector parts indexing fromselector-observer for simple queries and animation events frominsertionQuery for complex selectors.

Simple selector is id/name/class/tag followed by classes or attrs.

  • #a,.x.y,[name="e"].x,*,a-b-c:x - simple selectors.
  • a b,#b .c - complex selectors.

Alternatives

element-behaviors,insertionQuery,selector-observer,qso,qsa-observer,element-observer,livequery,selector-listener,mutation-summary,fast-on-load,selector-set,rkusa/selector-observer.css-chain


[8]ページ先頭

©2009-2025 Movatter.jp