Skip to content

Instantly share code, notes, and snippets.

Created November 15, 2012 18:03
Show Gist options
  • Save anonymous/4080160 to your computer and use it in GitHub Desktop.
Save anonymous/4080160 to your computer and use it in GitHub Desktop.
A CodePen by Fabrice Weinberg. position:sticky; polyfill/shim - Just tried to make a polyfill for position:sticky; Wrote a Modernizr test. Tested in Chrome 23 (supported), Safari 6 and Firefox 14/15 doesn't work on iOS.
<div id="shim"></div>
<div class="textHolder"><h1>Try to scroll</h1><div class="sticky">Java script</div>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</div><div class="textHolder"><h1>Try to scroll</h1><div class="sticky">position:sticky</div>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</div><div class="textHolder"><h1>Try to scroll</h1><div class="sticky">shim</div>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</div>
// Test for position sticky support (Only Chrome Canary at time of writing)
Modernizr.addTest('csssticky', function(){
var bool;
Modernizr.testStyles("#modernizr { position: -webkit-sticky;position: -moz-sticky;position: -o-sticky; position: -ms-sticky; position: sticky;}", function(elem, rule) {
bool = ((window.getComputedStyle ? getComputedStyle(elem, null) : elem.currentStyle)["position"]).indexOf("sticky") !== -1
});
return bool;
});
(function($) {
$.fn.shimSticky = function() {
var cssPattern = /\s*(.*?)\s*{(.*?)}/g;
var matchPosition = /\.*?position:.*?sticky.*?;/
var getTop = /\.*?top:(.*?);/
var toObserve = [];
var parse = function(css) {
var matches,
css = css.replace(/(\/\*([\s\S]*?)\*\/)|(\/\/(.*)$)/gm, '').replace(/\n|\r/g, '');
while ((matches = cssPattern.exec(css)) !== null){
var selector = matches[1];
if (matchPosition.test(matches[2]) && selector !== "#modernizr"){
var topMatch = getTop.exec(matches[2]),
topCSS = ((topMatch !== null)?parseInt(topMatch[1]) : 0);
$(selector).each(function(){
var elem = $(this),
height = elem.height(),
parent = elem.parent(),
parOff = parent.offset(),
parOffTop = ((parOff !== null && parOff.top !== null)?parOff.top:0),
elmOff = elem.offset(),
elmOffTop = ((elmOff !== null && elmOff.top !== null)?elmOff.top:0),
start = elmOffTop - topCSS,
end = (parOffTop + parent.outerHeight(false)) - height - topCSS,
newCSS = matches[2] + "position:fixed;width:"+elem.width()+"px;height:"+height+"px";
toObserve.push({element : elem, parent : parent, repl : $('<span style="position:static;display:block;height:'+height+'px;"></span>'), start : start, end : end, oldCSS : matches[2], newCSS : newCSS, fixed : false});
});
}
}
}
$('style').each(function() {
$(this).is('link') ? $.get(this.href).success(function(css) { parse(css); }) : parse($(this).text());
});
var $window = $(window);
$window.scroll(function(e){
var scrollTop = $window.scrollTop();
for (var i=0;i<toObserve.length;i++){
var obj = toObserve[i];
if (obj.fixed === false && scrollTop > obj.start && scrollTop < obj.end){
obj.element.attr('style', obj.newCSS);
obj.repl.insertBefore(obj.element);
obj.fixed = true;
}else if (obj.fixed === true){
if (scrollTop < obj.start){
obj.element.attr('style', obj.oldCSS);
obj.fixed = false;
obj.repl.remove();
} else if (scrollTop > obj.end){
var absolute = obj.element.offset();
absolute.position = "absolute"; // Overwrite with absolute;
obj.element.attr('style', obj.newCSS).css(absolute);
obj.fixed = false;
}
}
}
});
};
})(jQuery);
var $shim = $('#shim');
if (!Modernizr.csssticky){
$shim.html("Shim used");
jQuery.fn.shimSticky();
}else{
$shim.html("Shim not used");
}
body{
font-family:Helvetica Neue, sans-serif;
height:3000px;
}
.textHolder{
margin:100px auto;
width:700px;
height:300px;
overflow:hidden;
}
.sticky{
position:-webkit-sticky;
background:rgb(50,50,250);
height:50px;
width:100%;
border-radius:5px;
text-align:center;
line-height:46px;
color:white;
font-size:30px;
top:10px;
}
#shim{
text-align:center;
color:red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment