0
有没有一种方法可以抵消此滚动功能,以便在滚动到的元素上方停止80px?偏移此滚动元素功能
我有一个固定的标题,它的高度为80px,当我滚动到一个元素时,标题遮住了一些内容。
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');
$('a[href*=#]').each(function() {
var thisPath = filterPath(this.pathname) || locationPath;
if ( locationPath == thisPath
&& (location.hostname == this.hostname || !this.hostname)
&& this.hash.replace(/#/,'')) {
var $target = $(this.hash), target = this.hash;
if (target) {
var targetOffset = $target.offset().top;
$(this).click(function(event) {
event.preventDefault();
$(scrollElem).animate({scrollTop: targetOffset}, 700, function() {
location.hash = target;
});
});
}
}
});
// use the first element that is "scrollable"
function scrollableElement(els) {
for (var i = 0, argLength = arguments.length; i <argLength; i++) {
var el = arguments[i],
$scrollElement = $(el);
if ($scrollElement.scrollTop()> 0) {
return el;
} else {
$scrollElement.scrollTop(1);
var isScrollable = $scrollElement.scrollTop()> 0;
$scrollElement.scrollTop(0);
if (isScrollable) {
return el;
}
}
}
return [];
}
这种方法很有用,它可以在我想要的地方短暂地滚动和停止,但之后它会再次跳转到之前的地方......例如,请参阅www.thisisrobot.co.uk/如果您单击“工作“你会看到我的意思 –
啊,我明白了。这将是因为你的链接'#'标签匹配你正在滚动的div的ID。你提到的跳转是浏览器的默认行为。我会修改我的答案,但可能需要修改div ID才能使用。 – boz
如果我更改了ID,滚动工作将会如何?不要divs需要链接链接到相同的ID? –