[英]Add minimum height when scrolling to top functionality
I have this JS code which scrolls to the div whenever clicked. 我有此JS代码,只要单击该代码即可滚动到div。 However it shows the DIV at the top.
但是,它在顶部显示DIV。 This creates a problem since I have a fixed header which is 90px in height and the div goes behind it;
这产生了一个问题,因为我有一个固定的标头,高度为90px,并且div位于其后; the title of the DIV is hidden.
DIV的标题被隐藏。
I would like to modify this code so that the DIV doesn't scroll to the very top but leaves a certain height of say 90px. 我想修改此代码,以使DIV不会滚动到最顶部,但会保持一定高度,例如90px。 See the screenshot and notice how it hides behind the fixed header:
查看屏幕截图,并注意它如何隐藏在固定标题的后面:
This is the Jasvascript: 这是Jasvascript:
this.scrollTo = function(eID) {
// This scrolling function
// is from http://www.itnewb.com/tutorial/Creating-the-Smooth-Scroll-Effect-with-JavaScript
var startY = currentYPosition();
var stopY = elmYPosition(eID);
var distance = stopY > startY ? stopY - startY : startY - stopY;
if (distance < 100) {
scrollTo(0, stopY); return;
}
var speed = Math.round(distance / 100);
if (speed >= 20) speed = 20;
var step = Math.round(distance / 25);
var leapY = stopY > startY ? startY + step : startY - step;
var timer = 0;
if (stopY > startY) {
for ( var i=startY; i<stopY; i+=step ) {
setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
leapY += step; if (leapY > stopY) leapY = stopY; timer++;
} return;
}
for ( var i=startY; i>stopY; i-=step ) {
setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
leapY -= step; if (leapY < stopY) leapY = stopY; timer++;
}
function currentYPosition() {
// Firefox, Chrome, Opera, Safari
if (self.pageYOffset) return self.pageYOffset;
// Internet Explorer 6 - standards mode
if (document.documentElement && document.documentElement.scrollTop)
return document.documentElement.scrollTop;
// Internet Explorer 6, 7 and 8
if (document.body.scrollTop) return document.body.scrollTop;
return 0;
}
function elmYPosition(eID) {
var elm = document.getElementById(eID);
var y = elm.offsetTop;
var node = elm;
while (node.offsetParent && node.offsetParent != document.body) {
node = node.offsetParent;
y += node.offsetTop;
} return y;
}
};
You may be able to get away with just modifying your code to change this line: 您可能只需要修改代码即可更改此行:
var stopY = elmYPosition(eID);
to this: 对此:
var stopY = elmYPosition(eID) - 90;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.