Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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
Appearance settings

Check if an element is visible in the viewport using Vanilla JavaScript with Vue 3 support.

License

NotificationsYou must be signed in to change notification settings

opuu/inview

Repository files navigation

InView is a lightweight, high-performance JavaScript library for detecting when elements enter or exit the viewport. Perfect for lazy loading, scroll-triggered animations, infinite scrolling, and more.


🚀 Features

  • Simple API: Intuitive and flexible configuration
  • TypeScript Support: Complete type definitions included
  • Vue.js Integration: Custom directives for seamless usage
  • High Performance: Minimal overhead, efficient observation
  • Customizable: Precision, delay, and single/multi-element support
  • Modern Browser Support: Built on Intersection Observer API

📚 Table of Contents


📦 Installation

Install via your preferred package manager:

npm install @opuu/inview# orpnpm install @opuu/inview# oryarn add @opuu/inview

Or use via CDN:

<scripttype="module">importInViewfrom"https://cdn.jsdelivr.net/npm/@opuu/inview/dist/inview.js";</script>

⚡ Quick Start

Import

For module bundlers (webpack, rollup, parcel, etc.):

importInViewfrom"@opuu/inview";

For browsers with ES module support:

<scripttype="module">importInViewfrom"node_modules/@opuu/inview/dist/inview.js";</script>

Or use the CDN snippet above.


🛠️ API Reference

Constructor

Create a new InView instance by passing a CSS selector or a configuration object:

constinview=newInView(selectorOrConfig);
  • selectorOrConfig:string (CSS selector) orInViewConfig object

InViewConfig Interface

Configure observation with the following options:

interfaceInViewConfig{selector:string;delay?:number;precision?:"low"|"medium"|"high";single?:boolean;}
PropertyTypeDefaultDescription
selectorstringCSS selector for elements to observe
delaynumber0Delay (ms) before triggering callbacks
precision"low" | "medium" | "high""medium"Intersection precision
singlebooleanfalseObserve only the first matching element

Methods

on(event, callback)

Listen for"enter" or"exit" events:

inview.on("enter",(event)=>{// Element entered viewport});inview.on("exit",(event)=>{// Element exited viewport});
  • event:"enter" or"exit"
  • callback:(event: InViewEvent) => void

pause()

Pause observation:

inview.pause();

resume()

Resume observation:

inview.resume();

setDelay(delay)

Set callback delay (in ms):

inview.setDelay(200);

InViewEvent Object

The callback for"enter" and"exit" receives anInViewEvent object:

interfaceInViewEvent{percentage:number;rootBounds:DOMRectReadOnly|null;boundingClientRect:DOMRectReadOnly;intersectionRect:DOMRectReadOnly;target:Element;time:number;event:"enter"|"exit";}
PropertyTypeDescription
percentagenumber% of element visible in viewport
rootBoundsDOMRectReadOnly | nullViewport rectangle
boundingClientRectDOMRectReadOnlyElement's rectangle
intersectionRectDOMRectReadOnlyIntersection rectangle
targetElementObserved element
timenumberEvent timestamp
event"enter" | "exit"Event type

🧑‍💻 Examples

Basic Usage

Observe Elements by Selector

constelements=newInView(".css-selector");elements.on("enter",(event)=>{// Element entered viewport});elements.on("exit",(event)=>{// Element exited viewport});

Advanced Configuration

constelement=newInView({selector:".css-selector",delay:100,// ms delay before callbackprecision:"high",// "low" | "medium" | "high"single:true,// Observe only the first match});

TypeScript Example

importInViewfrom"@opuu/inview";importtype{InViewConfig,InViewEvent}from"@opuu/inview";constconfig:InViewConfig={selector:".css-selector",delay:0,precision:"medium",single:true,};constelement:InView=newInView(config);element.on("enter",(event:InViewEvent)=>{// Handle enter event});

Vue.js Integration

InView provides Vue directives for easy integration.

Register Directives Globally

import{createInViewDirective,createOutViewDirective}from"@opuu/inview/vue";constapp=createApp(App);app.directive("inview",createInViewDirective({delay:100,precision:"high",single:true}));app.directive("outview",createOutViewDirective());

Usage in Templates

<template><divv-inview="onEnterHandler"v-outview="onExitHandler"><!-- Content --></div></template>

You can also register directives locally within a component.


🌐 Browser Support

InView uses theIntersection Observer API, supported by all modern browsers.
See browser compatibility.


📄 License

MIT License


👤 Author

Obaydur Rahman


🔗 References

About

Check if an element is visible in the viewport using Vanilla JavaScript with Vue 3 support.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp