如何使用 jquery 滚动到页面上的特定位置?

发布于 2024-08-08 08:04:42 字数 149 浏览 8 评论 0 原文

是否可以使用 jQuery 滚动到页面上的特定位置?

我想要滚动的位置是否必须具有:

<a name="#123">here</a>

或者它可以移动到特定的 DOM id 吗?

Is it possible to scroll to a specific location on the page using jQuery?

Does the location I want to scroll to have to have:

<a name="#123">here</a>

Or can it just move to a specific DOM id?

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

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

发布评论

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

评论(11

夜光 2024-08-15 08:04:43

jQuery Scroll Plugin

因为这是一个用 jquery 标记的问题,我不得不说,这个库有一个非常好的平滑滚动插件,你可以在这里找到它:
http://plugins.jquery.com/scrollTo/

文档摘录:

$('div.pane').scrollTo(...);//all divs w/class pane

$.scrollTo(...);//the plugin will take care of this

自定义 jQuery滚动函数

您可以通过定义自定义滚动 jquery 函数来使用非常轻量级方法

$.fn.scrollView = function () {
    return this.each(function () {
        $('html, body').animate({
            scrollTop: $(this).offset().top
        }, 1000);
    });
}

,并像这样使用它:

$('#your-div').scrollView();

滚动到页面坐标

Animate html具有 scrollTopscrollLeft 属性的 body 元素

$('html, body').animate({
    scrollTop: 0,
    scrollLeft: 300
}, 1000);

Plain javascript

使用 window.scroll 滚动

window.scroll(horizontalOffset, verticalOffset);

只是总结一下,使用 window.location.hash 跳转到 ID 为

window.location.hash = '#your-page-element';

直接在 HTML 中的元素(辅助功能增强)

<a href="#your-page-element">Jump to ID</a>

<div id="your-page-element">
    will jump here
</div>

jQuery Scroll Plugin

since this is a question tagged with jquery i have to say, that this library has a very nice plugin for smooth scrolling, you can find it here:
http://plugins.jquery.com/scrollTo/

Excerpts from Documentation:

$('div.pane').scrollTo(...);//all divs w/class pane

or

$.scrollTo(...);//the plugin will take care of this

Custom jQuery function for scrolling

you can use a very lightweight approach by defining your custom scroll jquery function

$.fn.scrollView = function () {
    return this.each(function () {
        $('html, body').animate({
            scrollTop: $(this).offset().top
        }, 1000);
    });
}

and use it like:

$('#your-div').scrollView();

Scroll to a page coordinates

Animate html and body elements with scrollTop or scrollLeft attributes

$('html, body').animate({
    scrollTop: 0,
    scrollLeft: 300
}, 1000);

Plain javascript

scrolling with window.scroll

window.scroll(horizontalOffset, verticalOffset);

only to sum up, use the window.location.hash to jump to element with ID

window.location.hash = '#your-page-element';

Directly in HTML (accesibility enhancements)

<a href="#your-page-element">Jump to ID</a>

<div id="your-page-element">
    will jump here
</div>
心奴独伤 2024-08-15 08:04:43

是的,即使使用纯 JavaScript,这也很容易。您给一个元素一个 id,然后您可以将其用作“书签”:

<div id="here">here</div>

如果您希望它在用户单击链接时滚动到那里,您可以使用经过验证的方法:

<a href="#here">scroll to over there</a>

要以编程方式执行此操作,请使用scrollIntoView()

document.getElementById("here").scrollIntoView()

Yep, even in plain JavaScript it's pretty easy. You give an element an id and then you can use that as a "bookmark":

<div id="here">here</div>

If you want it to scroll there when a user clicks a link, you can just use the tried-and-true method:

<a href="#here">scroll to over there</a>

To do it programmatically, use scrollIntoView()

document.getElementById("here").scrollIntoView()
梦明 2024-08-15 08:04:43

不需要使用任何插件,你可以这样做:

var divPosition = $('#divId').offset();

然后使用它来滚动文档到特定的DOM:

$('html, body').animate({scrollTop: divPosition.top}, "slow");

There is no need to use any plugin, you can do it like this:

var divPosition = $('#divId').offset();

then use this to scroll document to specific DOM:

$('html, body').animate({scrollTop: divPosition.top}, "slow");
浪漫之都 2024-08-15 08:04:43

这是一个纯 JavaScript 版本:

location.hash = '#123';

它会自动滚动。
记得添加“#”前缀。

Here's a pure javascript version:

location.hash = '#123';

It'll scroll automatically.
Remember to add the "#" prefix.

鹊巢 2024-08-15 08:04:43

简单的 JavaScript:

window.location = "#elementId"

Plain Javascript:

window.location = "#elementId"
荆棘i 2024-08-15 08:04:43
<div id="idVal">
    <!--div content goes here-->
</div>
...
<script type="text/javascript">
     $(document).ready(function(){
         var divLoc = $('#idVal').offset();
         $('html, body').animate({scrollTop: divLoc.top}, "slow");
     });
</script>

此示例显示如何定位到特定的 div id,即本例中的“idVal”。如果您有后续的 div/表格将通过 ajax 在此页面中打开,那么您可以分配唯一的 div 并调用脚本滚动到 div 的每个内容的特定位置。

希望这会有用。

<div id="idVal">
    <!--div content goes here-->
</div>
...
<script type="text/javascript">
     $(document).ready(function(){
         var divLoc = $('#idVal').offset();
         $('html, body').animate({scrollTop: divLoc.top}, "slow");
     });
</script>

This example shows to locate to a particular div id i.e, 'idVal' in this case. If you have subsequent divs/tables that will open up in this page via ajax, then you can assign unique divs and call the script to scroll to the particular location for each contents of divs.

Hope this will be useful.

诗化ㄋ丶相逢 2024-08-15 08:04:43
<script type="text/javascript">
    $(document).ready(function(){
        $(".scroll-element").click(function(){
            $('html,body').animate({
                scrollTop: $('.our_companies').offset().top
            }, 1000);

            return false;
        });
    })
</script>

<script type="text/javascript">
    $(document).ready(function(){
        $(".scroll-element").click(function(){
            $('html,body').animate({
                scrollTop: $('.our_companies').offset().top
            }, 1000);

            return false;
        });
    })
</script>

蓝颜夕 2024-08-15 08:04:43

试试这个

<div id="divRegister"></div>

$(document).ready(function() {
location.hash = "divRegister";
});

Try this

<div id="divRegister"></div>

$(document).ready(function() {
location.hash = "divRegister";
});
过期以后 2024-08-15 08:04:43

这是 @juraj-blahunka 的轻量级方法的变体。此函数不假设容器是文档,并且仅在项目不在视图范围内时滚动。动画排队也被禁用以避免不必要的弹跳。

$.fn.scrollToView = function () {
    return $.each(this, function () {
        if ($(this).position().top < 0 ||
            $(this).position().top + $(this).height() > $(this).parent().height()) {
            $(this).parent().animate({
                scrollTop: $(this).parent().scrollTop() + $(this).position().top
            }, {
                duration: 300,
                queue: false
            });
        }
    });
};

Here is variant of @juraj-blahunka's lightweight approach. This function does not assume the container is the document and only scrolls if the item is out of view. Animation queuing is also disabled to avoid unnecessary bouncing.

$.fn.scrollToView = function () {
    return $.each(this, function () {
        if ($(this).position().top < 0 ||
            $(this).position().top + $(this).height() > $(this).parent().height()) {
            $(this).parent().animate({
                scrollTop: $(this).parent().scrollTop() + $(this).position().top
            }, {
                duration: 300,
                queue: false
            });
        }
    });
};
红颜悴 2024-08-15 08:04:43

使用jquery.easing.min.js,修复IE控制台错误

Html

<a class="page-scroll" href="#features">Features</a>
<section id="features" class="features-section">Features Section</section>


        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="js/jquery.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>

        <!-- Scrolling Nav JavaScript -->
        <script src="js/jquery.easing.min.js"></script>

Jquery

//jQuery to collapse the navbar on scroll, you can use this code with in external file with name scrolling-nav.js
        $(window).scroll(function () {
            if ($(".navbar").offset().top > 50) {
                $(".navbar-fixed-top").addClass("top-nav-collapse");
            } else {
                $(".navbar-fixed-top").removeClass("top-nav-collapse");
            }
        });
        //jQuery for page scrolling feature - requires jQuery Easing plugin
        $(function () {
            $('a.page-scroll').bind('click', function (event) {
                var anchor = $(this);
                if ($(anchor).length > 0) {
                    var href = $(anchor).attr('href');
                    if ($(href.substring(href.indexOf('#'))).length > 0) {
                        $('html, body').stop().animate({
                            scrollTop: $(href.substring(href.indexOf('#'))).offset().top
                        }, 1500, 'easeInOutExpo');
                    }
                    else {
                        window.location = href;
                    }
                }
                event.preventDefault();
            });
        });

Using jquery.easing.min.js, With fixed IE console Errors

Html

<a class="page-scroll" href="#features">Features</a>
<section id="features" class="features-section">Features Section</section>


        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="js/jquery.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>

        <!-- Scrolling Nav JavaScript -->
        <script src="js/jquery.easing.min.js"></script>

Jquery

//jQuery to collapse the navbar on scroll, you can use this code with in external file with name scrolling-nav.js
        $(window).scroll(function () {
            if ($(".navbar").offset().top > 50) {
                $(".navbar-fixed-top").addClass("top-nav-collapse");
            } else {
                $(".navbar-fixed-top").removeClass("top-nav-collapse");
            }
        });
        //jQuery for page scrolling feature - requires jQuery Easing plugin
        $(function () {
            $('a.page-scroll').bind('click', function (event) {
                var anchor = $(this);
                if ($(anchor).length > 0) {
                    var href = $(anchor).attr('href');
                    if ($(href.substring(href.indexOf('#'))).length > 0) {
                        $('html, body').stop().animate({
                            scrollTop: $(href.substring(href.indexOf('#'))).offset().top
                        }, 1500, 'easeInOutExpo');
                    }
                    else {
                        window.location = href;
                    }
                }
                event.preventDefault();
            });
        });
一紙繁鸢 2024-08-15 08:04:43

您可以使用 scroll-behavior: smooth; 来完成此操作,无需 Javascript

https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior

You can use scroll-behavior: smooth; to get this done without Javascript

https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior

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