简体   繁体   English

我如何滚动到#target-100px?

[英]how can i scroll to #target - 100px?

I'm trying to use 我正在尝试使用

<a href="#target">Details</a>

<div id="target">asd...</div>

but I also have top popup menu. 但我也有顶部弹出菜单。 So it's getting behind that menu. 所以它落后于该菜单。

Is it possible to write a JavaScript (also I'm already using bootstrap and jquery libraries on my template) go to #target - 100px 是否可以编写JavaScript(我已经在模板上使用了bootstrap和jquery库)转到#target - 100px

Thanks in advance 提前致谢

Never tried, but something like this should work. 从未尝试过,但是类似的事情应该起作用。

 $('html, body').animate({
     scrollTop: $("#elementtoScrollToID").offset().top - 100
 }, 1000); // this is the time in millisecond of the animation

A simple JavaScript only solution is: 一个简单的仅JavaScript解决方案是:

window.scrollTo(x,y)

For your example you would use: 对于您的示例,您将使用:

window.scrollTo(0,100)

Step 1: Include Ariel Flesler's jquery.scrollTo-min.js 步骤1:加入Ariel Flesler的jquery.scrollTo-min.js

http://flesler.com/jquery/scrollTo/js/jquery.scrollTo-min.js http://flesler.com/jquery/scrollTo/js/jquery.scrollTo-min.js

Step2: Add 步骤2:新增

$('a').scrollTo( $('#target'), 1000, {offset:-100} );
 scrollTop: ($($anchor.attr('href')).offset().top)-300

This is my code and it's works. 这是我的代码,可以正常工作。 -300 = pixels -300 =像素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM