Repository: ameesme/SlimScroller.js Branch: master Commit: 1362f512e432 Files: 8 Total size: 15.4 KB Directory structure: gitextract_mp77rv2z/ ├── LICENSE ├── README.md ├── demo.html ├── dist/ │ └── init.js ├── install.json ├── package.json └── src/ ├── slimScroller.full.js └── slimScroller.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Mees Boeijen 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. ================================================ FILE: README.md ================================================ # slimScroller.js ![](http://forthebadge.com/images/badges/built-with-love.svg) Smoothly scroll to positions on your website, under 350 bytes, no dependencies. ## Two versions ### [SlimScroller Base](https://github.com/ameesme/SlimScroller.js/blob/master/dist/slimScroller.min.js) 343 bytes gzipped 580 bytes uncompressed The base version includes support for one type of scroll-target: - CSS selector ( eg. `slimScroller.scroll("p:first-of-type")` ) ### [SlimScroller Full](https://github.com/ameesme/SlimScroller.js/blob/master/dist/slimScroller.full.min.js) 570 bytes gzipped 1030 bytes uncompressed The full version includes support for three types of scroll-targets: - Position in pixels ( eg. `slimScroller.scroll(200)` ) - CSS selector ( eg. `slimScroller.scroll("p:first-of-type")` ) - Direct JavaScript HTML-element ( eg. `slimScroller.scroll(document.querySelector(p#hi))` ) ### Hasthags Want to bind SlimScroller to all anchor-tags on a page? Run `slimScroller.bind()` when the dom finishes loading. Only supported on [SlimScroller Full](https://github.com/ameesme/SlimScroller.js/blob/master/dist/slimScroller.full.min.js). ## How to use Just use the `slimScroller.scroll`-function with the following parameters. - `Required` Scroll-target (according to SlimScroller-version) - `Optional` Scroll-duration (`ms`) - `Optional` Horizontal-scrolling instead of vertical (`boolean`) - `Optional` Callback-function with final scroll-position as parameter ### NPM Run `npm install slimscroller`. ### Hosted #### Base `````` #### Full `````` ## Demo [View here](https://amees.me/files/public/SlimScroller.js/demo.html). ## NPM Run `npm install slimscroller`. ##License (MIT) Copyright © 2016 Mees Boeijen 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. ================================================ FILE: demo.html ================================================ slimScroller.js Demo

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

Scroll to a paragraph via an anchor-tag.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

This is a line of text that does not serve any actual purpose but to waste some of your energy.

