├── .DS_Store ├── README.md ├── images ├── .DS_Store ├── funny-big-gun.jpg ├── stickEmUp.jpg ├── stickEmUpHand.jpg └── stickEmUpHandBW.jpg ├── index.html ├── js ├── .DS_Store └── stickEmUp.js └── style ├── .DS_Store └── main.css /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benrlodge/stickEmUp/b86e94c8691a6671b9dea156fb7b589c0b28ac88/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Stick Em Up! 2 | 3 | Stick Em Up is a jQuery plugin that provides easy way to determine when an element has been reached, passed, and passed back. 4 | 5 | Demo and documentation: [http://www.benrlodge.com/projects/stickEmUp](http://www.benrlodge.com/projects/stickEmUp) 6 | -------------------------------------------------------------------------------- /images/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benrlodge/stickEmUp/b86e94c8691a6671b9dea156fb7b589c0b28ac88/images/.DS_Store -------------------------------------------------------------------------------- /images/funny-big-gun.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benrlodge/stickEmUp/b86e94c8691a6671b9dea156fb7b589c0b28ac88/images/funny-big-gun.jpg -------------------------------------------------------------------------------- /images/stickEmUp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benrlodge/stickEmUp/b86e94c8691a6671b9dea156fb7b589c0b28ac88/images/stickEmUp.jpg -------------------------------------------------------------------------------- /images/stickEmUpHand.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benrlodge/stickEmUp/b86e94c8691a6671b9dea156fb7b589c0b28ac88/images/stickEmUpHand.jpg -------------------------------------------------------------------------------- /images/stickEmUpHandBW.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benrlodge/stickEmUp/b86e94c8691a6671b9dea156fb7b589c0b28ac88/images/stickEmUpHandBW.jpg -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | stickEmUp! 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 30 | 31 |
32 | 33 |

Stick Em Up! .js

34 | 35 |

Flexible, simple, sticky stuff

36 |

I created Stick Em Up so I could have a flexible and easy way to set fixed positioning on elements based on where visitors are scrolling on webpages.

37 | 38 | 39 |
40 |

Quick Start Guide

41 | 42 |

Getting started is easy, no options necessary other than the element you want to target. Let's take the sidebar to the left for an example.

43 |

You invoke the plugin as so:

44 | 45 |
 46 | <script>
 47 |   // For basic use
 48 |   $('.sidebar').stickEmUp();
 49 |   
 50 |   // To access additional public methods:
 51 |   $('.sidebar').stickEmUp().data('plugin_stickEmUp');
 52 | </script>
 53 | 
54 | 55 |

When the sidebar reaches the top of the viewport, the class stickeEmUp (which can be overridden in the options) is added to the sidebar element. This class would contain the position:fixed and any other styling you wish to add, creating the sticky effect. When you scroll back up, once the sidebar has reached its original position, the class is removed, and the element regains it's original positioning.

56 |

Given the unique needs of an elements fixed position specific to a webpage and it's surrounding context, this plugin does not attempt to get into any of that. Instead, the stickEmUp or other named class you provide is added and removed, and you are left to create the CSS needed for your desired effect.

57 | 58 |

The code below is an example of what is used on this page.

59 | 60 |
 61 | <style>
 62 |   .sidebar{
 63 |     position: absolute;
 64 |     width: 200px;
 65 |     margin-top: 130px;
 66 |     background: #fff;
 67 |     border-radius: 4px;
 68 |     padding: 20px 0;
 69 |   }
 70 |   .sidebar.stickEmUp{
 71 |     position: fixed;
 72 |     margin-top: 10px;
 73 |   }
 74 | </style>
 75 | 
 76 | <script>
 77 |   $('.sidebar').stickEmUp({
 78 |     stickOffset: 10,
 79 |     callback: function(info){
 80 |       console.log(info);
 81 |     }
 82 |   });
 83 | </script>
 84 | 
85 | 86 |
87 | 88 |
89 |

Additional Features

90 |

You can disable the plugin at any time by calling the public disableSticky method, and re-enable 91 | the plugin with defaultSticky. Be sure to include the .data('plugin_stickEmUp') if using 92 | this feature.

93 | 94 | 95 | 96 | 97 | 98 |
 99 |   // Set a variable for your element:
100 |   
101 |   var stickEm = $('.sidebar').stickEmUp().data('plugin_stickEmUp');
102 | 
103 |   // Disable plugin:
104 |   $('[data-action="stop"]').click(function(){
105 |     stickEm.disableSticky();
106 |   })
107 | 
108 |   // Re-enable plugin
109 |   $('[data-action="stop"]').click(function(){
110 |     stickEm.defaultSticky();
111 |   })
112 | 
113 | 114 | 115 | 116 |
117 | 118 | 119 |
120 |

Download

121 |

View it on Github

122 |

Download ZIP

123 |
 git clone https://github.com/benrlodge/stickEmUp.git 
124 | 125 | 126 |
127 | 128 | 129 | 130 |
131 |

About

132 | 133 |

Setting elements sticky at certain positions on pages is a frequently requested feature. As a resourceful (AKA lazy) developer I looked for plugins that would help me do this without writing my own. I wanted something simple, but couldn't find it, so I built this once.

134 |

This plugin will tell you when your given element is at a certain point on the page, what direction the user was scrolling when it hit that element, adds a class to the element during that time, and provides a callback throughout all of this scrolling to do what you please with this information.

135 | 136 |

This is all I've needed for my basic needs, so that's all it provides. If you have any ideas for additional features, please feel free to submit a Github issue or submit a pull request

137 |
138 |
139 |
140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 166 | 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /js/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benrlodge/stickEmUp/b86e94c8691a6671b9dea156fb7b589c0b28ac88/js/.DS_Store -------------------------------------------------------------------------------- /js/stickEmUp.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * stickEmUp! 3 | * Original author: @benrlodge 4 | * Licensed under the MIT license 5 | */ 6 | 7 | ;(function ( $, window, document, undefined ) { 8 | 9 | // Defaults 10 | var pluginName = 'stickEmUp', 11 | document = window.document, 12 | defaults = { 13 | stickyClass: 'stickEmUp', 14 | stickOffset: 0, 15 | callback: '' 16 | }; 17 | 18 | // The actual plugin constructor 19 | function Plugin( element, options ) { 20 | this.element = element; 21 | this.options = $.extend( {}, defaults, options); 22 | this._defaults = defaults; 23 | this._name = pluginName; 24 | this.init(); 25 | } 26 | 27 | 28 | // Methods 29 | Plugin.prototype = { 30 | sticky: false, 31 | elCachedPosition: '', 32 | pastOffset: 0, 33 | direction: '', 34 | 35 | theStateOfThings: function(){ 36 | var dos = this.docOffset(); 37 | var eos = this.elOffset(); 38 | var dir = this.direction; 39 | var stic = this.sticky; 40 | 41 | return { 42 | docOffset: dos, 43 | elOffset: eos, 44 | direction: dir, 45 | stickEmUp: stic 46 | }; 47 | }, 48 | 49 | docOffset: function() { 50 | return $(document).scrollTop(); 51 | }, 52 | 53 | elOffset: function() { 54 | return parseInt($(this.element).offset().top - this.options.stickOffset); 55 | }, 56 | 57 | stick: function() { 58 | if(this.sticky){ return; } 59 | $(this.element).addClass(this.options.stickyClass); 60 | this.sticky = true; 61 | }, 62 | 63 | unstick: function() { 64 | if(!this.sticky){ return; } 65 | $(this.element).removeClass(this.options.stickyClass); 66 | this.sticky = false; 67 | }, 68 | 69 | cachePositions: function(){ 70 | this.elCachedPosition = this.elOffset(); 71 | }, 72 | 73 | checkPositions: function(){ 74 | 75 | if(this.disabled){ 76 | return; 77 | } 78 | 79 | var docOff = this.docOffset(); 80 | 81 | // set direction 82 | this.direction = this.pastOffset < docOff ? 'down' : 'up'; 83 | this.pastOffset = docOff; 84 | 85 | 86 | // check stickiness 87 | if (docOff >= this.elCachedPosition){ 88 | this.stick(); 89 | } 90 | else{ 91 | this.unstick(); 92 | } 93 | 94 | // send callback 95 | var things = this.theStateOfThings(); 96 | if (typeof this.options.callback === 'function') { // make sure the callback is a function 97 | this.options.callback(things); 98 | } 99 | 100 | }, 101 | 102 | events: function(){ 103 | var scope; 104 | scope = this; 105 | $(window).scroll(function() { 106 | scope.checkPositions(); 107 | }); 108 | }, 109 | 110 | init: function(){ 111 | this.cachePositions(); 112 | this.events(); 113 | }, 114 | 115 | // override and remove sticky state 116 | disableSticky: function(){ 117 | this.disabled = true; 118 | this.unstick(); 119 | }, 120 | 121 | defaultSticky: function(){ 122 | this.disabled = false; 123 | this.checkPositions(); 124 | } 125 | }; 126 | 127 | // Build the Plugin 128 | $.fn[ pluginName ] = function ( options ) { 129 | return this.each(function () { 130 | if (!$.data( this, 'plugin_' + pluginName ) ) { 131 | $.data( this, 'plugin_' + pluginName, new Plugin( this, options ) ); 132 | } 133 | }); 134 | }; 135 | 136 | })( jQuery, window ); 137 | -------------------------------------------------------------------------------- /style/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benrlodge/stickEmUp/b86e94c8691a6671b9dea156fb7b589c0b28ac88/style/.DS_Store -------------------------------------------------------------------------------- /style/main.css: -------------------------------------------------------------------------------- 1 | 2 | .browsehappy { 3 | margin: 0.2em 0; 4 | background: #ccc; 5 | color: #000; 6 | padding: 0.2em 0; 7 | } 8 | 9 | body{ 10 | background: #ababab; 11 | font-family: sans-serif; 12 | font-size: 16px; 13 | } 14 | ul li{ 15 | list-style: none; 16 | } 17 | p{ 18 | margin: 10px 0; 19 | font-size: 16px; 20 | line-height: 1.6; 21 | } 22 | a{ 23 | color: black; 24 | text-decoration: underline; 25 | } 26 | img{ 27 | max-width: 100%; 28 | } 29 | 30 | 31 | h1, h2, h3, h4 { 32 | text-shadow: 1px 4px 1px rgba(0, 0, 0, 0.8); 33 | font-family: 'Bangers', sans-serif; 34 | letter-spacing: 3px; 35 | color: #fff; 36 | } 37 | 38 | h1{ 39 | font-size: 90px; 40 | } 41 | 42 | h1 span{ 43 | font-size: 30px; 44 | letter-spacing: 6px; 45 | } 46 | } 47 | h2{ 48 | margin: 20px 0; 49 | } 50 | section{ 51 | margin: 30px 0; 52 | } 53 | 54 | .code-j{ 55 | color: yellow; 56 | } 57 | .code-tag{ 58 | color: #FF4500; 59 | } 60 | .code-class{ 61 | color: #32CD32; 62 | } 63 | .code-comment{ 64 | color: #bbb; 65 | } 66 | pre{ 67 | background: black; 68 | margin: 10px 0; 69 | color: #fff; 70 | padding: 10px; 71 | } 72 | 73 | .container{ 74 | overflow: hidden; 75 | margin: 0 auto; 76 | max-width: 865px; 77 | position: relative; 78 | } 79 | 80 | .content{ 81 | margin-left: 250px; 82 | width: 600px; 83 | } 84 | 85 | .sidebar{ 86 | position: absolute; 87 | width: 200px; 88 | margin-top: 131px; 89 | background: #fff; 90 | border-radius: 4px; 91 | padding: 20px 0; 92 | } 93 | 94 | .sidebar.stickEmUp{ 95 | position: fixed; 96 | margin-top: 10px; 97 | 98 | } 99 | } 100 | 101 | 102 | .sidebar_nav-item{ 103 | margin: 10px 0; 104 | } 105 | .sidebar_nav-item-link{ 106 | display: block; 107 | } 108 | 109 | 110 | --------------------------------------------------------------------------------