锚标签在页面上有效,但在页面上无效

时间:2018-09-18 08:36:07

标签: javascript html css anchor scrollmagic

我的项目遇到一个奇怪的问题,我的锚标记在页面上起作用,但在页面上似乎不起作用。我设法用最少的代码重新创建了这个问题。

我已在代码示例中添加了调试功能,以提供尽可能多的信息。我们使用的插件是ScrollMagic功能,我尝试尝试在JS中修复此问题,但无济于事。关于锚点为什么不向上滚动的任何想法?

/*============================================================
    -----SCROLLMAGIC SETUP-----
============================================================*/
var ctrl = new ScrollMagic.Controller({
  globalSceneOptions: {
    triggerHook: 'onLeave'
  }
});

$("section").each(function() {
  var name = $(this).attr('id');
  new ScrollMagic.Scene({
    triggerElement: this
  })
    .setPin(this)
    .addIndicators({
    colorStart: "rgba(255,255,255,0.5)",
    colorEnd: "rgba(255,255,255,0.5)",
    colorTrigger : "rgba(255,255,255,1)",
    name:name
  })
    .loglevel(3)
    .addTo(ctrl);
});

var wh = window.innerHeight;
new ScrollMagic.Scene({
  offset: wh*3
});

/*============================================================
    -----SCROLLMAGIC ANCHORS/SCROLLTO-----
============================================================*/
$('header a').on('click',function() {
  var targetSection = $(this).attr('href').substring(1);
  var targetPerc = (targetSection-1) / ($('nav a').length-1);
  var targetPos = scene.scrollOffset() + (scene.duration()*targetPerc);
  controller.scrollTo(targetPos);
});

/*============================================================
    -----BOUNCEIN ANIMATIONS ON VIEW-----
============================================================*/
$(function() {
  AOS.init();
});
/* Make the body 100% of the browser viewport height */
html, body, .hero { height: 110%; margin: 0; }

/* Make each section 100% of the browser viewport height */
section { height: 100%; position: relative; }

/* Header to follow through the page */
header { position: fixed; left: 5%; top: 40%; z-index: 99; font-size: 18px; }

/* Background colors for each section */
section#one   { background: url('https://via.placeholder.com/1920x1080') center / cover; }
section#two   { background: linear-gradient(90deg, #F4F6F5 50%, #FFFFFF 50%);            }
section#three { background: url('https://via.placeholder.com/1920x1080') center / cover; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.3/ScrollMagic.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/aos@3.0.0-beta.5/dist/aos.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/aos@2.3.0/dist/aos.css" rel="stylesheet"/>
<!-- Header -->
<header id="navigation">
  <p><a href="#one">who we are</a></p>
  <p><a href="#two">what we do</a></p>
  <p><a href="#three">get in touch</a></p>
</header>

<!-- Section One -->
<section id="one">
  <div class="container text-center" data-aos="fade-up">
    <h1>One</h1>
  </div>
</section>

<!-- Section Two -->
<section id="two">
  <div class="container text-center" data-aos="fade-up" data-aos-offset="0">
    <h2>Two</h2>
  </div>
</section>

<!-- Section Three -->
<section id="three">
  <div class="container text-center" data-aos="fade-up" data-aos-offset="0">
    <h1>Three</h1>
  </div>
</section>

2 个答案:

答案 0 :(得分:1)

如果将脚本添加为head元素的最后一部分,则代码似乎运行良好。

http://jsfiddle.net/1f7e3akn/10/

如果将其添加为外部脚本,也可以在同一位置使用

<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.3/ScrollMagic.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/aos@3.0.0-beta.5/dist/aos.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/aos@2.3.0/dist/aos.css" rel="stylesheet"/>
<script src="<your_script>"></script>

答案 1 :(得分:0)

我不确定ScrollMagic是否真的为您工作-向上或向下。在http://scrollmagic.io/examples/advanced/anchor_link_scrolling.html处查看他们的示例代码,您的代码与他们的代码有一些区别。也许尝试更紧密地复制文档?例如...

您将新的ScrollMagic.Controller保存到变量ctrl,但是稍后将其称为控制器-在调试页面时,变量控制器上的状态未定义。

在他们的示例中,他们还将新的ScrollMagic.Scene保存到名为scene的变量中,但是您没有保存它-后来又引用了变量场景,而我再次变得未定义。

我认为您的代码只能正常运行,因为它是作为普通锚标记运行的。