代码之家  ›  专栏  ›  技术社区  ›  Jim Soho

Facebook如何在加载不同页面时保持页眉和页脚不变?

  •  15
  • Jim Soho  · 技术社区  · 17 年前

    5 回复  |  直到 17 年前
        1
  •  39
  •   Josh Stodola    17 年前

    请参考Mark Brittingham的回答,了解如何设计它,尽管我认为这不是你在这里问的问题。我将向您详细介绍它的工作原理(以及为什么它相当出色)。

    http://www.facebook.com/profile.php?id=514287820&ref=profile

    http://www.facebook.com/home.php#/profile.php?id=514287820&ref=profile

    注意“#” fragment identifier/hash

    要使用Javascript实现这一点,您所要做的就是为这些链接分配一个点击事件处理程序,如下所示。。。

    var header = document.getElementById('header');
    var headerLinks = header.getElementsByTagName('a');
    
    for(var i = 0, l = headerLinks.length; i < l; i++) {
      headerLinks[i].onclick = function() {
        var href = this.href;
    
        //Load the AJAX page (this is a whole other topic)
        loadPage(href);  
    
        //Update the address bar to make it look like you were redirected
        location.hash = '#' + href;
    
        //Unfocus the link to make it look like you were redirected
        this.blur();
    
        //Prevent the natural HTTP redirect
        return false;
      }
    }
    

    另外,将哈希值更改为DOM中找不到的值(通过元素ID)将使页面一直滚动到顶部。看看我在说什么:你从脸书顶部向下滚动大约10像素,露出顶部菜单的一半。点击其中一个项目,一旦片段标识符更新(没有 任何

        2
  •  3
  •   Mark Brittingham    17 年前

    #Footer  { 
      font-size:xx-small; 
      text-align:left; 
      width:100%; 
      bottom:0px; 
      position:fixed; 
      left:0px; 
      background-color: #CCCCCC; 
      border-top: 1px solid #999999; 
      padding:4px; 
      padding-right:20px; 
      color:#666666; 
    }
    

    实际上,它会停留在页面上,但由于定位非常紧密且不变,除非页面加载时间过长,否则它看起来就像是这样。我不知道这是否是脸书所做的,但它会给你带来大致相同的效果。

        3
  •  0
  •   Zan Lynx    17 年前

    因此,新页面会快速返回包含样式、页眉和页脚的HTML。浏览器可以立即呈现此内容,因此当它显示下一页时,看起来这些内容没有改变。

    如果页面正在生成动态内容,一个好的技巧是将所有静态信息放在顶部,输出并刷新数据缓冲区。然后进行动态数据库查询等。

        4
  •  0
  •   bowenthebeard    17 年前
        5
  •  0
  •   Community Mohan Dere    9 年前