scroll events - sticky box

A jQuery plugin that moves an element to its original position within the browser's viewport after the user has scrolled the page; demonstrates the use of functional programming principles

by Patrick Hund

HTML

<div class="stickyBox lime left">hello</div>
<div class="stickyBox powderblue center">awesome</div>
<div class="stickyBox hotpink right">world</div>Pergo coctione, et ego, et tu oblivisci Pinkman. Obliviscendum hoc unquam factum. Intelligamus hoc in sola SINGULTO multo aliter atque fructuosa negotium structura. Malo B. Option. Ille vivere. Ut ad te quaerebam ... purgare caeli. Sunt uh ... nonnullus propter errorem qui de rebus inter nos et iacere puto suus in causa, id est in mensa. Levir meus, priusquam oppugnarent tempus quis, admonere dicitur. Credo quod idem mihi praesidium. Suspicio? Bene ... tunc ibimus? Quis uh ... CONEXUS locus his diebus? Quisque semper aliquid videtur, in volutpat mauris. Nolo enim dicere. Vobis neque ab aliis. Ego feci memetipsum explicans. Gus mortuus est. Lorem opus habeo. Jackson Isai? Tu quoque ... A te quidem a ante. Vos scitis quod blinking res Ive 'been vocans super vos? Et conteram illud, et conteram hoc. Maledicant druggie excors. Iam hoc tu facere conatus sum ad te in omni tempore? Ludum mutavit. Verbum est ex. Et ... sunt occidat. Videtur quod est super omne oppidum. Quis transfretavit tu iratus es contudit cranium cum dolor apparatus. Qui curis! Modo nobis certamen est, qui non credunt at. Nonne vides quid sit? Tu es ... Jesse me respice. Tu ... blowfish sunt. A blowfish! Cogitare. Statura pusillus, nec sapientium panem, nec artificum. Sed predators facile prædam blowfish secretum telum non se habet. Non ille? Quid faciam blowfish, Isai. Blowfish quid faciat? In blowfish inflat, purus? Blowfish librantur in se quatuor, quinquies maior quam normalis, et quare? Quare id faciam? Ut terrorem facit, qui quid. Terrent! Ut alter, scarier pisces agminis off. Et quod tu es? Vos blowfish. Tu iustus in omnibus visio. Vides ... suus ' suus 'non aliud aerem. Nunc ... qui cum partibus blowfish Isai? Tu damnare ius. Vos blowfish. Dicere iterum. Dicere illam quasi velis eam. Es BLOWFISH! Ut sibi fuerat socius sagittis. Ego intervenerit....

CSS

body {
    width: 2000px;
}
.stickyBox {
    position: absolute;
    border-radius: 20px;
    border: 1px solid black;
    width: 150px;
    text-align:center;
    padding: 15px;
}
.powderblue {
    background-color: powderblue;
}
.lime {
    background-color: lime;
}
.hotpink {
    background-color: hotpink;
}
.left {
    left: 0px;
}
.right {
    right: 0px;
}
.center {
    left: calc(50% - 75px);
}

JavaScript

// A jQuery plugin that moves an element to its original position within the browser's viewport 
// after the user has scrolled the page; demonstrates the use of functional programming principles

$.fn.stickyBox = function (input) {
    // given a preficate function and an update interval (ms),
    // creates a function that accepts a callback function that is executed when 
    // the predicate function returns true
    function periodicChecker(predf, ms) {
        return function checkf(callback) {
            window.setTimeout(function () {
                if (predf()) {
                    callback();
                    return;
                }
                checkf(callback);
            }, ms);
        };
    }

    // given an update function, creates a function that returns false if the update function
    // returns a different value than before and true if the update function returns the same value
    function updater(updatef) {
        var oldValue = updatef();
        return function () {
            var newValue = updatef();
            if (oldValue !== newValue) {
                oldValue = newValue;
                return false;
            }
            return true;
        };
    }

    // initialize jQuery plugin logic on each selected element, returning the element set
    return this.each(function () {
        var $element = $(this), // jquery element that the plugin instance is working on
            position, // the position of the element before the scrolling
            defaults, // default settings
            settings, // settings of this plugin instance
            init, // initialization functions
            get, // utility functions for getting stuff
            scrollPosChecker;

        // default plugin settings, can by overridden by passing options
        defaults = {
            // position check interval
            ms: 100,

            // this is the "payload" function that determines what happens when the user stops
   ...