导航到Android上的section / fragment(href = page#section)

时间:2015-06-15 20:16:43

标签: javascript jquery html

我正在尝试将我的footer.php #section添加到联系表单中,它在PC浏览器上运行良好,但Android web开发让我疯狂。

如何从page_2#form添加指向page_1的部分链接?

我试过了

<a name=form> ... <a href=page_2#form>

<div id=form name=form> ... <a href=page_2#form>

但是Android浏览器并不喜欢它。

有任何建议或解决方法吗?

1 个答案:

答案 0 :(得分:0)

下面!有一个polyfill:

(function checkHashFragForSection () {
  document.addEventListener('load', fragExtractor)
  addEventListener('hashchange', fragExtractor)

  function fragExtractor () {
    var split = location.href.split('#')
    if(split[1] && split[1].length > 0) scrollToSection(split[1])
  }

  function scrollToSection (name) {
    var newPos = getYOffset(getElement(name))
    if(newPos) scrollTo(newPos)
  }

  function getElement (name) {
    return document.getElementById(name)
  }

  function getYOffset(elem) {
    var offset = 0
    if(!elem) return false
    while (elem && (offset += elem.offsetTop)) elem = elem.offsetParent
    return offset
  }
})()