================================================ FILE: dist/init.js ================================================ // This script is only used for installation via Eager. document.addEventListener("DOMContentLoaded", function(event) { slimScroller.bind(); }); ================================================ FILE: install.json ================================================ { "resources": { "body": [ { "type": "script", "src": "dist/slimScroller.min.js" }, { "type": "script", "src": "dist/init.js" } ] } } ================================================ FILE: package.json ================================================ { "name": "slimscroller", "version": "2.0.4", "description": "Slim smooth-scrolling under 350 bytes (gzipped), with no other dependencies.", "main": "dist/slimScroller.full.min.js", "author": "Mees Boeijen (https://amees.me)", "license": "MIT", "homepage": "https://github.com/ameesme/SlimScroller.js", "repository": "https://github.com/ameesme/SlimScroller.js", "keywords": ["tiny", "smooth scroll", "no dependencies"] } ================================================ FILE: src/slimScroller.full.js ================================================ var slimScroller = function (){ 'use strict'; var elapsed, horizontal; var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame; var scroll = function (target, horizontal, durationTime, callbackFunction, scrollTarget) { var scrollTargetReference = (scrollTarget && scrollTarget instanceof Element) || window; var duration = durationTime || 500; var horizontal = horizontal || false; var callback = callbackFunction || false; var startPosition = (horizontal) ? window.pageXOffset : window.pageYOffset; var total = (horizontal) ? pageXOffset.scrollWidth : pageXOffset.scrollHeight; var targetPosition = (parsePosition(target) > total) ? total : parsePosition(target); var clock = Date.now(); step(duration, horizontal, callback, startPosition, total, targetPosition, scrollTarget, clock)(); }; var easeInOutCubic = function (time) { return (time < 0.5) ? 4 * time * time * time : (time - 1) * (2 * time - 2) * (2 * time - 2) + 1; }; var calculatePosition = function (elapsed, duration, targetPosition, startPosition) { return (elapsed > duration) ? targetPosition : startPosition + (targetPosition - startPosition) * easeInOutCubic(elapsed / duration); }; var parsePosition = function (target){ var parseNumber = parseInt(target); var parseElement = target.offsetLeft; var parseSelector; try{ parseSelector = document.querySelector(target); }catch(e){} if (parseNumber) { // Target is pixel value return parseNumber; }else if(parseSelector){ // Target is CSS-selector return (horizontal) ? document.querySelector(target).offsetLeft : document.querySelector(target).offsetTop; }else if(parseElement !== undefined){ // Target is HTML-element return (horizontal) ? parseElement : target.offsetTop; }else{ // Unknown type throw new Error('Unknown type as target'); } }; var step = function (duration, horizontal, callback, startPosition, total, targetPosition, scrollTarget, clock) { return function() { elapsed = Date.now() - clock; var stepPosition = calculatePosition(elapsed, duration, targetPosition, startPosition); var scrollTargetReference = (scrollTarget && scrollTarget instanceof Element) || window; if (horizontal){ scrollTargetReference.scroll(stepPosition, 0); }else{ scrollTargetReference.scroll(0, stepPosition); } if (elapsed > duration) { if (callback) { callback([window.scrollX,window.scrollY]); } } else { requestAnimationFrame(step(duration, horizontal, callback, startPosition, total, targetPosition, scrollTarget, clock)); } } }; var bind = function (time, callbackFunction) { var allAnchors = document.querySelectorAll('a'); var i; for (i = 0; i < allAnchors.length; i++) { if (allAnchors[i].href == window.location.href.split('#')[0] + '#' + allAnchors[i].href.split('#')[1]) { allAnchors[i].addEventListener('click', function (event) { event.preventDefault(); slimScroller.scroll('#'+event.target.href.split('#')[1], null, time, callbackFunction); window.location.href = event.target.href; return; }); } } }; return { scroll: scroll, bind: bind }; }(); ================================================ FILE: src/slimScroller.js ================================================ var slimScroller = function (){ 'use strict'; var targetPosition,horizontal,clock,elapsed,duration,startPosition,total,callback,elementPosition; var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame; var scroll = function (target, horizontal, durationTime, callbackFunction) { duration = durationTime || 500; horizontal = horizontal || false; callback = callbackFunction || false; startPosition = (horizontal) ? window.pageXOffset : window.pageYOffset; total = (horizontal) ? document.body.scrollWidth : document.body.scrollHeight; elementPosition =(horizontal) ? document.querySelector(target).offsetLeft : document.querySelector(target).offsetTop; targetPosition = (elementPosition > total) ? total : elementPosition; clock = Date.now(); step(); }; var easeInOutCubic = function (time) { return (time < 0.5) ? 4 * time * time * time : (time - 1) * (2 * time - 2) * (2 * time - 2) + 1; }; var calculatePosition = function () { return (elapsed > duration) ? targetPosition : startPosition + (targetPosition - startPosition) * easeInOutCubic(elapsed / duration); }; var step = function () { elapsed = Date.now() - clock; var stepPosition = calculatePosition(); if (horizontal){ window.scroll(stepPosition, 0); }else{ window.scroll(0, stepPosition); } if (elapsed > duration) { if (callback) { callback([window.scrollX,window.scrollY]); } } else { requestAnimationFrame(step); } }; return { scroll: scroll }; }();