Skip to content

Instantly share code, notes, and snippets.

@albybarber
Last active December 10, 2015 13:58
Show Gist options
  • Save albybarber/4444173 to your computer and use it in GitHub Desktop.
Save albybarber/4444173 to your computer and use it in GitHub Desktop.
jQuery.sticky() is a jQuery plugin that adds bar at the bottom of the users screen with the pages primary actions if they are not within the users viewport. e.g. At the bottom of the screen and need to be scrolled to view.
/* ========================================================
* jQuery.sticky();
* flipreversestickynavthatollywants 1.0
* https://vendadocs.onconfluence.com/x/2ItZAw
* Author: Alby Barber
* ========================================================
* Copyright 2012 Venda
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ======================================================== */
(function($) {
"use strict";
$.fn.sticky = function() {
var $this = $(this),
$window = $(window),
position = $this.position();
var _actions = {
stick: function() {
$this.addClass('is_stuck');
},
unstick: function(){
$this.removeClass('is_stuck');
}
};
var _math = {
is_this_visible: function(){
if($this.length > 0){
var distance_fromtop = $window.height() + $window.scrollTop();
if ((position.top + $this.height()) > distance_fromtop){
_actions.stick();
}
else {
_actions.unstick();
}
}
}
};
$window.scroll(function() {
_math.is_this_visible();
});
$window.resize(function() {
_math.is_this_visible();
});
_math.is_this_visible();
};
})(jQuery);
<!doctype html>
<title>Demo</title>
<link rel="stylesheet" href="styles.css" />
<div class="tall-element"></div>
<div class="sticky-element">
<p>Try resizing the window's height..</p>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="flipreversestickynavthatollywants.js"></script>
<script>
jQuery(function() {
jQuery('.sticky-element').sticky(); // run .ready or when required
});
</script>
body {
margin: 0;
padding: 0;
background-color: salmon;
}
.tall-element{
height: 400px;
}
.sticky-element {
position: relative;
padding: 0 0 0 50px;
}
.sticky-element p {
font-family: sans-serif;
font-size: 2em;
color: #fff;
margin: 0;
}
.is_stuck {
-webkit-transition: background-color 0.35s ease-in-out;
-moz-transition: background-color 0.35s ease-in-out;
-ms-transition: background-color 0.35s ease-in-out;
-o-transition: background-color 0.35s ease-in-out;
transition: background-color 0.35s ease-in-out;
padding: 5px 0 5px 50px;
background-color: rgba(44, 44, 44, 0.64);
position: fixed;
left: 0px;
bottom: 0px;
width: 100%;
z-index: 2;
}
@stevegoldberg
Copy link

That's great Alby but it's still possible to have the text hidden.

Also, no point linking to Confluence as no one will be able to access it ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment