├── src ├── mocha.opts └── scrollingUtilities.js ├── .gitignore ├── tea.yaml ├── package.json ├── LICENSE ├── test └── index.test.js └── README.md /src/mocha.opts: -------------------------------------------------------------------------------- 1 | --recursive 2 | --ui bdd 3 | --reporter spec 4 | --timeout 5000 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Directories 2 | node_modules/ 3 | dist/ 4 | coverage/ 5 | .cache/ 6 | 7 | # Files 8 | .env 9 | .DS_Store -------------------------------------------------------------------------------- /tea.yaml: -------------------------------------------------------------------------------- 1 | # https://tea.xyz/what-is-this-file 2 | --- 3 | version: 1.0.0 4 | codeOwners: 5 | - '0x9BAac40A197bbb110ae5B2f31342D8d15D734832' 6 | - '0xD4e4DdE6b565D8f5A516fF3458f35C387C648775' 7 | - '0x1aaa6646eb35b7B9024f6f4f334Baa4c55af23f0' 8 | - '0xbeb35Bb154767482CC93bf476628e31F0Bf6470C' 9 | - '0x7ecA45585B56FDEb8Ce0cb69DEe47F04d3c76889' 10 | - '0x01329c2d9337994763288A01436Faa07d66c18A6' 11 | - '0xF8cd6657F7A3C1FB8803bA00dAc049EB2486C3B2' 12 | - '0x0a1F0913eB913e7cb0903A9b1DD3692B6a90B34f' 13 | - '0x42E973c701A08474cc849ad349BBF51b524bd66d' 14 | - '0x270893dE562Ee9DF3558656efabEdC3Fec274319' 15 | - '0x14084Dd5B2015eaC8e95F04b0d77d06DC374674b' 16 | quorum: 1 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "onedionys-scrolling-utilities", 3 | "version": "5.0.0", 4 | "description": "One Dionys (Scrolling Utilities) - Functions to detect scroll position and provide control over scroll behavior.", 5 | "main": "src/scrollingUtilities.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "mocha" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/onedionys/onedionys-scrolling-utilities.git" 15 | }, 16 | "keywords": [ 17 | "onedionys", 18 | "tea", 19 | "package-manager", 20 | "scrolling-utilities" 21 | ], 22 | "author": "One Dionys", 23 | "license": "ISC", 24 | "bugs": { 25 | "url": "https://github.com/onedionys/onedionys-scrolling-utilities/issues" 26 | }, 27 | "homepage": "https://github.com/onedionys/onedionys-scrolling-utilities#readme", 28 | "dependencies": { 29 | "@types/chai": "^4.3.12", 30 | "@types/mocha": "^10.0.6", 31 | "chai": "^5.1.0", 32 | "mocha": "^10.3.0" 33 | }, 34 | "devDependencies": { 35 | "chai": "^5.1.0", 36 | "mocha": "^10.3.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 ONE DIONYS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /test/index.test.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'); 2 | const scrollingUtilities = require('../src/scrollingUtilities'); 3 | 4 | describe('Scrolling Utilities', function() { 5 | describe('#isScrolledToBottom()', function() { 6 | it('should return true if scrolled to the bottom', function() { 7 | // Mocking scrollY and innerHeight for testing purposes 8 | global.window = { scrollY: 100, innerHeight: 200 }; 9 | global.document = { body: { offsetHeight: 300 } }; 10 | assert.strictEqual(scrollingUtilities.isScrolledToBottom(), true); 11 | }); 12 | 13 | it('should return false if not scrolled to the bottom', function() { 14 | global.window = { scrollY: 100, innerHeight: 200 }; 15 | global.document = { body: { offsetHeight: 400 } }; 16 | assert.strictEqual(scrollingUtilities.isScrolledToBottom(), false); 17 | }); 18 | }); 19 | 20 | describe('#isScrolledToTop()', function() { 21 | it('should return true if scrolled to the top', function() { 22 | global.window = { scrollY: 0 }; 23 | assert.strictEqual(scrollingUtilities.isScrolledToTop(), true); 24 | }); 25 | 26 | it('should return false if not scrolled to the top', function() { 27 | global.window = { scrollY: 100 }; 28 | assert.strictEqual(scrollingUtilities.isScrolledToTop(), false); 29 | }); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/scrollingUtilities.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Scroll Utilities 3 | * A collection of utility functions for handling scrolling events. 4 | */ 5 | 6 | /** 7 | * Function to check if the page is scrolled to the bottom. 8 | * @returns {boolean} True if scrolled to the bottom, false otherwise. 9 | */ 10 | function isScrolledToBottom() { 11 | return window.innerHeight + window.scrollY >= document.body.offsetHeight; 12 | } 13 | 14 | /** 15 | * Function to check if the page is scrolled to the top. 16 | * @returns {boolean} True if scrolled to the top, false otherwise. 17 | */ 18 | function isScrolledToTop() { 19 | return window.scrollY === 0; 20 | } 21 | 22 | /** 23 | * Function to smoothly scroll to a specific element on the page. 24 | * @param {HTMLElement} element The element to scroll to. 25 | * @param {number} duration The duration of the scroll animation in milliseconds. 26 | */ 27 | function scrollToElement(element, duration) { 28 | const targetPosition = element.offsetTop; 29 | const startPosition = window.scrollY; 30 | const distance = targetPosition - startPosition; 31 | let startTime = null; 32 | 33 | function animation(currentTime) { 34 | if (startTime === null) startTime = currentTime; 35 | const timeElapsed = currentTime - startTime; 36 | const run = ease(timeElapsed, startPosition, distance, duration); 37 | window.scrollTo(0, run); 38 | if (timeElapsed < duration) requestAnimationFrame(animation); 39 | } 40 | 41 | function ease(t, b, c, d) { 42 | t /= d / 2; 43 | if (t < 1) return c / 2 * t * t + b; 44 | t--; 45 | return -c / 2 * (t * (t - 2) - 1) + b; 46 | } 47 | 48 | requestAnimationFrame(animation); 49 | } 50 | 51 | // Exporting functions for use in other modules 52 | module.exports = { 53 | isScrolledToBottom, 54 | isScrolledToTop, 55 | scrollToElement 56 | }; 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Welcome to One Dionys - Scrolling Utilities! 👋

