使用百分比 % 滚动到顶部 .offset()

发布于 2024-11-25 00:56:56 字数 631 浏览 1 评论 0原文

我只是为滚动页面编写了这个,它工作正常并且做了它应该做的事情..

$('#nav a').click(function(){

var sid = $(this).attr('id');

$('html,body').animate({
 scrollTop: $('#'+ sid +'-content').offset().top - 200}, 1000);
  return false;
});

..但我希望偏移量是按 % 而不是 px 计算的

,即,而不是

top - 200 

它可能是

top - 30%

如何实现这一点的任何想法?

一如既往,我们感谢任何帮助,并提前致谢。

快速编辑:

当前的 3 个答案(谢谢)似乎每次都会成倍增加,这不是我想要的,我希望每个答案都有 30% 窗口高度的恒定间隙所以每次 #id-content 都会滚动到顶部,并与我拥有的固定定位侧边栏对齐。

我当前的代码留下了 200px 的间隙,但这会导致不同显示器/浏览器尺寸的问题,而用 % 可以解决这个问题。

I just wrote this for scrolling pages which is working fine and does what it should..

$('#nav a').click(function(){

var sid = $(this).attr('id');

$('html,body').animate({
 scrollTop: $('#'+ sid +'-content').offset().top - 200}, 1000);
  return false;
});

..but I want the offset to be calculated by % rather then px

ie rather then

top - 200 

it could be

top - 30%

any ideas how to accomplish this?

As always any help is appreciated and thanks in advance.

Quick Edit:

The current 3 answers (thank you) seem to multiply each time which is not what I want, I wish to have a constant gap of 30% window height each time so each time the #id-content is scrolled to the top lines up with a fixed positioned sidebar I have.

My current code leaves a 200px gap but that causes an issue with different monitor/browser sizes where as a % would sort it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝梦月影 2024-12-02 00:56:57

以下命令将始终将框放置在距顶部 60% 的位置:

var offset = parseInt($('#example').offset().top);
var bheight = $(window).height();
var percent = 0.6;
var hpercent = bheight * percent;
$('html,body').animate({scrollTop: offset - hpercent}, 1000);

The following will position the box always 60% from the top:

var offset = parseInt($('#example').offset().top);
var bheight = $(window).height();
var percent = 0.6;
var hpercent = bheight * percent;
$('html,body').animate({scrollTop: offset - hpercent}, 1000);
仅此而已 2024-12-02 00:56:57

您可以计算 30% 的偏移量并使用它:

$('#nav a').click(function(){

    var sid = $(this).attr('id');
    var offset = $('#'+ sid +'-content').offset().top;

    $('html,body').animate({scrollTop: offset - (offset * 0.3)}, 1000);
    return false;
});

这是一个示例小提琴,显示了此操作的实际情况。

You could calculate 30% of the offset and use that:

$('#nav a').click(function(){

    var sid = $(this).attr('id');
    var offset = $('#'+ sid +'-content').offset().top;

    $('html,body').animate({scrollTop: offset - (offset * 0.3)}, 1000);
    return false;
});

Here's an example fiddle showing this in action.

走过海棠暮 2024-12-02 00:56:57

我知道这已经有很多年了,OP现在很可能已经找到了解决方案,但对于任何遇到这个问题的人来说,这是我的简单解决方案。

  var navBarHeight = $('#navBar').height();
            $('html, body').animate({
                scrollTop: $("#scrollDestination").offset().top-navbarheight
            }, 1000);

I know this is years old and the OP has most likely found a solution by now, but here's my simple solution for anyone who comes across this anyway..

  var navBarHeight = $('#navBar').height();
            $('html, body').animate({
                scrollTop: $("#scrollDestination").offset().top-navbarheight
            }, 1000);
烟柳画桥 2024-12-02 00:56:57

不确定你到底想要什么 30%,但你能做的就是将你想要的百分比乘以 0.3,然后从顶部减去它。

如果它是顶部偏移量本身,您需要 30%:

$('html,body').animate({
    scrollTop: $('#'+ sid +'-content').offset().top - $('#'+ sid +'-content').offset().top * 0.3 }, 1000);
    return false;
});

当然这看起来有点愚蠢,因为它相当于 $('#'+ sid +'-content').offset().top * 0.7< /code>,但您可以将其替换为您想要的任何内容。

Not sure exactly what you want 30% of, but what you can do is just multiply what you want a percentage of by 0.3, and then subtract that from top.

If it's the top offset itself you want 30% of:

$('html,body').animate({
    scrollTop: $('#'+ sid +'-content').offset().top - $('#'+ sid +'-content').offset().top * 0.3 }, 1000);
    return false;
});

Of course that seems a bit silly, since it's just equivalent to $('#'+ sid +'-content').offset().top * 0.7, but you can replace it with whatever you want.

半世晨晓 2024-12-02 00:56:57

试试这个

$('#nav a').click(function(){

var sid = $(this).attr('id');
var contentTop = $('#'+ sid +'-content').offset().top;
contentTop = parseInt(contentTop - (contentTop *0.3)); 
$('html,body').animate({ scrollTop: contentTop }, 1000);
  return false;
});

Try this

$('#nav a').click(function(){

var sid = $(this).attr('id');
var contentTop = $('#'+ sid +'-content').offset().top;
contentTop = parseInt(contentTop - (contentTop *0.3)); 
$('html,body').animate({ scrollTop: contentTop }, 1000);
  return false;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文