【问题标题】:How to scroll browser to desired element by javascript?如何通过javascript将浏览器滚动到所需的元素?
【发布时间】:2012-08-09 17:45:07
【问题描述】:

问题很简单。

如何通过 javascript 将浏览器滚动到所需元素或所需位置?

非常感谢您的帮助!

【问题讨论】:

    标签: javascript html browser


    【解决方案1】:

    到一个元素:

    document.getElementById('id').scrollIntoView();
    

    Has cross browser support 可能是最简单的方法......

    或到特定位置:

    window.scroll(x,y);
    

    Docs for window.scroll() here

    【讨论】:

      【解决方案2】:
      //Finds y value of given object
      function findPos(obj) {
          var curtop = 0;
          if (obj.offsetParent) {
              do {
                  curtop += obj.offsetTop;
              } while (obj = obj.offsetParent);
          return [curtop];
          }
      }
      //Get object
      var SupportDiv = document.getElementById('customer_info');
      
      //Scroll to location of SupportDiv on load
      window.scroll(0,findPos(SupportDiv));
      

      【讨论】:

      • 知道为什么它并不总是在 android 移动设备上运行吗?
      猜你喜欢
      • 1970-01-01
      • 2013-03-17
      • 2019-03-17
      • 2011-10-13
      • 2013-12-03
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多