Skip to content

Instantly share code, notes, and snippets.

@arkadylukashov
Created October 29, 2012 02:39
Show Gist options
  • Save arkadylukashov/3971141 to your computer and use it in GitHub Desktop.
Save arkadylukashov/3971141 to your computer and use it in GitHub Desktop.
upPanelScroll
(function($){
$.fn.upPanel = function(options) {
var
settings = $.extend({
fade : false, // int, etc;
speed : 300, // up scroll speed
waterline : 600, // 'auto', 'formula'
leftPosition : false, // { nearElement: 'selector', space: 10 }
onShow : $.noop,
onHide : $.noop
}, options),
$panel = $(this),
methods = {
init : function() {
// base css for panel
$panel.hide().css({position: 'fixed'})
// smart waterline
if (typeof settings.waterline == 'string' && /auto/.test()) {
methods.helpers.waterLine.formula = $.trim('auto 1/3'.replace('auto', ''));
methods.helpers.waterLine.isCalc = true;
methods.helpers.waterLine.value = methods.helpers.waterLine.calculate();
}
},
scrollHandler : function() {
if ( $(window).scrollTop() > settings.waterLine ) {
if ( ! $panel.is('visible') ) {
methods.helpers.panelToggle('show')
if ( settings.leftPosition !== false ) {
methods.helpers.calculateLeft();
}
}
} else {
methods.helpers.panelToggle('hide')
}
},
resizeHandler : function() {
if ( settings.leftPosition !== false ) {
methods.helpers.calculateLeft();
}
},
upstairHandler : function() {
var mode = window.opera ? 'html' : 'html, body';
$(mode).animate({scrollTop: 0}, settings.speed, settings.onComplete)
},
helpers : {
panelToggle : function(mode) {
var mthd = mode == 'show' ? 'fadeIn' : 'fadeOut',
clbck = mode == 'show' ? 'onShow' : 'onHide';
$panel[mthd]( (!settings.fade ? 1 : settings.fade) , function() { settings[clbck].call($panel) } )
},
calculateLeft : function() {
var b = $(settings.leftPosition.near).offset().left - settings.leftPosition.margin,
w = $panel.outerWidth();
$panel.css({left: (b - w)})
},
waterLine : {
formula : '',
value : 0,
isCalc : false,
calculate : function() {
}
}
}
};
methods.init();
$(window)
.scroll(methods.scrollHandler)
.resize(methods.resizeHandler);
$panel
.on('click', methods.upstairHandler)
return this;
}
})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment