Skip to content

Instantly share code, notes, and snippets.

@rlemon

rlemon/upbar.js Secret

Created October 18, 2016 17:47
Show Gist options
  • Save rlemon/b8d37b1d967d8ac1ddaf8699cf48de48 to your computer and use it in GitHub Desktop.
Save rlemon/b8d37b1d967d8ac1ddaf8699cf48de48 to your computer and use it in GitHub Desktop.
upbar
var createCookie = function(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = '; expires=' + date.toGMTString();
}
else var expires = '';
document.cookie = name + '=' + value + expires + '; path=/';
};
var readCookie = function(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
};
var eraseCookie = function(name) {
createCookie(name, '', -1);
};
var topbar = $(document.createElement('div')),
sticky = $(document.createElement('a')),
menu = $(document.getElementById('sidebar-menu')),
chat_body = $(document.getElementById('chat-body')),
top, hidden = false,
stuck = false,
cname = 'sechat_topbar_sticky';
var checkTop = function() {
if ($(window).scrollTop() === 0) {
topbar.stop(true, true).animate({
left: -(topbar.width() - 6)
}, 300);
hidden = true;
return true;
}
return false;
};
var checkHidden = function() {
if (hidden) {
topbar.stop().animate({
left: 0
}, 300);
hidden = false;
}
};
var setPositionTop = function() {
_top = -(topbar.outerHeight() - 25);
topbar.css('top', (stuck ? 0 : _top));
};
stuck = readCookie(cname);
sticky.text((stuck ? 'unstick' : 'stick'));
sticky.css({
'float': 'right',
'margin': '2px 1px 0 0',
'cursor': 'pointer'
});
sticky.bind('click', function() {
stuck = !stuck;
if (stuck) {
$(this).text('unstick');
createCookie(cname, true, 14);
} else {
$(this).text('stick');
eraseCookie(cname);
}
});
menu.append(sticky);
topbar.css({
'position': 'fixed',
'left': '0px',
'z-index': '1',
'width': '49%',
'background-color': '#fff',
'border-bottom': '2px solid #777',
'box-shadow': '0px 5px 10px #777',
'padding': '5px'
});
topbar.append($(document.getElementById('present-users')).detach());
topbar.append(menu.detach());
chat_body.append(topbar);
this.onresize = function() {
setPositionTop();
};
this.onresize();
topbar.hover(function() {
if (!stuck) {
$(this).stop(true, true).animate({
top: 0
}, 300, function() {
checkHidden();
});
}
}, function() {
if (!stuck) {
$(this).stop(false, false).animate({
top: _top
}, 300);
checkTop();
}
});
$(this).scroll(function() {
if (!checkTop()) {
checkHidden();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment