简体   繁体   English

页面完全加载时的窗口滚动位置

[英]Window scroll position when the page is fully loaded

I would like to know window scroll position when a page is fully loaded. 我想知道页面完全加载后的窗口滚动位置。

When I click on a link with id, page is loaded according to element with this id.When page is loaded, I would like to know window position. 当我单击具有id的链接时,页面将根据具有此id的元素加载。加载页面时,我想知道窗口位置。

index.html 的index.html

<a href="page.html#2012_8">link</a>

page.html page.html中

<script>
$(window).load(function(){
    console.log($(window).scrollTop());
});
</script>

<p id="2012_8">This is some text in a paragraph.</p>

When I click on link, console log get 0. When I refresh page.html, console log get correct window scroll position. 当我单击链接时,控制台日志将获得0。当刷新page.html时,控制台日志将获得正确的窗口滚动位置。 Why? 为什么? Thanks for any help. 谢谢你的帮助。

try using 尝试使用

console.log($(document).scrollTop());

I tested in firebug and it worked fine. 我在萤火虫中进行了测试,效果很好。

The 'load' event is not triggering when you change the window hash. 更改窗口哈希时,不会触发“加载”事件。 To get the event to fire you should hook into the hashchange event. 要触发该事件,您应该加入hashchange事件。

$(window).on('hashchange',function() {
    console.log($(window).scrollTop());
});

$(document).ready(function() {
    $(window).trigger('hashchange');
});

You can use 您可以使用

$(document).ready(function(){
console.log($(document).prop('scrollHeight'));
})

or you can use attr for this 或者你可以使用attr

$(document).ready(function(){
console.log($(document).attr('scrollHeight'));
})

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

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