theme: fix sticky header to remove horizontal scrollbar #82

HTML5 solution; sorry IE11
This commit is contained in:
Sören Weber 2021-09-23 21:25:45 +02:00
parent 891ecac371
commit 3224bfb1a3
No known key found for this signature in database
GPG key ID: 07D17FF580AE7589
6 changed files with 59 additions and 339 deletions

View file

@ -22,7 +22,6 @@
<script src="{{"js/clipboard.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script> <script src="{{"js/clipboard.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/perfect-scrollbar.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script> <script src="{{"js/perfect-scrollbar.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/perfect-scrollbar.jquery.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script> <script src="{{"js/perfect-scrollbar.jquery.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/jquery.sticky.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/jquery.svg.pan.zoom.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script> <script src="{{"js/jquery.svg.pan.zoom.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/featherlight.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script> <script src="{{"js/featherlight.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/modernizr.custom-3.6.0.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script> <script src="{{"js/modernizr.custom-3.6.0.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>

View file

@ -42,46 +42,44 @@
<div id="overlay"></div> <div id="overlay"></div>
<div class="padding highlightable"> <div class="padding highlightable">
{{- if not .IsHome }} {{- if not .IsHome }}
<div> <div id="top-bar">
<div id="top-bar"> {{- if and (or .IsPage .IsSection) .Site.Params.editURL }}
{{- if and (or .IsPage .IsSection) .Site.Params.editURL }} {{- $File := .File }}
{{- $File := .File }} {{- $Site := .Site }}
{{- $Site := .Site }} {{- with $File.Path }}
{{- with $File.Path }} <div id="top-github-link">
<div id="top-github-link"> <a class="github-link" title='{{ T "Edit-this-page" }}' href="{{ $Site.Params.editURL }}{{ replace $File.Dir "\\" "/" }}{{ $File.LogicalName }}" target="blank">
<a class="github-link" title='{{ T "Edit-this-page" }}' href="{{ $Site.Params.editURL }}{{ replace $File.Dir "\\" "/" }}{{ $File.LogicalName }}" target="blank"> <i class="fas fa-code-branch"></i>
<i class="fas fa-code-branch"></i> <span id="top-github-link-text">{{T "Edit-this-page"}}</span>
<span id="top-github-link-text">{{T "Edit-this-page"}}</span> </a>
</a> </div>
</div>
{{- end }}
{{- end }} {{- end }}
{{- $toc := (and (not .Params.disableToc) (not .Params.chapter)) }} {{- end }}
<div id="breadcrumbs"> {{- $toc := (and (not .Params.disableToc) (not .Params.chapter)) }}
<span id="sidebar-toggle-span"> <div id="breadcrumbs">
<a href="#" id="sidebar-toggle" data-sidebar-toggle=""> <span id="sidebar-toggle-span">
<i class="fas fa-bars"></i> <a href="#" id="sidebar-toggle" data-sidebar-toggle="">
</a> <i class="fas fa-bars"></i>
</span> </a>
{{- if $toc }} </span>
<span id="toc-menu"><i class="fas fa-list-alt"></i></span>
{{- end }}
{{- $showBreadcrumb := (and (not .Params.disableBreadcrumb) (not .Site.Params.disableBreadcrumb)) }}
{{- if $showBreadcrumb }}
<ol class="links" itemscope itemtype="http://schema.org/BreadcrumbList">
<meta itemprop="itemListOrder" content="Descending" />
{{- template "breadcrumb" dict "page" . "depth" 0 }}
</ol>
{{- else }}
<span class="links">
{{ .Title }}
</span>
{{- end }}
</div>
{{- if $toc }} {{- if $toc }}
{{- partial "toc.html" . }} <span id="toc-menu"><i class="fas fa-list-alt"></i></span>
{{- end }}
{{- $showBreadcrumb := (and (not .Params.disableBreadcrumb) (not .Site.Params.disableBreadcrumb)) }}
{{- if $showBreadcrumb }}
<ol class="links" itemscope itemtype="http://schema.org/BreadcrumbList">
<meta itemprop="itemListOrder" content="Descending" />
{{- template "breadcrumb" dict "page" . "depth" 0 }}
</ol>
{{- else }}
<span class="links">
{{ .Title }}
</span>
{{- end }} {{- end }}
</div> </div>
{{- if $toc }}
{{- partial "toc.html" . }}
{{- end }}
</div> </div>
{{- end }} {{- end }}
<div id="head-tags"> <div id="head-tags">

View file

@ -269,18 +269,21 @@ figcaption h4 {
} }
} }
.is-sticky #top-bar { #top-bar {
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 999;
}
#top-bar.is-sticky {
box-shadow: -1px 2px 5px 1px rgba(0, 0, 0, 0.1); box-shadow: -1px 2px 5px 1px rgba(0, 0, 0, 0.1);
} }
@media print { @media print {
/* the header is sticky which is not suited for print; /* the header is sticky which is not suitable for print; */
because there are inline styles set, we have to use !important here #top-bar{
to remove those styles in print;
chrome will repeat the header otherwise if you've already
scrolled the page before printing */
.is-sticky #top-bar{
position: inherit !important; /* IE11 doesn't know "initial" here */
width: inherit !important;
box-shadow: initial; box-shadow: initial;
position: inherit; /* IE11 doesn't know "initial" here */
} }
} }

