Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed header margin #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
:target {
background-color: yellow;
}
.smooth-scroll-header {
position: fixed;
top: 0; left: 0;
width: 100%; height: 60px;
box-shadow: 0 0 5px 0 rgba(0, 0, 0, .7);
background-color: #ffffff;
z-index: 100;
}
</style>
<body>
<script src="smoothscroll.js" type="text/javascript" charset="utf-8"></script>

<header class="smooth-scroll-header"><h5>custom header</h5></header>

<!-- From here, this is just a copy paste of w3.org page -->
<div class=head> <!--begin-logo-->
Expand Down
18 changes: 12 additions & 6 deletions smoothscroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ var position = function(start, end, elapsed, duration) {
// if the first argument is numeric then scroll to this location
// if the callback exist, it is called when the scrolling is finished
// if context is set then scroll that element, else scroll window
var smoothScroll = function(el, duration, callback, context){
// if marginTop is set then scroll will be smaller for this value (helper when site has fixed header/top)
var smoothScroll = function(el, duration, callback, context, marginTop){
duration = duration || 500;
context = context || window;
var start = window.pageYOffset;
Expand All @@ -61,6 +62,11 @@ var smoothScroll = function(el, duration, callback, context){
} else {
var end = getTop(el);
}

if (typeof(marginTop) === 'number') {
end -= marginTop;
}
console.log(marginTop);

var clock = Date.now();
var requestAnimationFrame = window.requestAnimationFrame ||
Expand Down Expand Up @@ -97,11 +103,11 @@ var linkHandler = function(ev) {
// change the scrolling duration in this call
var node = document.getElementById(this.hash.substring(1))
if(!node) return; // Do not scroll to non-existing node

smoothScroll(node, 500, function(el) {
location.replace('#' + el.id)
// this will cause the :target to be activated.
});
var fixedHeader = document.querySelector('.smooth-scroll-header'), headerHeight = 0;
if (fixedHeader) headerHeight = fixedHeader.offsetHeight;

smoothScroll(node, 500, false, false, headerHeight);
}

// We look for all the internal links in the documents and attach the smoothscroll function
Expand Down
2 changes: 1 addition & 1 deletion smoothscroll.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.