Small utility to determine and normalize the Element.scrollLeft property's behavior
Installations
npm install normalize-scroll-left
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
12.11.0
NPM Version
6.13.3
Score
99.7
Supply Chain
98.9
Quality
75.4
Maintenance
100
Vulnerability
100
License
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
alitaheri
Download Statistics
Total Downloads
109,073,250
Last Day
12,652
Last Week
159,240
Last Month
888,019
Last Year
9,448,389
GitHub Statistics
19 Stars
15 Commits
4 Forks
2 Watching
8 Branches
4 Contributors
Bundle Size
1.40 kB
Minified
608.00 B
Minified + Gzipped
Package Meta Information
Latest Version
0.2.1
Package Id
normalize-scroll-left@0.2.1
Size
3.73 kB
NPM Version
6.13.3
Node Version
12.11.0
Publised On
07 Sept 2020
Total Downloads
Cumulative downloads
Total Downloads
109,073,250
Last day
-73.8%
12,652
Compared to previous day
Last week
-31.6%
159,240
Compared to previous week
Last month
6.1%
888,019
Compared to previous month
Last year
-9.7%
9,448,389
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Normalize Scroll Left for Right-to-Left
This library normalizes the Element.scrollLeft
property when direction is rtl
.
All the hardwork are based on this juqery plugin and this stackoverflow answer.
Since Element.scrollLeft
's behavior with dir="rtl"
is not defined in any spec we use
a feature detection logic to determine the behavior of the current browser.
Types of scrollLeft
(scrollWidth
= 100) (Copied from
here)
Browser | Type | Most Left | Most Right | Initial |
---|---|---|---|---|
WebKit | default | 0 | 100 | 100 |
Firefox/Opera | negative | -100 | 0 | 0 |
IE/Edge | reverse | 100 | 0 | 0 |
Installation
You can install this package with the following command:
1npm install normalize-scroll-left
API
This library exposes these methods:
detectScrollType
1type ScrollType = 'indeterminate' | 'default' | 'negative' | 'reverse'; 2function detectScrollType(): ScrollType;
This function returns the scroll type detected, Keep in mind, this function caches the result as it should render a dummy on the DOM (which is expensive). Make sure the first invocation of this function happens after the body is loaded.
note: To support server-side-rendering, it will output indeterminate
if
it detects a non-browser environment.
1import { detectScrollType } from 'normalize-scroll-left'; 2 3const type = detectScrollType();
The output is not based on the browser, but feature detection:
Browser | Type |
---|---|
WebKit | default |
Firefox/Opera | negative |
IE/Edge | reverse |
Other/Server | indeterminate |
getNormalizedScrollLeft
1function getNormalizedScrollLeft(element: HTMLElement, direction: 'rtl' | 'ltr'): number;
You can use this method to get the normalized scrollLeft
property of an element.
You should explicitly pass the direction for the following reasons:
- Querying the
getComputedStyle
is expensive and might cause a reflow. - The behavior shouldn't be changed when direction is
ltr
.
The output is NaN
on the server. Otherwise, it will mimic the behavior of
WebKit
as it's the esiest to work with.
1import { getNormalizedScrollLeft } from 'normalize-scroll-left'; 2 3const element = document.getElementById('my-scrollable-container'); 4 5// element.scrollWidth = 100; 6 7const scrollLeft = getNormalizedScrollLeft(element, 'rtl'); 8 9// scrollLeft will always be from 0 (Most Left) to 100 (Most Right). 10// It will initially be 100, That means the most right.
setNormalizedScrollLeft
1function setNormalizedScrollLeft(
2 element: HTMLElement,
3 scrollLeft: number,
4 direction: 'rtl' | 'ltr',
5): void;
You can use this method to set the scrollLeft
property of an element as normalized.
You should explicitly pass the direction for the same reasons as getNormalizedScrollLeft
:
For scrollWidth = 100
the argument scrollLeft
must be between 0
and 100
. This
function will automatically convert it into something the current browser understands.
1import { setNormalizedScrollLeft } from 'normalize-scroll-left'; 2 3const element = document.getElementById('my-scrollable-container'); 4 5// element.scrollWidth = 100, element.clientWidth = 20; 6 7setNormalizedScrollLeft(element, 20, 'rtl'); 8 9// Will set element.scrollLeft to ... 10// 20 in WebKit (chrome) 11// -60 in Firefox/Opera 12// 60 in IE/Edge 13// Does nothing on the server
Typings
The typescript type definitions are also available and are installed via npm.
License
This project is licensed under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 3/14 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 4 are checked with a SAST tool
Reason
17 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-2j2x-2gpw-g8fm
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
2
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More