├── .gitignore ├── test ├── support │ └── boot.js └── jquery-during-scroll_test.coffee ├── bower.json ├── package.json ├── karma.conf.js ├── source └── during-scroll.js └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | coverage 4 | -------------------------------------------------------------------------------- /test/support/boot.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | window.define = jasmine.createSpy('define'); 4 | window.define.amd = true; 5 | 6 | window.module = jasmine.createSpy('module'); 7 | 8 | window.jQuery = jasmine.createSpy('jQuery'); 9 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-during-scroll", 3 | "main": "source/during-scroll.js", 4 | "version": "1.1.0", 5 | "authors": [ 6 | "Jack " 7 | ], 8 | "description": "A plugin to handle callbacks during and after a scroll event.", 9 | "moduleType": [ 10 | "amd", 11 | "globals", 12 | "node" 13 | ], 14 | "keywords": [ 15 | "jQuery", 16 | "scroll", 17 | "onscroll" 18 | ], 19 | "license": "Creative Commons Attribution-ShareAlike 4.0 International", 20 | "homepage": "https://jack.ofspades.com/jquery-during-scroll/", 21 | "ignore": [ 22 | "**/.*", 23 | "node_modules", 24 | "bower_components", 25 | "test", 26 | "tests" 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-during-scroll", 3 | "version": "1.1.0", 4 | "description": "A plugin to handle callbacks during and after a scroll event.", 5 | "main": "source/during-scroll.js", 6 | "directories": { 7 | "test": "karma" 8 | }, 9 | "scripts": { 10 | "test": "./node_modules/karma/bin/karma start && CODECLIMATE_REPO_TOKEN=d8038d0e98ed952fe1894c601075aba6eee01c5ab7d0e4f82a93babfc7689c8a codeclimate < coverage/report-lcov/lcov.info" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/jacopotarantino/jquery-during-scroll.git" 15 | }, 16 | "keywords": [ 17 | "jQuery", 18 | "scroll", 19 | "onscroll" 20 | ], 21 | "author": "Jack ", 22 | "license": "Creative Commons Attribution-ShareAlike 4.0 International", 23 | "bugs": { 24 | "url": "https://github.com/jacopotarantino/jquery-during-scroll/issues" 25 | }, 26 | "homepage": "https://github.com/jacopotarantino/jquery-during-scroll", 27 | "devDependencies": { 28 | "codeclimate-test-reporter": "0.0.4", 29 | "jasmine-core": "^2.2.0", 30 | "karma": "^0.12.31", 31 | "karma-chrome-launcher": "^0.1.7", 32 | "karma-coffee-preprocessor": "^0.2.1", 33 | "karma-coverage": "^0.2.7", 34 | "karma-jasmine": "^0.3.5" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | module.exports = function(config) { 2 | config.set({ 3 | 4 | // base path, that will be used to resolve files and exclude 5 | basePath: './', 6 | 7 | // frameworks to use 8 | frameworks: ['jasmine'], 9 | 10 | // list of files / patterns to load in the browser 11 | files: [ 12 | 'test/support/boot.js', 13 | 'source/during-scroll.js', 14 | 'test/jquery-during-scroll_test.coffee' 15 | ], 16 | 17 | // test results reporter to use 18 | reporters: ['progress', 'dots', 'coverage'], 19 | 20 | preprocessors: { 21 | // source files, that you wanna generate coverage for 22 | // do not include tests or libraries 23 | // (these files will be instrumented by Istanbul) 24 | 'source/during-scroll.js': ['coverage'], 25 | '**/*.coffee': ['coffee'] 26 | }, 27 | 28 | coffeePreprocessor: { 29 | // options passed to the coffee compiler 30 | options: { 31 | bare: true, 32 | sourceMap: true 33 | }, 34 | // transforming the filenames 35 | transformPath: function(path) { 36 | return path.replace(/\.coffee$/, '.js'); 37 | } 38 | }, 39 | 40 | // optionally, configure the reporter 41 | coverageReporter: { 42 | type : 'html', 43 | dir: 'coverage/', 44 | reporters: [ 45 | { 46 | type : 'html', 47 | subdir : 'report-html' 48 | }, 49 | { 50 | type : 'lcov', 51 | subdir : 'report-lcov' 52 | } 53 | ] 54 | }, 55 | 56 | // web server port 57 | port: 9876, 58 | 59 | // enable / disable colors in the output (reporters and logs) 60 | colors: true, 61 | 62 | // level of logging 63 | logLevel: config.LOG_INFO, 64 | 65 | // enable / disable watching file and executing tests whenever any file changes 66 | autoWatch: true, 67 | 68 | // Start these browsers 69 | browsers: ['Chrome'], 70 | 71 | // If browser does not capture in given timeout [ms], kill it 72 | captureTimeout: 60000, 73 | 74 | captureConsole: false, 75 | 76 | hostname: 'localhost', 77 | 78 | // Continuous Integration mode 79 | // if true, it capture browsers, run tests and exit 80 | singleRun: true 81 | }); 82 | }; 83 | -------------------------------------------------------------------------------- /source/during-scroll.js: -------------------------------------------------------------------------------- 1 | (function(window) { 2 | 'use strict'; 3 | /** 4 | * @module duringScroll 5 | * @param {Object} opts - a hash of optional overrides. 6 | * @returns {Number} - a reference to the interval invoked. 7 | */ 8 | function duringScroll(opts) { 9 | var top_offset = document.body.scrollTop, 10 | state = 'not scrolling', 11 | noop = function() {}, 12 | default_options = { 13 | interval: 60, 14 | always: noop, 15 | scrollStart: noop, 16 | duringScroll: noop, 17 | afterScroll: noop 18 | }, 19 | options = merge_hashes(default_options, opts); 20 | 21 | function handle_scrolling(scrollStart, duringScroll, afterScroll) { 22 | if(state === 'not scrolling' && top_offset !== document.body.scrollTop) { 23 | // we've started scrolling 24 | top_offset = document.body.scrollTop; 25 | state = 'scrolling'; 26 | scrollStart(); 27 | 28 | } else if(state === 'scrolling' && top_offset !== document.body.scrollTop) { 29 | // we're still scrolling 30 | top_offset = document.body.scrollTop; 31 | duringScroll(); 32 | 33 | } else if(state === 'scrolling' && top_offset === document.body.scrollTop) { 34 | // we've stopped scrolling 35 | state = 'not scrolling'; 36 | afterScroll(); 37 | } 38 | } 39 | 40 | return setInterval(function() { 41 | options.always(); 42 | handle_scrolling(options.scrollStart, options.duringScroll, options.afterScroll); 43 | 44 | }, options.interval); 45 | } 46 | 47 | function merge_hashes() { 48 | var new_hash = {}; 49 | 50 | Array.prototype.forEach.call(arguments, function(obj) { 51 | Object.keys(obj).forEach(function(property, index) { 52 | new_hash[property] = obj[property]; 53 | }); 54 | }); 55 | 56 | return new_hash; 57 | } 58 | 59 | /** 60 | * @description export the module based on environment. 61 | */ 62 | var hasDefine = typeof window.define === 'function' && window.define.amd, 63 | hasExports = typeof window.module !== 'undefined', 64 | hasJquery = typeof window.jQuery !== 'undefined'; 65 | 66 | if( hasDefine ) { 67 | window.define(function() { 68 | return { 69 | duringScroll: duringScroll 70 | }; 71 | }); 72 | } 73 | 74 | if( hasExports ) { 75 | window.module.exports = duringScroll; 76 | } 77 | 78 | if( hasJquery ) { 79 | window.jQuery.duringScroll = duringScroll; 80 | } 81 | 82 | if( !hasDefine && !hasExports && !hasJquery ) { 83 | window.duringScroll = duringScroll; 84 | } 85 | 86 | })(window); 87 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # jQuery During Scroll 2 | 3 | A script to execute arbitrary code when a user starts to scroll, during the scroll and after a scroll has completed. This is different from the default browser and jQuery behavior in that it treats the entire scroll action as one long event instead of executing hundreds of times during a single scroll action. 4 | 5 | [![Code Climate](https://codeclimate.com/github/jacopotarantino/jquery-during-scroll/badges/gpa.svg)](https://codeclimate.com/github/jacopotarantino/jquery-during-scroll) 6 | 7 | [![Test Coverage](https://codeclimate.com/github/jacopotarantino/jquery-during-scroll/badges/coverage.svg)](https://codeclimate.com/github/jacopotarantino/jquery-during-scroll) 8 | 9 | 10 | ## Installation 11 | 12 | While the plugin is titled as though it's just a jQuery plugin, it is available in many formats. The plugin exposes itself as an AMD, CommonJS and jQuery plugin and exposes itself as a global "duringScroll" if none of those are found. 13 | 14 | Include the script after jQuery and any other dependencies(such as requirejs) if you want the plugin to bind to them instead of the global context. 15 | 16 | ### Via Bower 17 | 18 | ```bash 19 | bower install --save jquery-during-scroll 20 | ``` 21 | 22 | ### Manually 23 | 24 | Copy the file `source/during-scroll.js` into your project and reference it in a `