阻止锚点滚动到顶部!

发布于 2024-11-10 18:45:46 字数 3501 浏览 3 评论 0原文

我正在尝试建立一个网站,其中所有“页面”都在一个文档中。每个“页面”都是一个锚定部分,因此当我从位于网站顶部的主导航中调用它时,该部分会滑入视图。但我有两个问题:1)当我从网站外部链接时,我希望能够链接到任何单独的锚定部分,而无需页面向上滚动(因为这样你就看不到位于顶部的导航)和2)我希望这样,如果我刷新,页面就不会从头开始。为了明白我的意思,这里有一个网站,它具有与我完全相同的滑动内容到视图布局,并且准确地说明了我想要的内容:

http://www.incub.ro/#page-news

请注意,锚点的名称不仅显示在地址栏上,而且还显示在页面本身上完全加载并且不会将锚定主题滚动到顶部?是的,这就是我想要实现的目标!

我的代码看起来像这样:

HTML 代码 ------

<!-- Commence Navigation -->

<div class="wrapper">

<div id="headcontainer">
  <div id="banner"><a href="#home" class="panel"><img src="images/layout/0_homepage.png" id="top" height="100" width="420"border="0" /></a>

    <div id="navigation">
    <div class="center">
    <ul id="topnav">
        <li><a href="#link1" class="panel">Link 1</a></li> 
        <li><a href="#link2" class="panel">Link 2</a></li> 
        <li><a href="#link3" class="panel">Link 3</a></li> 
        <li><a href="#link4" class="panel">Link 4</a></li>
    </ul>
    </div>
    </div>
  </div>
</div>

<!-- End Navigation -->


<!-- Commence Content -->

<div id="wrapper">
<div id="mask">


    <div id="home" class="item">
    <a name="home"></a>
    <div id="container-slideshow"></div>
    </div>
    </div>

    <div id="link1" class="item">
    <a name="link1"></a>
    <div class="content">
    </div>
    </div>

    <div id="link2" class="item">
    <a name="link2"></a>
    <div class="content">
    </div>
    </div>

    <div id="link3" class="item">
    <a name="link3"></a>
    <div class="content">
    </div>
    </div>

    <div id="link4" class="item">
    <a name="about"></a>
    <div class="content">
    </div>
    </div>

   </div> 
   </div> 
   </div>
   <!-- End Content -->

   <div class="footer"> </div>

CSS 代码 ------

body {
    font-family: Helvetica, Arial, sans-serif;
    font-size: 0.8em;
    background-color: #131419;
    background-attachment: fixed;
    background-image: url("../images/layout/background_lrg2.jpg");
    background-position: center top;
    width:100%;
    overflow:hidden;
    padding: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-left: 0px;
}

.wrapper {
    min-height: 100%;
    height: 70%;
    margin-top: 0;
    margin-right: auto;
    margin-left: auto;
    overflow:hidden; 
  }

#wrapper {
    width:100%;
    height:100%;
    position:relative;
    top:0px;
    left:0;
    overflow:hidden;
}


#mask {
    width:900%;
    height:100%;
    overflow:hidden;
}

.item {
    width:11.1%;
    height:100%;
    float:left;
    margin-right: auto;
    margin-left: auto;
    position:relative;
    top:30px;
    overflow:hidden;
}

.content {
    font-family: Helvetica, Arial, sans-serif;
    color: #d1d1d1;
    font-size: 14px;
    line-height: 22px;
    width: 1000px;
    overflow:auto;
    padding-left: 30px;
    padding-right: 10px;
    padding-bottom: 10%;
    position:relative;
    margin-top: 0;
    margin-right: auto;
    margin-left: auto;
}

我希望这是有道理的。提前致谢!

I'm trying to build a website where all the 'pages' are in a single document. Each 'page' is an anchored section, so when I call it from the main navigation which is placed at the top of the site, the section slides into view. But I have two issues: 1) when I link from outside the site, I want to be able to link to any individual anchored section without having the page scroll up (because then you can't see the Navigation which is placed at the top) and 2) I'd like it so that if I refresh, the page doesn't start from the beginning. To get what I mean, here's a site that has exactly the same sliding-content-into-view layout that I have and illustrates exactly what I'd like to have in addition:

http://www.incub.ro/#page-news

Notice how not only the name of the anchor appears on the address bar but the page itself loads in full and doesn't scroll the anchored subject up to the top? Yeah, well that's what I'm trying to achieve!

My code looks something like this:

HTML CODE ------

