试图在meteor js中将锚标记链接到div id

时间:2014-06-11 13:45:35

标签: css html5 hyperlink scroll meteor

出于某种原因,我似乎无法将我的锚标记链接到页面中较低的div id。继续link到破损的网站,尝试点击它如何工作并最小化您的窗口以允许滚动发生,它不向下滚动页面我不知道为什么。我现在试图用javascript(特别是jQuery和meteors模板事件)来解决这个问题。我认为meteor正在阻止从工作中做到这一点的经典方式,但我仍然卡住了。

我正在使用meteor,所以我认为这可能是我的问题,我现在正在尝试不同的解决方案,但它无法正常工作

JS

Template.postsList.events({
  'click #worksClick':function(e,tmpl) {
      e.preventDefault();
      $('html, body').animate({
          scrollTop: $("#works").offset().top
      }, 600);
   }
});

HTML

<template name="postsList">

<div class="container">

  <div class="logo">
    <h1>iClinic</h1>
  </div>

  <nav class="topNav">
    <ul>
      <li><a id="worksClick" href="#works">How it works</a></li>
      <li><a href="#">Applications</a></li>
      <li><a href="#">Our Story</a></li>
      <li><a href="#">Blog</a></li>
      <li><a href="#">Reach Us</a></li>
    </ul>
  </nav>

  <div class="mastHead">
    <header>
      <h2>iClinic helps the medical professional bring superior health-care to all people</h2>
    </header>
  </div><!--mastHead ends-->

  <div class="detailsWrapper">

    <section class="details">
      <h3>The more you know the better you can treat.</h3>
    </section>

  </div>

  <div id="works" class="worksWrapper">

    <section class="works">
      <h3>How it Works</h3>
      <p>iClinic runs on any web browser or Windows computer, you simply create a account and begin to intergrate your existing and new patient data into iClinic</p>
    </section>

  </div>


  <div id="workas" class="worksWrapper">

    <section class="works">
      <h3>How it Works</h3>
      <p>iClinic runs on any web browser or Windows computer, you simply create a account and begin to intergrate your existing and new patient data into iClinic</p>
    </section>

  </div>

</div><!--container ends-->

</template>

STYLUS(符合css,这不是错误编写的css)

*
  margin 0
  padding 0

.container
  width 100%

.adminNav
  display none

html
  font-size 90%
  font-family 'Source Sans Pro', sans-serif

.logo
  float left
  background #7d7d7d
  width 15%
  padding 3rem 0 3rem 0
  //padding-top 3.5rem
  h1
    font-size 4rem
    font-weight 100
    letter-spacing: 0.2rem
    text-align center
    color #fff


.topNav
  float left
  width 85%
  padding 4.7rem 0 3.8rem 0
  background #d3d3d3
  ul
    li
      display inline
      font-size 2rem
      font-weight 100
      a
        margin-left 3rem
        text-decoration none
        color #fff

.mastHead
  background #989797 url("http://www.proteus.com/assets/helius_professional_header2.jpg")
  background-size cover
  background-position 0 -5rem
  background-repeat no-repeat
  background-attachment fixed
  -webkit-filter: grayscale(100%);s
  width 100%
  clear left

header
  width 66%
  margin auto
  padding-top 5rem
  padding-bottom 5rem
  h2
    font-size 4rem
    font-weight 500
    letter-spacing 0.2rem
    text-align center
    color #fff
    text-shadow 2px 2px 2px #000

@media(min-width:1400px)
  header
    width 50%
    padding-top 20rem
    padding-bottom 20rem

.detailsWrapper
  width 100%
  background #989797
  float left

.details
  width 66%
  margin auto
  text-align center
  padding 5rem 0 5rem 0

  h3

    font-size 3rem
    font-weight 100
    color #fff
    letter-spacing 0.3rem

.worksWrapper
  width 100%
  float left
  background rgba(155, 154, 154, 0.18)

.works
  width 66%
  margin auto
  text-align center
  padding 5rem 0 5rem 0

  h3

    font-size 3rem
    font-weight 100
    color #fff
    letter-spacing 0.3rem

编译CSS

* {
    margin: 0;
    padding: 0
}

