【问题标题】:how to Scroll to top of page in Mobile Devices [closed]如何在移动设备中滚动到页面顶部[关闭]
【发布时间】:2016-05-06 11:46:54
【问题描述】:

我在 onLoad 使用下面的代码转到页面顶部。 Javascript 中的 window.scrollTo(0,0) 它适用于浏览器,但不适用于任何移动设备。

【问题讨论】:

  • 下面的代码?粘贴您的代码

标签: javascript jquery html css angularjs


【解决方案1】:

关于移动页面的“window.scrollTo(0,1) 修复”的文章不少。使用幻数超时可能会让用户感到沮丧。内容开始出现,用户开始向下滚动页面并使用信息,然后重新回到页面顶部。更好的方法是这样的:

window.addEventListener("load", function() {
    setTimeout(function() {
        var scrollPos = window.pageYOffset || 
            document.documentElement.scrollTop || 
            document.body.scrollTop;

        // seems like the author of linked code source had a logic bug here
        // if you need to apply this, probably you will want to check scrollPos > 1

        // I keep the comment above - although it is wrong. You should 
        // not scroll, once the user already interacted with the page.
        // For further information, see the linked article below.
        if (scrollPos < 1) {
            window.scrollTo(0,1);
        }
    }, 0);
});

更多信息 - here is one article(脚本来自那里)

【讨论】:

  • window.scrollTo(0,1);即使您放置了 addEventListner/setTimeOut,也不适用于移动设备。而不是这个,我使用角度功能滚动到 ID,即锚滚动。下面的代码对我有用。 ..... 注入 $location,$anchorScroll,$timeout,$location 依赖项来工作。 .... $timeout(function () { $location.hash("在此处输入 id 名称以滚动到"); $anchorScroll(); });
  • @pocketrocket 如果您阅读了相关文章,则需要
猜你喜欢
  • 2023-01-02
  • 1970-01-01
  • 2022-12-09
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
相关资源
最近更新 更多