jquery页面滚动到顶部_如何使用jQuery / JavaScript动态滚动到页面顶部或底部

jquery页面滚动到顶部

JavaScript Icon

JavaScript can be used to manipulate HTML elements and create dynamic effects without having to refresh the page. JavaScript was originally developed by an employee for Netscape (that developed and made available the Netscape Communicator web browser and Internet suite) – with JavaScript originally named LiveScript, but was later renamed to JavaScript.

JavaScript可用于处理HTML元素和创建动态效果,而无需刷新页面。 JavaScript最初是由Netscape的一名员工开发的(该公司开发并提供了Netscape Communicator Web浏览器和Internet套件)– JavaScript最初名为LiveScript ,但后来更名为JavaScript。

jQuery Logo
()

如何使用JavaScript动态滚动到窗口的顶部或底部? (How do I dynamically scroll to the top or bottom of the window using JavaScript?)

In the code example below,  I am scrolling to the top of a specific HTML element that makes use of a specific ID attribute value, but 40 pixels above this specific HTML element. However, I am making use of the jQuery JavaScript library for ease of use and simplicity. You can download and use jQuery free. The code:

在下面的代码示例中,我将滚动到使用特定ID属性值但在此特定HTML元素上方40像素的特定HTML元素的顶部。 但是,为了便于使用和简化,我正在使用jQuery JavaScript库。 您可以免费下载和使用jQuery 。 代码:

$(‘html, body’).animate({scrollTop: jQuery(‘#register’).position().top – 40}, ‘slow’);

$('html,body')。animate({scrollTop:jQuery('#register')。position()。top – 40},'slow');

You can change the #register value to whichever HTML element or ID that you want to scroll to. If you look at the code above, you’ll see the portion “.top – 40”, this is because the position.top returns an integer value and by subtracting an extra 40 pixels, there will be some extra space when the “scrolling” is invoked – so it will stop at 40 pixels above the HTML element that uses the “register” ID attribute value.

您可以将#register值更改为想要滚动到HTML元素或ID。 如果您看上面的代码,则会看到“ .top – 40”部分 ,这是因为position.top返回一个整数值,并减去40像素后,“滚动”被调用–因此它将停止在使用“注册” ID属性值HTML元素上方 40像素

如何使用jQuery? (How do I make use of jQuery?)

In order to make use of the jQuery library, you need to include the library itself within the <head> section of your HTML document. As well as that, you need to make sure that when you are making use of the library the HTML DOM is fully loaded before executing the JavaScript code making use of the jQuery library, so everything is ready prior to the execution of the JavaScript code. The full code below:

为了使用jQuery库,您需要将库本身包含在HTML文档的<head>部分中。 除此之外,还需要确保在使用库时,在使用jQuery库执行JavaScript代码之前,HTML DOM已完全加载,因此在执行JavaScript代码之前,一切都已准备就绪。 完整代码如下:

$(document).ready(function() { $(‘html, body’).animate({scrollTop: jQuery(‘#register’).position().top – 40}, ‘slow’); });

$(document).ready(function(){$('html,body')。animate({scrollTop:jQuery('#register')。position()。top – 40},'slow');});

翻译自: https://www.eukhost.com/blog/webhosting/how-to-dynamically-scroll-to-the-top-or-bottom-of-the-page-using-jquery-javascript/

jquery页面滚动到顶部

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值