使用带有固定位置标题的scrollIntoView
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用带有固定位置标题的scrollIntoView相关的知识,希望对你有一定的参考价值。
我有一个标题设置为position: fixed
的网站。在我的一个页面上,我在一个元素上使用scrollIntoView(true)
。我的问题是,当调用scrollIntoView
时,元素位于标题下方。我如何解决这个问题,以便元素显示在标题下方?
我使用的是Bootstrap框架,标题用navbar navbar-fixed-top
设计。
答案
这有点hacky但这是一个解决方法。
var node = 'select your element';
var yourHeight = 'height of your fixed header';
// scroll to your element
node.scrollIntoView(true);
// now account for fixed header
var scrolledY = window.scrollY;
if(scrolledY){
window.scroll(0, scrolledY - yourHeight);
}
另一答案
您可以使用CSS解决此问题,方法是将padding-top
和负margin-top
应用于要滚动到的元素。
Demo
document.querySelector('.scroll-to-working-inline').addEventListener('click', function() {
document.querySelector('.working-inline').scrollIntoView();
});
document.querySelector('.scroll-to-working-block').addEventListener('click', function() {
document.querySelector('.working-block').scrollIntoView();
});
document.querySelector('.scroll-to-broken').addEventListener('click', function() {
document.querySelector('.broken').scrollIntoView();
});
body {
margin: 0;
padding-top: 60px;
}
header {
position: fixed;
top: 0;
background-color: tomato;
color: white;
width: 100%;
height: 60px;
line-height: 60px;
text-align: center;
}
[class^='working'],
[class^='broken'] {
font-size: 3rem;
}
.working-inline {
padding-top: 60px;
margin-top: -60px;
}
.working-block {
padding-top: 60px;
margin-top: -60px;
}
<header>
scroll to...
<button class="scroll-to-working-inline">working inline element</button>
<button class="scroll-to-working-block">working block element</button>
<button class="scroll-to-broken">broken element</button>
</header>
<main>
<p>Sql daemon epoch all your base are belong to us packet system perl semaphore. Interpreter warez pragma kilo worm back door baz continue chown blob unix Dennis Ritchie stack mutex bar throw fopen man pages linux. Sql suitably small values bit infinite loop pwned rm -rf.</p>
<a class="working-inline">Working inline</a>
<p>Syn baz man pages unix vi crack leapfrog semaphore fail pwned afk null socket cd long leet emacs Donald Knuth bin grep todo pragma stdio.h January 1, 1970. Alloc gc system new finally sql stack trace syn mainframe cat machine code memory leak server salt flood tunnel in back door thread. Bytes fatal throw ctl-c Dennis Ritchie over clock eof tera perl regex.</p>
<div class="working-block">Working block element</div>
<p>Public injection class unix malloc error script kiddies packet less fail int I'm sorry Dave, I'm afraid I can't do that. Tarball memory leak double rsa pwned public all your base are belong to us. False bytes bang bar tarball semaphore warez cd port daemon exception mountain dew sql mainframe gcc ifdef chown private.</p>
<div class="broken">Broken element</div>
<p>Daemon bubble sort protected mutex overflow grep snarf crack warez I'm compiling bit if memory leak Starcraft nak script kiddies long it's a feature. Hello world public server James T. Kirk injection terminal wannabee race condition syn alloc. Gobble leapfrog finally bypass concurrently while irc gurfle do back door blob man pages sql over clock.</p>
<p>Char hello world then man pages ascii long salt while char fatal do boolean tunnel in system else foo packet sniffer float terminal int default. Trojan horse ssh ifdef /dev/null chown cache error protocol afk todo rm -rf mainframe piggyback pwned regex xss warez Starcraft try catch stdio.h bubble sort. It's a feature I'm sorry Dave, I'm afraid I can't do that *.* port bypass ip.</p>
<p>Stdio.h epoch mutex flood wannabee do race condition sql access exception. Bar pragma man pages dereference flush todo highjack while buffer bit nak big-endian syn xss salt for d00dz. Leslie Lamport linux server error hexadecimal snarf tunnel in rm -rf firewall then shell all your base are belong to us.</p>
<p>Ascii gcc grep int flood kilo linux access mailbomb hash *.* fork semaphore frack else win bar ssh Leslie Lamport. Man pages strlen cache gnu segfault tarball race condition perl packet sniffer root cookie private chown d00dz January 1, 1970. Rsa public crack bit warez throw for void concurrently ip mutex.</p>
<p>Char hello world then man pages ascii long salt while char fatal do boolean tunnel in system else foo packet sniffer float terminal int default. Trojan horse ssh ifdef /dev/null chown cache error protocol afk todo rm -rf mainframe piggyback pwned regex xss warez Starcraft try catch stdio.h bubble sort. It's a feature I'm sorry Dave, I'm afraid I can't do that *.* port bypass ip.</p>
<p>Stdio.h epoch mutex flood wannabee do race condition sql access exception. Bar pragma man pages dereference flush todo highjack while buffer bit nak big-endian syn xss salt for d00dz. Leslie Lamport linux server error hexadecimal snarf tunnel in rm -rf firewall then shell all your base are belong to us.</p>
<p>Ascii gcc grep int flood kilo linux access mailbomb hash *.* fork semaphore frack else win bar ssh Leslie Lamport. Man pages strlen cache gnu segfault tarball race condition perl packet sniffer root cookie private chown d00dz January 1, 1970. Rsa public crack bit warez throw for void concurrently ip mutex.</p>
<p>Char hello world then man pages ascii long salt while char fatal do boolean tunnel in system else foo packet sniffer float terminal int default. Trojan horse ssh ifdef /dev/null chown cache error protocol afk todo rm -rf mainframe piggyback pwned regex xss warez Starcraft try catch stdio.h bubble sort. It's a feature I'm sorry Dave, I'm afraid I can't do that *.* port bypass ip.</p>
<p>Stdio.h epoch mutex flood wannabee do race condition sql access exception. Bar pragma man pages dereference flush todo highjack while buffer bit nak big-endian syn xss salt for d00dz. Leslie Lamport linux server error hexadecimal snarf tunnel in rm -rf firewall then shell all your base are belong to us.</p>
<p>Ascii gcc grep int flood kilo linux access mailbomb hash *.* fork semaphore frack else win bar ssh Leslie Lamport. Man pages strlen cache gnu segfault tarball race condition perl packet sniffer root cookie private chown d00dz January 1, 1970. Rsa public crack bit warez throw for void concurrently ip mutex.</p>
</main>
另一答案
以下代码生成一个平滑滚动到元素的顶部,并带有固定标题的偏移量:
var topOfElement = document.querySelector('#targetElement').offsetTop - XX;
window.scroll({ top: topOfElement, behavior: "smooth" });
其中XX是固定标题的高度。
另一答案
如果有任何人遇到问题,你的容器div的上边距在scrollIntoView之后被忽略,那么不要将你的元素滚动到视图中,而是相对于其父滚动容器执行scrollTop,如下所示:
var topOfElementToView= $('#elementToScroll').position().top;
$('#parentScrollingContainer').scrollTop(topOfElementToView);
在这个帖子中得到了user113716的回答:How to go to a 以上是关于使用带有固定位置标题的scrollIntoView的主要内容,如果未能解决你的问题,请参考以下文章 window.scrollto 和 scrollIntoView 在 ios 中不能平滑滚动 使用带有位置的背景滑块时,菜单上的悬停消失:固定和 z-index 带有固定元素的 jQuery .animate() 和 css 位置