Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
compute-scroll-into-view
Advanced tools
The compute-scroll-into-view npm package is a utility that calculates the scroll position necessary to make an element visible within its container. This is useful for smoothly scrolling items into view, especially in custom scrolling interfaces where the default browser behavior does not suffice.
Compute scroll position
This feature calculates the necessary scroll positions to bring an element into view within its container. The function returns an array of actions that can be applied to achieve the desired scroll effect. Each action specifies the element to scroll and the new top and left scroll positions.
import computeScrollIntoView from 'compute-scroll-into-view';
const element = document.getElementById('targetElement');
const actions = computeScrollIntoView(element, {
scrollMode: 'if-needed',
block: 'nearest',
inline: 'nearest'
});
actions.forEach(({ el, top, left }) => {
el.scrollTop = top;
el.scrollLeft = left;
});
This package also calculates and performs scrolling to bring an element into view if it's not already visible. It is similar to compute-scroll-into-view but also provides the functionality to automatically perform the scroll, whereas compute-scroll-into-view only computes the necessary scroll positions.
An extension of scroll-into-view-if-needed, this package adds smooth scrolling effects. It compares to compute-scroll-into-view by providing both the computation and the smooth scrolling execution, offering a more integrated solution for developers looking for out-of-the-box smooth scrolling.
Lower level API that is used by the ponyfill scroll-into-view-if-needed to compute where (if needed) elements should scroll based on options defined in the spec. Use this if you want the smallest possible bundlesize and is ok with implementing the actual scrolling yourself.
yarn add compute-scroll-into-view
The UMD build is also available on unpkg:
<script src="https://unpkg.com/compute-scroll-into-view/umd/compute-scroll-into-view.min.js"></script>
You can find the library on window.computeScrollIntoView
.
// es6 import
import computeScrollIntoView from 'compute-scroll-into-view'
// or es5
const computeScrollIntoView = require('compute-scroll-into-view')
const node = document.getElementById('hero')
// same behavior as Element.scrollIntoView({block: "nearest", inline: "nearest"})
// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
const actions = computeScrollIntoView(node, {
scrollMode: 'if-needed',
block: 'nearest',
inline: 'nearest',
})
// same behavior as Element.scrollIntoViewIfNeeded(true)
// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded
const actions = computeScrollIntoView(node, { scrollMode: 'if-needed' block: 'center', inline: 'center' })
// Then perform the scrolling, use scroll-into-view-if-needed if you don't want to implement this part
actions.forEach(({ el, top, left }) => {
el.scrollTop = top
el.scrollLeft = left
})
FAQs
The engine that powers scroll-into-view-if-needed
The npm package compute-scroll-into-view receives a total of 2,966,738 weekly downloads. As such, compute-scroll-into-view popularity was classified as popular.
We found that compute-scroll-into-view demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.