.container {
    width: 100%
}

.adminNav {
    display: none
}

html {
    font-size: 90%;
    font-family: 'Source Sans Pro', sans-serif
}

.logo {
    float: left;
    background: #7d7d7d;
    width: 15%;
    padding: 3rem 0 3rem 0
}

.logo h1 {
    font-size: 4rem;
    font-weight: 100;
    letter-spacing: 0.2rem;
    text-align: center;
    color: #fff
}

.topNav {
    float: left;
    width: 85%;
    padding: 4.7rem 0 3.8rem 0;
    background: #d3d3d3
}

.topNav ul li {
    display: inline;
    font-size: 2rem;
    font-weight: 100
}

.topNav ul li a {
    margin-left: 3rem;
    text-decoration: none;
    color: #fff
}

.mastHead {
    background: #989797 url("http://www.proteus.com/assets/helius_professional_header2.jpg");
    background-size: cover;
    background-position: 0 -5rem;
    background-repeat: no-repeat;
    background-attachment: fixed;
    -webkit-filter: grayscale(100%);
    width: 100%;
    clear: left
}

header {
    width: 66%;
    margin: auto;
    padding-top: 5rem;
    padding-bottom: 5rem
}

header h2 {
    font-size: 4rem;
    font-weight: 500;
    letter-spacing: 0.2rem;
    text-align: center;
    color: #fff;
    text-shadow: 2px 2px 2px #000
}

@media (min-width: 1400px) {
    header {
        width:50%;
        padding-top: 20rem;
        padding-bottom: 20rem
    }
}

.detailsWrapper {
    width: 100%;
    background: #989797;
    float: left
}

.details {
    width: 66%;
    margin: auto;
    text-align: center;
    padding: 5rem 0 5rem 0
}

.details h3 {
    font-size: 3rem;
    font-weight: 100;
    color: #fff;
    letter-spacing: 0.3rem
}

.worksWrapper {
    width: 100%;
    float: left;
    background: rgba(155,154,154,0.18)
}

.works {
    width: 66%;
    margin: auto;
    text-align: center;
    padding: 5rem 0 5rem 0
}

.works h3 {
    font-size: 3rem;
    font-weight: 100;
    color: #fff;
    letter-spacing: 0.3rem
}

5 个答案:

答案 0 :(得分:2)

这结束了我的工作。

 Template.postsList.events({
    'click #worksClick': function (evt) {
        $('html, body').animate({
        scrollTop: $("#works").offset().top
    }, 2000);
  }
});

答案 1 :(得分:1)

Javascript侦听器会阻止此操作。您点击的元素上绑定的任何event.stopPropagation()return false(正文,div,...)。您的页面在禁用Javascript时工作正常,请检查您的所有代码或向我们展示您的JS。

答案 2 :(得分:0)

在Meteor中不起作用的锚标签是known issue。这是一个简单的解决方法:

# Create link to anchor
<li><a href="{{pathFor 'postsList' hash='works'}}">How it works</a></li>

# Set anchor
<div id="works"></div>

答案 3 :(得分:0)

对于找到此帖子的任何人,以前的答案都可以快速改进。这是似乎对我有用的解决方案:

  window.setTimeout(function(){
    if(window.location.hash) {
      let checkContent = $(window.location.hash).length;
      if(checkContent > 0){
        $([document.documentElement, document.body]).animate({
          scrollTop: $(window.location.hash).offset().top
        }, 1000);
      }else{
        window.setTimeout(function(){
          let checkContent = $(window.location.hash).length;
          if(checkContent > 0){
            $([document.documentElement, document.body]).animate({
              scrollTop: $(window.location.hash).offset().top
            }, 1000);
          }
        }, 300);
      }
    }
  }, 300);

只需将其扔到onRendered函数中,它就可以涵盖大多数用例。它检查#元素,等待300毫秒以呈现事物,然后平滑滚动。如果该元素在300毫秒后不存在,则会再等待300毫秒,然后重试。

我知道它仍然不是100%最优的,但是比我发现的过去解决方案更具防御性。

希望这能帮助到别人! :)

答案 4 :(得分:-3)

您有一个名为workas的变量,不确定是否应该可行。