可以't使用Javascript强制Chrome滚动到页面顶部
Can't force Chrome to Scroll to top of page with Javascript
我已经完成了我的网站的开发,我正在测试它。1秒后我运行一个脚本来检查页眉/顶部的height
,并在内容部分添加一个margin-top
。但是该标题/顶部根据滚动位置(在运行时)而改变大小。因此,首先我必须确保页面滚动设置为(顶部)0,然后运行此脚本。有时用户向下滚动并刷新,浏览器的自然行为是将页面滚动到原来的位置。
在Google Chrome上尝试了几次,并对页面加载时触发的大部分代码进行了评论后,我在Firefox上也尝试了同样的操作,结果成功了!所以这个代码在Chrome上不起作用:
$(document).scrollTop(0);
// or this
$(document).ready(function(){ $(this).scrollTop(0); });
我使用了animate
函数,并向alert
传递了一个回调"Hello",它在顶部发出警报,然后我单击"Ok",它会回到刷新前的滚动位置。
我也尝试过不使用jQuery,但什么都没发生
window.scrollTo(0,0);
在Firefox上运行良好。除了scrollTop
之外,没有Javascirpt代码在运行(也许是社交插件之类的东西,但我的什么都没有)。
有人知道变通办法或如何解决吗?Chrome怎么了?
我使用的是Chrome 41,它可以工作:
$("html, body").animate({
scrollTop: 0
}, 9000);
另一个潜在的解决方案:
window.setTimeout(function() {
window.scrollTo(0,0);
}, 0);
在多次刷新后,我确实对上面的代码有间歇性的问题,直到我删除了对谷歌托管的jQuery库的引用。这可能只是一种措施,以减少滥用不断刷新。
这里有很多好的建议:同样的老问题,.rollTop(0)在Chrome中不起作用;Safari
Try these solution. I hope this will be helpful
$(document).scroll(function() {
if ($(this).scrollTop() > 100) {
if(window.innerWidth <= 768){
$("html, body").css({
"position" : 'fixed',
"top" : 0 , "margin-left" : 566});
}else{
$("html, body").css({
"position" : 'fixed',
"top" : 0 , "margin-left" : 721});
}
} else {
$("html, body").css({ "position" : 'relative', "margin-left" : 0});
}
})
在jQuery中:
只需在文档加载的开头添加这一行(假设您在HTML页面上解析了jQuery框架):
$(document).ready(function)() {
$(document.body).animate({scrollTop: '0px'}, 1000);// 1sec animation scroll up
//Your rest of the code goes below -
});
相关文章:
- 在firefox和chrome中的左侧显示iframe滚动条
- 滚动在Chrome中有效,但在Firefox或IE中无效
- 可以't使用Javascript强制Chrome滚动到页面顶部
- 向下滚动时隐藏菜单,向上滚动时显示,适用于Chrome,不适用于Safari(手机)
- Chrome的滚动优化
- 如何在HTML / CSS中填充垂直和滚动中间窗格(Chrome可以工作,但不能Firefox)
- 滚动到顶部按钮在 chrome 或 safari 中不起作用
- 使用 nameProp 的 JQuery 图像滚动更新在 Firefox 和 Chrome 中不起作用
- Chrome中无法进行水平滚动
- Chrome标签或书签滚动会影响angular应用程序
- 移动Chrome和Safari之间的页面滚动不一致
- JavaScript滚动图片库在Chrome或IE中不起作用
- 当用户向下滚动我的网页时,如何删除chrome地址栏周围的边框
- 将IFrame滚动到顶部在Chrome中不起作用
- jQuery fadeOut / fadeIn Chrome 中的滚动问题
- 为什么使用 chrome 从本网站向上/向下滚动很慢
- 无限滚动仅在Chrome中工作
- 在 Chrome 中滚动到视图
- 在 iOS Chrome 中获取连续滚动事件
- 导航栏在移动上折叠,滚动chrome/safari