jQuery 滚动问题

发布于 2024-10-15 17:07:03 字数 935 浏览 6 评论 0原文

我目前正在尝试在我的投资组合网站上实现向上和向下按钮(浮动菜单),以在页面上向下滚动一定量。例如,我单击向下按钮,它会向下滚动 100px,反之亦然。

我目前正在使用此代码在单击按钮时滚动到特定锚点,但我只想修改它以在单击时向上或向下移动一定数量的像素。

$(document).ready(function(){
    $('a[href*=#jump]').click(function() {
        if (location.pathname.replace(/^\//,") == this.pathname.replace(/^\//,") && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});

html 中的当前链接是:返回顶部

,滚动到锚点:

我想有人会立即明白我的意思,但问我是否需要更多详细信息,我可以提供一个我正在尝试的示例来实现。如有帮助,将不胜感激。

I'm currently trying to implement an up and down button (floating menu) on my portfolio website to scroll a certain amount down the page. For example I would click the down button and it would scroll 100px down and vice versa.

I am currently using this code to scroll to an a specific anchor when a button is clicked but I just want to modify it to shift up or down a set amount of pixels when clicked.

$(document).ready(function(){
    $('a[href*=#jump]').click(function() {
        if (location.pathname.replace(/^\//,") == this.pathname.replace(/^\//,") && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});

The current link in html is : <a class="jump" href="#jump">Back To Top</a>

which scrolls to the anchor : <a id="jump" href="#"></a>

I assume someone would know what I mean straight away but ask me if you need more detail and I can provide an example of what I'm trying to achieve. Help would be appreciated.

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

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

发布评论

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

评论(2

勿忘心安 2024-10-22 17:07:03

可以使用jQuery的scrollTop()来调整滚动垂直滚动条。

例如,如果您的向下滚动按钮的 id 为 scrollDownButton,并且您希望将页面向下滚动 100px,则以下代码将执行此操作。

$('#scrollDownButton').click( function() {
    var currPosition = $('body').scrollTop();
    $('body').scrollTop(currPosition + 10);
});

scrollTop() 将为您提供垂直滚动条的当前位置,scrollTop(value) 会将其设置为给定位置。

如果您想向上滚动页面,请从当前值中减去而不是相加。

注意:您想要选择具有滚动条的元素。在上面的示例中,它是 ,但 也是一个常见元素。另外,scrollTop() 已在 JQuery 1.2.6 中实现,请确保您的版本足够新。

更新:如何为滚动设置动画。

如果您想要为滚动设置动画并看到它发生,而不仅仅是向下或向上跳,您可以使用以下代码:

$('#scrollDownButton').click( function() {
    var currPosition = $('body').scrollTop();
    $('body').animate({scrollTop: currPosition+100}, 'slow');
})

Ryan 更新

这是您的特定页面的代码。

$('document').ready( function() {
    $('#scrollDownButton').click( function() {
        var currPosition = $('html').scrollTop();
        $('html').animate({scrollTop: currPosition+100}, 'slow');
    });
});

You can use jQuery's scrollTop() to adjust the scroll vertical scroll bar.

For instance if your button to scroll down has an id of scrollDownButton and you want to scroll down the page by 100px then the following code will do that.

$('#scrollDownButton').click( function() {
    var currPosition = $('body').scrollTop();
    $('body').scrollTop(currPosition + 10);
});

scrollTop() will give you the current position of the vertical scroll bar and scrollTop(value) will set it to the given position.

If you want to scroll up the page then subtract from the current value rather than add.

Note: You want to select whichever element has the scroll bar. In the above example it was the <body>, but <html> is also a common element. Also scrollTop() was implemented in JQuery 1.2.6 make sure your version is new enough.

Update: How to animate the scroll.

If you want to animate the scroll and see it happen rather than just jump down or up you can use the following code:

$('#scrollDownButton').click( function() {
    var currPosition = $('body').scrollTop();
    $('body').animate({scrollTop: currPosition+100}, 'slow');
})

Update for Ryan

Here is the code for your specific page.

$('document').ready( function() {
    $('#scrollDownButton').click( function() {
        var currPosition = $('html').scrollTop();
        $('html').animate({scrollTop: currPosition+100}, 'slow');
    });
});
心作怪 2024-10-22 17:07:03

一种方法是使用 jQuery 的 ScrollTo 并简单地指定偏移量。

jQuery ScrollTo

例如,您可以让它始终滚动到同一元素,但要更改以 100px 为增量的偏移值等。

但我想还有比这更好的方法......

One approach could be to use jQuery's ScrollTo and simply specify an offset amount.

jQuery ScrollTo

For example, you could have it always scroll to the same element, but change the offset value by increments of 100px, ect.

But I'd imagine there is a much better way than this out there...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文