<!-- Commence Navigation -->

<div class="wrapper">

<div id="headcontainer">
  <div id="banner"><a href="#home" class="panel"><img src="images/layout/0_homepage.png" id="top" height="100" width="420"border="0" /></a>

    <div id="navigation">
    <div class="center">
    <ul id="topnav">
        <li><a href="#link1" class="panel">Link 1</a></li> 
        <li><a href="#link2" class="panel">Link 2</a></li> 
        <li><a href="#link3" class="panel">Link 3</a></li> 
        <li><a href="#link4" class="panel">Link 4</a></li>
    </ul>
    </div>
    </div>
  </div>
</div>

<!-- End Navigation -->


<!-- Commence Content -->

<div id="wrapper">
<div id="mask">


    <div id="home" class="item">
    <a name="home"></a>
    <div id="container-slideshow"></div>
    </div>
    </div>

    <div id="link1" class="item">
    <a name="link1"></a>
    <div class="content">
    </div>
    </div>

    <div id="link2" class="item">
    <a name="link2"></a>
    <div class="content">
    </div>
    </div>

    <div id="link3" class="item">
    <a name="link3"></a>
    <div class="content">
    </div>
    </div>

    <div id="link4" class="item">
    <a name="about"></a>
    <div class="content">
    </div>
    </div>

   </div> 
   </div> 
   </div>
   <!-- End Content -->

   <div class="footer"> </div>

CSS CODE ------

body {
    font-family: Helvetica, Arial, sans-serif;
    font-size: 0.8em;
    background-color: #131419;
    background-attachment: fixed;
    background-image: url("../images/layout/background_lrg2.jpg");
    background-position: center top;
    width:100%;
    overflow:hidden;
    padding: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-left: 0px;
}

.wrapper {
    min-height: 100%;
    height: 70%;
    margin-top: 0;
    margin-right: auto;
    margin-left: auto;
    overflow:hidden; 
  }

#wrapper {
    width:100%;
    height:100%;
    position:relative;
    top:0px;
    left:0;
    overflow:hidden;
}


#mask {
    width:900%;
    height:100%;
    overflow:hidden;
}

.item {
    width:11.1%;
    height:100%;
    float:left;
    margin-right: auto;
    margin-left: auto;
    position:relative;
    top:30px;
    overflow:hidden;
}

.content {
    font-family: Helvetica, Arial, sans-serif;
    color: #d1d1d1;
    font-size: 14px;
    line-height: 22px;
    width: 1000px;
    overflow:auto;
    padding-left: 30px;
    padding-right: 10px;
    padding-bottom: 10%;
    position:relative;
    margin-top: 0;
    margin-right: auto;
    margin-left: auto;
}

I hope that makes sense. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2024-11-17 18:45:46

你愿意使用 jQuery 吗?如果是这样,也许这个片段可以提供帮助:

$(function() {
    // assigns click action for the a's
    $('#topnav a').click(function(e) {
        // prevent default behavior so clicking the <a> won't go up
        e.preventDefault();
        // get the anchor in the <a> and change the window hash
        // e.g. http://site.com/#link4
        window.location.hash = $(this).attr('href');
    });
});

Are you willing to use jQuery? If so, maybe this snippet can help:

$(function() {
    // assigns click action for the a's
    $('#topnav a').click(function(e) {
        // prevent default behavior so clicking the <a> won't go up
        e.preventDefault();
        // get the anchor in the <a> and change the window hash
        // e.g. http://site.com/#link4
        window.location.hash = $(this).attr('href');
    });
});
请止步禁区 2024-11-17 18:45:46

好吧,最后我设法找到了锚点向上滚动问题的解决方案。只需将 #headcontainer 的位置更改为“固定”并将其移动到 HTML 页面的底部即可,同时添加“top:200px;”到我的内容类,以便我们可以看到导航。

因此,在 tradyblix 的帮助下,这基本上解决了我的问题,尽管当我点击导航上的链接时,内容会发生一种奇怪的“闪烁”。我真的不知道该如何解释。但它以前不存在,所以它与新代码有关......

Okay, finally I managed to find a solution to the anchors scrolling upwards issue. Just changing the position of the #headcontainer to "fixed" and moving it at the bottom of the HTML page did the trick, along with adding "top:200px;" to my content class, so that we can see the navigation.

So with the help of tradyblix, this essentially solves my problems, although there is a weird sort of "flicker" that happens to the content when I click on a link on the nav. I don't really know how to explain it. It wasn't there before though so it has to do with the new code...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文