├── .gitignore ├── .prettierrc ├── .eslintrc.json ├── scripts └── minify.js ├── index.html ├── style.css ├── LICENSE.md ├── package.json ├── js ├── momentum.min.js └── momentum.js └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | node_modules/ 4 | dist -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 120, 3 | "tabWidth": 2, 4 | "semi": true, 5 | "singleQuote": true, 6 | "trailingComma": "es5", 7 | "bracketSpacing": true, 8 | "jsxBracketSameLine": true 9 | } 10 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "parserOptions": { 4 | "ecmaVersion": 6, 5 | "ecmaFeatures": { 6 | "jsx": true 7 | } 8 | }, 9 | "env": { 10 | "jest": true, 11 | "mocha": true 12 | }, 13 | "plugins": ["prettier"], 14 | "extends": ["standard", "prettier"], 15 | "rules": { 16 | "prettier/prettier": "error" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /scripts/minify.js: -------------------------------------------------------------------------------- 1 | var compressor = require('node-minify'); 2 | 3 | // With Promise 4 | var promise = compressor.minify({ 5 | compressor: 'uglifyjs', 6 | input: './js/momentum.js', 7 | output: './js/momentum.min.js', 8 | }); 9 | 10 | promise 11 | .then(function(min) { 12 | console.log('Successfully Minified File!'); 13 | }) 14 | .catch(function(error) { 15 | console.log('Failed to minify file.', error); 16 | }); 17 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | momentum 9 | 10 | 11 | 12 | 13 |
14 |
15 |
16 |
17 |
18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | body { 2 | --noise-interval: 1000; 3 | min-height: 300vh; 4 | transition: opacity 1s; 5 | opacity: var(--loaded); 6 | } 7 | 8 | .test { 9 | width: 100px; 10 | height: 100px; 11 | background: #555; 12 | border-radius: 50%; 13 | top: 0; 14 | left: 32px; 15 | position: absolute; 16 | } 17 | 18 | .test2 { 19 | top: 200px; 20 | left: 400px; 21 | width: 40px; 22 | height: 30px; 23 | background: blue; 24 | position: absolute; 25 | } 26 | .momentumcss { 27 | transform: translatey(calc(var(--scrolly)*1.5px + 50px)); 28 | } 29 | 30 | .jitter { 31 | transform: translateX(calc(var(--noise)*10px - 5px)); 32 | transition: transform 1000ms linear; 33 | } 34 | .initter { 35 | width: 100px; 36 | height: 100px; 37 | background: green; 38 | transform: translateY(calc(var(--noise)*10px - 5px)); 39 | transition: transform 200ms linear; 40 | transition-delay: -150ms; 41 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright © 2024 [Scott Kellum](https://scottkellum.com/) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | **The software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.** -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "momentum-css", 3 | "version": "1.0.0", 4 | "description": "Use JS events within your CSS", 5 | "main": "js/momentum.js", 6 | "scripts": { 7 | "build": "node ./scripts/minify.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/scottkellum/momentum.git" 12 | }, 13 | "keywords": [ 14 | "movement", 15 | "parallax", 16 | "events", 17 | "css", 18 | "style", 19 | "lint", 20 | "height", 21 | "size" 22 | ], 23 | "author": "Scott Kellum", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/scottkellum/momentum/issues" 27 | }, 28 | "homepage": "https://github.com/scottkellum/momentum#readme", 29 | "devDependencies": { 30 | "babel-eslint": "^8.2.1", 31 | "eslint": "^4.16.0", 32 | "eslint-config-prettier": "^2.5.0", 33 | "eslint-config-standard": "^11.0.0-beta.0", 34 | "eslint-plugin-import": "^2.8.0", 35 | "eslint-plugin-node": "^5.2.1", 36 | "eslint-plugin-prettier": "^2.5.0", 37 | "eslint-plugin-promise": "^3.6.0", 38 | "eslint-plugin-standard": "^3.0.1", 39 | "husky": "^0.14.3", 40 | "lint-staged": "^6.0.1", 41 | "node-minify": "^3.3.0", 42 | "prettier": "^1.10.2" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /js/momentum.min.js: -------------------------------------------------------------------------------- 1 | (function(){var e,t,o,n,r,i,s,l,d,c,m,u,a,f,w;function p(){e.setProperty("--viewportwidth",window.innerWidth),e.setProperty("--viewportheight",window.innerHeight);for(var t=0;t` tag. 8 | 9 | ## Usage 10 | 11 | All values are unitless meaning you will need to add your units to them. For example, `calc(var(--scrolly)*1px)` results in a pixel value for the vertical scroll position. 12 | 13 | ### Global variables 14 | 15 | 16 |
PropertyDescription 17 |
--loaded
0 before the page is loaded, 1 when the page is done loading. 18 |
--viewportwidth
The width of the viewport. 19 |
--viewportheight
The height of the viewport. 20 |
--scrollx
Distance scrolled on the X axis. 21 |
--scrolly
Distance scrolled on the Y axis. 22 |
--clientx
Pointer coordinates on the X axis. This includes touch events. 23 |
--clienty
Pointer coordinates on the Y axis. This includes touch events. 24 |
--pointerdown
Boolean value if the pointer is up or down. Use touchpoints below for touch events. 25 |
--touchpoints
Number of touch points currently active. 26 |
--orientationalpha
Gyroscopic orientation along the alpha axis. Only avalible in some mobile browsers. 27 |
--orientationbeta
Gyroscopic orientation along the beta axis. Only avalible in some mobile browsers. 28 |
--orientationgamma
Gyroscopic orientation along the gamma axis. Only avalible in some mobile browsers. 29 |
--compassheading
Compass heading. Only avalible in some mobile browsers. 30 |
--random
A random value between 0 and 1. 31 |
--noise
A random value between 0 and 1 updated every 100ms unless otherwise specified with --noise-interval on the <body> tag. Use a --noise-interval of 0 to disable jitter. 32 |
33 | 34 | ### Element variables 35 | 36 | To trigger element variables, add the class `momentumcss` to your elemnt. 37 | 38 | 39 |
PropertyDescription 40 |
--width
Width of the element 41 |
--height
Height of the element 42 |
--top
Element offsetTop value 43 |
--left
Element offsetLeft value 44 |
--random
A random value between 0 and 1. This overrides the global value in the CSS cascade. 45 |
46 | -------------------------------------------------------------------------------- /js/momentum.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | 3 | var momentumRoot; 4 | var momentumEls; 5 | var scrollX, scrollY, clientX, clientY, pointerDown, touches, orientAlpha, orientBeta, orientGamma, compassHeading; 6 | 7 | // Page and element location and dimensions 8 | function momentumInit() { 9 | momentumRoot.setProperty('--viewportwidth', window.innerWidth); 10 | momentumRoot.setProperty('--viewportheight', window.innerHeight); 11 | for (var i = 0; i < momentumEls.length; i++) { 12 | var e = momentumEls[i]; 13 | e.style.setProperty('--width', e.offsetWidth); 14 | e.style.setProperty('--height', e.offsetHeight); 15 | e.style.setProperty('--top', e.offsetTop); 16 | e.style.setProperty('--left', e.offsetLeft); 17 | } 18 | } 19 | 20 | function updateProperties() { 21 | momentumRoot.setProperty('--scrollx', scrollX); 22 | momentumRoot.setProperty('--scrolly', scrollY); 23 | momentumRoot.setProperty('--clientx', clientX); 24 | momentumRoot.setProperty('--clienty', clientY); 25 | momentumRoot.setProperty('--pointerdown', pointerDown); 26 | momentumRoot.setProperty('--touches', touches); 27 | momentumRoot.setProperty('--orientalpha', orientAlpha); 28 | momentumRoot.setProperty('--orientbeta', orientBeta); 29 | momentumRoot.setProperty('--orientgamma', orientGamma); 30 | momentumRoot.setProperty('--compassheading', compassHeading); 31 | } 32 | 33 | function requestUpdate() { 34 | window.requestAnimationFrame(updateProperties); 35 | } 36 | 37 | // When the DOM is loaded 38 | document.addEventListener('DOMContentLoaded', function () { 39 | momentumRoot = document.body.style; 40 | momentumEls = document.querySelectorAll('.momentumcss'); 41 | momentumRoot.setProperty('--loaded', 0); 42 | 43 | // Initialize variables 44 | scrollX = window.scrollX; 45 | scrollY = window.scrollY; 46 | clientX = 0; 47 | clientY = 0; 48 | pointerDown = 0; 49 | touches = 0; 50 | orientAlpha = 0; 51 | orientBeta = 0; 52 | orientGamma = 0; 53 | compassHeading = 0; 54 | 55 | momentumRoot.setProperty('--random', Math.random()); 56 | momentumRoot.setProperty('--noise', Math.random()); 57 | 58 | // Scroll events 59 | window.addEventListener('scroll', function() { 60 | scrollX = window.scrollX; 61 | scrollY = window.scrollY; 62 | requestUpdate(); 63 | }, false); 64 | 65 | // Pointer events 66 | window.addEventListener('pointermove', function(e) { 67 | clientX = e.clientX; 68 | clientY = e.clientY; 69 | requestUpdate(); 70 | }, false); 71 | 72 | window.addEventListener('pointerdown', function(e) { 73 | pointerDown = 1; 74 | requestUpdate(); 75 | }, false); 76 | 77 | window.addEventListener('pointerup', function(e) { 78 | pointerDown = 0; 79 | requestUpdate(); 80 | }, false); 81 | 82 | // Override pointer events if touch events 83 | window.addEventListener('touchmove', function(e) { 84 | clientX = e.touches[0].clientX; 85 | clientY = e.touches[0].clientY; 86 | requestUpdate(); 87 | }, false); 88 | 89 | window.addEventListener('touchstart', function(e) { 90 | pointerDown = 0; 91 | clientX = e.touches[0].clientX; 92 | clientY = e.touches[0].clientY; 93 | touches = e.targetTouches.length; 94 | requestUpdate(); 95 | }, false); 96 | 97 | window.addEventListener('touchend', function(e) { 98 | touches = e.targetTouches.length; 99 | requestUpdate(); 100 | }, false); 101 | 102 | // Device orientation 103 | window.addEventListener('deviceorientation', function(e) { 104 | orientAlpha = e.alpha; 105 | orientBeta = e.beta; 106 | orientGamma = e.gamma; 107 | compassHeading = e.webkitCompassHeading; 108 | requestUpdate(); 109 | }); 110 | 111 | momentumInit(); 112 | for (var i = 0; i < momentumEls.length; i++) { 113 | var e = momentumEls[i]; 114 | e.style.setProperty('--random', Math.random()); 115 | } 116 | }, false); 117 | 118 | // When the page has fully loaded 119 | window.onload = function() { 120 | momentumInit(); 121 | momentumRoot.setProperty('--loaded', 1); 122 | 123 | // Noise 124 | var i = window.getComputedStyle(document.body).getPropertyValue('--noise-interval'); 125 | if (i != '0') { 126 | window.setInterval(function() { 127 | momentumRoot.setProperty('--noise', Math.random()); 128 | }, i || 100); 129 | } else { 130 | momentumRoot.setProperty('--noise', Math.random()); 131 | } 132 | }; 133 | 134 | window.addEventListener('resize', momentumInit); 135 | })(); 136 | --------------------------------------------------------------------------------