1

我有一个简单的单页网站,其中包含一些带有命名链接的导航。其中一个链接指向一个 id 为“home”的 div,它位于 body 标签的正下方。此链接的目标是返回到页面顶部,然而,当被选中时,页面将返回到页面顶部的下方(例如,需要您稍微向上滚动)。

在 Chrome、Firefox 和 Safari 上确认。没有javascript就不可能吗?仅供参考:我尝试使用 jsfiddle 并没有问题。

这是html:

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="main.css">
      <link href='http://fonts.googleapis.com/css?family=Cantata+One' rel='stylesheet' type='text/css'>
    </head>

<body>
    <div id="home">
        <div id="nav">
     <ul>
         <li><a href="#home">Home</a></li>
         <li><a href="#experiments">Experiments</a></li>
         <li><a href="#contact">Contact</a></li>
     </ul>
    </div>
        <h1>
          <span id="i">i</span> 
          <span id="n">n</span>
          <span id="c">c</span>
        </h1>
      <h2>
          <span id="tagline">"angel investor, coder want-to-be, nikola tesla groupie."</span>
    </h2>
</div>

<div id="experiments">
      <span id="ex">ex</span>   
          <span id="amples">amples</span>
    </div>
</body>

这是CSS:

/**
 * Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/)
 * http://cssreset.com
 */
REMOVED THIS PART OF THE CSS FOR BREVITY

/* Beginning of Custom CSS */

body {
background-image: url("straws.png");
 }

@Font-face {
font-family: 'curly';
src: url('fonts/HoneyScript-Light.ttf') format('truetype');
}

/* Main Title & Tag Line */
#home{
height: 1000px;
}
h1 {
    z-index: -100;
  text-align: center;
  margin-top: 10px;
  text-align: center;
}
#i {
    font-family: 'curly', serif;
    color: orange;
    font-size: 20em;
    text-shadow: 3px 3px 7px #999;
  }
 #n {
font-family: courier;
    color: black;
    font-size: 10em;
    text-shadow: 3px 3px 7px #999, -1px -1px -1px white, 1px 1px 0 #BBB;
}
#c {
        font-family: 'curly', serif;
    color: gray;
    font-size: 5em;
    text-shadow: 2px 2px 7px #999, -1px -1px -1px white, 1px 1px 0 #BBB;
}   
h2 {
  position: relative;
  top: -50px;
  text-align: center;
}  
 #tagline {
font-family: 'Cantata One' serif;
color: #666;
font-size: 2em;
}

/* Navigation */
#nav {
position: fixed;
margin-left: 45%;
font-family: 'arial';
font-size: 2em;
  margin-top: 40px;
}
#nav li {
display: inline;
margin-right: 10px;
 }
#nav li a {
color: grey;
text-decoration: none;
border: 2px solid black;
.webkit-box-shadow: 5px 5px 5px #777777;
box-shadow: 5px 5px 5px #777777;
padding: 5px;
} 
#nav li a:hover {
color: black;
}

#experiments{
height: 1000px;
 }
 #ex {
    font-family: 'curly', serif;
    color: orange;
    font-size: 20em;
    text-shadow: 3px 3px 7px #999;
  }
#amples {
font-family: courier;
    color: black;
    font-size: 10em;
    text-shadow: 3px 3px 7px #999, -1px -1px -1px white, 1px 1px 0 #BBB;
}
4

2 回答 2

1

不要显式设置#home 的高度,而是尝试使用溢出:自动

#home{
    overflow: auto;
}
于 2012-10-06T14:28:08.100 回答
0

在开始的正文标记之后使用命名锚

<body> <a name="top"></a>

然后重定向到#top,它会弹出。否则你必须使用js。

于 2012-10-06T14:18:19.463 回答