View file

@ -881,9 +881,11 @@ td {
} }
.highlightable { .highlightable {
padding: 1rem 0 1rem; padding: 1rem 0 1rem;
overflow: auto;
position: relative; position: relative;
} }
#sidebar .highlightable {
overflow: auto;
}
.lightbox-active #body { .lightbox-active #body {
overflow: visible; overflow: visible;
} }

View file

@ -1,288 +0,0 @@
// Sticky Plugin v1.0.4 for jQuery
// =============
// Author: Anthony Garand
// Improvements by German M. Bravo (Kronuz) and Ruud Kamphuis (ruudk)
// Improvements by Leonardo C. Daronco (daronco)
// Created: 02/14/2011
// Date: 07/20/2015
// Website: http://stickyjs.com/
// Description: Makes an element on the page stick on the screen as you scroll
// It will only set the 'top' and 'position' of your element, you
// might need to adjust the width in some cases.
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var slice = Array.prototype.slice; // save ref to original slice()
var splice = Array.prototype.splice; // save ref to original slice()
var defaults = {
topSpacing: 0,
bottomSpacing: 0,
className: 'is-sticky',
wrapperClassName: 'sticky-wrapper',
center: false,
getWidthFrom: '',
widthFromWrapper: true, // works only when .getWidthFrom is empty
responsiveWidth: false,
zIndex: 'inherit'
},
$window = $(window),
$document = $(document),
sticked = [],
windowHeight = $window.height(),
scroller = function() {
var scrollTop = $window.scrollTop(),
documentHeight = $document.height(),
dwh = documentHeight - windowHeight,
extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
for (var i = 0, l = sticked.length; i < l; i++) {
var s = sticked[i],
elementTop = s.stickyWrapper.offset().top,
etse = elementTop - s.topSpacing - extra;
//update height in case of dynamic content
s.stickyWrapper.css('height', s.stickyElement.outerHeight());
if (scrollTop <= etse) {
if (s.currentTop !== null) {
s.stickyElement
.css({
'width': '',
'position': '',
'top': '',
'z-index': ''
});
s.stickyElement.parent().removeClass(s.className);
s.stickyElement.trigger('sticky-end', [s]);
s.currentTop = null;
}
}
else {
var newTop = documentHeight - s.stickyElement.outerHeight()
- s.topSpacing - s.bottomSpacing - scrollTop - extra;
if (newTop < 0) {
newTop = newTop + s.topSpacing;
} else {
newTop = s.topSpacing;
}
if (s.currentTop !== newTop) {
var newWidth;
if (s.getWidthFrom) {
padding = s.stickyElement.innerWidth() - s.stickyElement.width();
newWidth = $(s.getWidthFrom).width() - padding || null;
} else if (s.widthFromWrapper) {
newWidth = s.stickyWrapper.width();
}
if (newWidth == null) {
newWidth = s.stickyElement.width();
}
s.stickyElement
.css('width', newWidth)
.css('position', 'fixed')
.css('top', newTop)
.css('z-index', s.zIndex);
s.stickyElement.parent().addClass(s.className);
if (s.currentTop === null) {
s.stickyElement.trigger('sticky-start', [s]);
} else {
// sticky is started but it have to be repositioned
s.stickyElement.trigger('sticky-update', [s]);
}
if (s.currentTop === s.topSpacing && s.currentTop > newTop || s.currentTop === null && newTop < s.topSpacing) {
// just reached bottom || just started to stick but bottom is already reached
s.stickyElement.trigger('sticky-bottom-reached', [s]);
} else if(s.currentTop !== null && newTop === s.topSpacing && s.currentTop < newTop) {
// sticky is started && sticked at topSpacing && overflowing from top just finished
s.stickyElement.trigger('sticky-bottom-unreached', [s]);
}
s.currentTop = newTop;
}
// Check if sticky has reached end of container and stop sticking
var stickyWrapperContainer = s.stickyWrapper.parent();
var unstick = (s.stickyElement.offset().top + s.stickyElement.outerHeight() >= stickyWrapperContainer.offset().top + stickyWrapperContainer.outerHeight()) && (s.stickyElement.offset().top <= s.topSpacing);
if( unstick ) {
s.stickyElement
.css('position', 'absolute')
.css('top', '')
.css('bottom', 0)
.css('z-index', '');
} else {
s.stickyElement
.css('position', 'fixed')
.css('top', newTop)
.css('bottom', '')
.css('z-index', s.zIndex);
}
}
}
},
resizer = function() {
windowHeight = $window.height();
for (var i = 0, l = sticked.length; i < l; i++) {
var s = sticked[i];
var newWidth = null;
if (s.getWidthFrom) {
if (s.responsiveWidth) {
newWidth = $(s.getWidthFrom).width();
}
} else if(s.widthFromWrapper) {
newWidth = s.stickyWrapper.width();
}
if (newWidth != null) {
s.stickyElement.css('width', newWidth);
}
}
},
methods = {
init: function(options) {
return this.each(function() {
var o = $.extend({}, defaults, options);
var stickyElement = $(this);
var stickyId = stickyElement.attr('id');
var wrapperId = stickyId ? stickyId + '-' + defaults.wrapperClassName : defaults.wrapperClassName;
var wrapper = $('<div></div>')
.attr('id', wrapperId)
.addClass(o.wrapperClassName);
stickyElement.wrapAll(function() {
if ($(this).parent("#" + wrapperId).length == 0) {
return wrapper;
}
});
var stickyWrapper = stickyElement.parent();
if (o.center) {
stickyWrapper.css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"});
}
if (stickyElement.css("float") === "right") {
stickyElement.css({"float":"none"}).parent().css({"float":"right"});
}
o.stickyElement = stickyElement;
o.stickyWrapper = stickyWrapper;
o.currentTop = null;
sticked.push(o);
methods.setWrapperHeight(this);
methods.setupChangeListeners(this);
});
},
setWrapperHeight: function(stickyElement) {
var element = $(stickyElement);
var stickyWrapper = element.parent();
if (stickyWrapper) {
stickyWrapper.css('height', element.outerHeight());
}
},
setupChangeListeners: function(stickyElement) {
if (window.MutationObserver) {
var mutationObserver = new window.MutationObserver(function(mutations) {
if (mutations[0].addedNodes.length || mutations[0].removedNodes.length) {
methods.setWrapperHeight(stickyElement);
}
});
mutationObserver.observe(stickyElement, {subtree: true, childList: true});
} else {
if (window.addEventListener) {
stickyElement.addEventListener('DOMNodeInserted', function() {
methods.setWrapperHeight(stickyElement);
}, false);
stickyElement.addEventListener('DOMNodeRemoved', function() {
methods.setWrapperHeight(stickyElement);
}, false);
} else if (window.attachEvent) {
stickyElement.attachEvent('onDOMNodeInserted', function() {
methods.setWrapperHeight(stickyElement);
});
stickyElement.attachEvent('onDOMNodeRemoved', function() {
methods.setWrapperHeight(stickyElement);
});
}
}
},
update: scroller,
unstick: function(options) {
return this.each(function() {
var that = this;
var unstickyElement = $(that);
var removeIdx = -1;
var i = sticked.length;
while (i-- > 0) {
if (sticked[i].stickyElement.get(0) === that) {
splice.call(sticked,i,1);
removeIdx = i;
}
}
if(removeIdx !== -1) {
unstickyElement.unwrap();
unstickyElement
.css({
'width': '',
'position': '',
'top': '',
'float': '',
'z-index': ''
})
;
}
});
}
};
// should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
if (window.addEventListener) {
window.addEventListener('scroll', scroller, false);
window.addEventListener('resize', resizer, false);
} else if (window.attachEvent) {
window.attachEvent('onscroll', scroller);
window.attachEvent('onresize', resizer);
}
$.fn.sticky = function(method) {
if (methods[method]) {
return methods[method].apply(this, slice.call(arguments, 1));
} else if (typeof method === 'object' || !method ) {
return methods.init.apply( this, arguments );
} else {
$.error('Method ' + method + ' does not exist on jQuery.sticky');
}
};
$.fn.unstick = function(method) {
if (methods[method]) {
return methods[method].apply(this, slice.call(arguments, 1));
} else if (typeof method === 'object' || !method ) {
return methods.unstick.apply( this, arguments );
} else {
$.error('Method ' + method + ' does not exist on jQuery.sticky');
}
};
$(function() {
setTimeout(scroller, 0);
});
}));

View file

@ -188,7 +188,16 @@ images.each(function(index){
$(window).resize(function() { $(window).resize(function() {
setMenuHeight(); setMenuHeight();
}); });
// for the sticky header
$(window).scroll(function() {
// add shadow when not in top position
if ($(this).scrollTop() == 0) {
$('#top-bar').removeClass("is-sticky");
}
else {
$('#top-bar').addClass("is-sticky");
}
});
// debouncing function from John Hann // debouncing function from John Hann
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
(function($, sr) { (function($, sr) {
@ -495,9 +504,6 @@ jQuery(function() {
$(document).ready($.proxy(anchorScrolls, 'init')); $(document).ready($.proxy(anchorScrolls, 'init'));
})(window.document, window.history, window.location); })(window.document, window.history, window.location);
// Stick the top to the top of the screen when scrolling
jQuery("#top-bar").sticky({topSpacing:0, zIndex: 1000});
// Add link button for every // Add link button for every
var text, clip = new ClipboardJS('.anchor'); var text, clip = new ClipboardJS('.anchor');
$("h1~h2,h1~h3,h1~h4,h1~h5,h1~h6").append(function(index, html){ $("h1~h2,h1~h3,h1~h4,h1~h5,h1~h6").append(function(index, html){