2 | 3 |

Functions to detect scroll position and provide control over scroll behavior. 💖

4 | 5 |

6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

14 | 15 | ## 💾 Requirements 16 | 17 | * `Web Browser` - Can be used as an emulator to build applications. Example [Chrome, Firefox, Safari & Opera]. 18 | * `Internet` - Because many use CDN and to make it easier to find solutions to all problems. 19 | 20 | ## 🎯 How To Use 21 | 22 | #### Example Syntax 23 | 24 | ```javascript 25 | const scrollingUtilities = require('./src/scrollingUtilities'); 26 | 27 | // Example usage of isScrolledToBottom function 28 | console.log(scrollingUtilities.isScrolledToBottom()); // Output: true/false 29 | 30 | // Example usage of isScrolledToTop function 31 | console.log(scrollingUtilities.isScrolledToTop()); // Output: true/false 32 | 33 | // Example usage of scrollToElement function 34 | const targetElement = document.getElementById('target'); 35 | scrollingUtilities.scrollToElement(targetElement, 1000); // Scroll to the target element smoothly in 1 second 36 | ``` 37 | 38 | #### Explanation 39 | 40 | * `isScrolledToBottom`: Checks if the page is scrolled to the bottom. 41 | * `isScrolledToTop`: Checks if the page is scrolled to the top. 42 | * `scrollToElement`: Smoothly scrolls to a specific element on the page. 43 | 44 | #### Return Value 45 | 46 | * `isScrolledToBottom`: Returns true if scrolled to the bottom, false otherwise. 47 | * `isScrolledToTop`: Returns true if scrolled to the top, false otherwise. 48 | * `scrollToElement`: No return value. 49 | 50 | ## 📆 Release Date 51 | 52 | * v1.0.0 : 08 March 2024 53 | * v1.0.1 : 11 March 2024 54 | * v4.0.0 : 11 March 2024 55 | * v4.0.1 : 13 March 2024 56 | * v4.0.2 : 18 March 2024 57 | * v5.0.0 : 31 March 2024 58 | 59 | ## 🧑 Author 60 | 61 | * Facebook : Oned Ionys 62 | * Instagram : @onedionys 63 | * Twitter : @onedionys 64 | * LinkedIn : @onedionys 65 | 66 | ## 📝 License 67 | 68 | * Copyright © 2024 One Dionys 69 | * **One Dionys - Scrolling Utilities is an open source project licensed under the MIT license** 70 | 71 | ## ☕️ Suppport & Donation 72 | 73 | Love One Dionys - Scrolling Utilities? Support this project by donating or sharing with others in need. 74 | 75 | 76 | 77 | **Made with ❤️ One Dionys** 78 | --------------------------------------------------------------------------------