【问题标题】:How do I get this JS code from Codepen to work?如何从 Codepen 获取此 JS 代码以工作?
【发布时间】:2018-03-26 05:13:02
【问题描述】:

这里是codepen的链接:https://codepen.io/rishabhp/pen/aNXVbQ?limit=all&page=7&q=navigation+bar

我正在使用括号并创建了一个 html、css、js 文件。一切似乎都在工作,除了 js。我已经在 head 部分链接了 css 和 js 文件。我是否必须以某种方式链接其他内容或下载额外的文件?

头部标签如下所示:

<link type="text/css" rel="stylesheet" href="style.css" />
<script src="theFile.js"></script>

HTML 代码:

<nav>
  <ul>
    <li><a href="#1">First</a></li>
    <li><a href="#2">Second</a></li>
    <li><a href="#3">Third</a></li>
    <li><a href="#4">Fourth</a></li>
    <li><a href="#5">Fifth</a></li>
  </ul>
</nav>

<div class="sections">
  <section id="1"><h1>First</h1></section>
  <section id="2"><h1>Second</h1></section>
  <section id="3"><h1>Third</h1></section>
  <section id="4"><h1>Fourth</h1></section>
  <section id="5"><h1>Fifth</h1></section>
</div>

<footer></footer>

<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

CSS 代码:

@import url(https://fonts.googleapis.com/css?family=Open+Sans);

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Open Sans', sans-serif;
}

/* Navigation */

nav {
  width: 100%;
  height: 60px; 
  position: fixed; 
  top: 0;
  background: #1ABC9C;
}

nav ul {
  padding: 20px;
  margin: 0 auto;
  list-style: none;
  text-align: center;
}
nav ul li {
  display: inline-block;
  margin: 0 10px;
}
nav ul li a {
  padding: 10px 0;
  color: #fff;
  font-size: 1rem;
  text-decoration: none;
  font-weight: bold;
  transition: all 0.2s ease;
}
nav ul li a:hover {
  color: #34495E;
}
a.active {
  border-bottom: 2px solid #ecf0f1;
}

/* Headings */

h1 {
  font-size: 5rem;
  color: #34495E;
}

/* Sections */

section {
  width: 100%;
  padding: 50px;
  background: #fff;
  border-bottom: 1px solid #ccc;
  height: 500px;
  text-align: center;
}
section:nth-child(even) {
  background: #ecf0f1;
}
section:nth-child(odd) {
  background: #bdc3c7;
}
.sections section:first-child {
  margin-top: 60px;
}
section.active {}

footer {
  height: 500px;
  background: #34495e;
}

JS代码:

var sections = $('section')
  , nav = $('nav')
  , nav_height = nav.outerHeight();

$(window).on('scroll', function () {
  var cur_pos = $(this).scrollTop();

  sections.each(function() {
    var top = $(this).offset().top - nav_height,
        bottom = top + $(this).outerHeight();

    if (cur_pos >= top && cur_pos <= bottom) {
      nav.find('a').removeClass('active');
      sections.removeClass('active');

      $(this).addClass('active');
      nav.find('a[href="#'+$(this).attr('id')+'"]').addClass('active');
    }
  });
});

nav.find('a').on('click', function () {
  var $el = $(this)
    , id = $el.attr('href');

  $('html, body').animate({
    scrollTop: $(id).offset().top - nav_height
  }, 500);

  return false;
});

【问题讨论】:

  • 您是否在浏览器中收到任何控制台错误?
  • 正如 Phani 所说,尝试通过单击 F12 检查开发人员的控制台,如果底部没有出现则可能单击 Esc。

标签: javascript jquery html css


【解决方案1】:

在包含jquery.min.js 之后,您必须包含您的 JavaScript 文件。否则你的代码会在 jQuery 初始化之前被执行。

删除head 标记中的&lt;script src="theFile.js"&gt;&lt;/script&gt; 并将其添加到HTML 的最底部。

<nav>
  <ul>
    <li><a href="#1">First</a></li>
    <li><a href="#2">Second</a></li>
    <li><a href="#3">Third</a></li>
    <li><a href="#4">Fourth</a></li>
    <li><a href="#5">Fifth</a></li>
  </ul>
</nav>

<div class="sections">
  <section id="1"><h1>First</h1></section>
  <section id="2"><h1>Second</h1></section>
  <section id="3"><h1>Third</h1></section>
  <section id="4"><h1>Fourth</h1></section>
  <section id="5"><h1>Fifth</h1></section>
</div>

<footer></footer>

<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="theFile.js"></script>

你可以检查你的控制台(快捷键:F12),应该有类似的错误

$ is not defined

在控制台中。


编辑:

您可以通过等待页面完全加载来确保在执行代码时初始化 jQuery。您只需使用 $(document).ready() 函数包装您的代码。

$(document).ready(function(){
    var sections = $('section')
      , nav = $('nav')
      , nav_height = nav.outerHeight();

    $(window).on('scroll', function () {
      var cur_pos = $(this).scrollTop();

      sections.each(function() {
        var top = $(this).offset().top - nav_height,
            bottom = top + $(this).outerHeight();

        if (cur_pos >= top && cur_pos <= bottom) {
          nav.find('a').removeClass('active');
          sections.removeClass('active');

          $(this).addClass('active');
          nav.find('a[href="#'+$(this).attr('id')+'"]').addClass('active');
        }
      });
    });

    nav.find('a').on('click', function () {
      var $el = $(this)
        , id = $el.attr('href');

      $('html, body').animate({
        scrollTop: $(id).offset().top - nav_height
      }, 500);

      return false;
    });
});

【讨论】:

  • 不幸的是,我仍然在定义之前使用了“$”。
  • 括号感觉js文件里面有5个错误,我会通过添加以下内容来修复它们: /*jslint browser: true*/ /*global $, jQuery, alert*/跨度>
  • @ModChelly 我更新了答案以使用$(document).ready() 函数。
  • 似乎是插件故障我刚刚尝试了 1.6.4 的 jquery 并为所有 div 添加了红色边框并且它有效..
  • 使用您的原始代码?如果是这样,您可能应该在问题中将其作为编辑提及。
猜你喜欢
  • 2015-05-17
  • 1970-01-01
  • 2023-03-22
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-19
相关资源
最近更新 更多