无法让JS运行

时间:2016-02-06 23:47:13

标签: javascript html css

我在CodePen上找到了一个我喜欢的滑块菜单,但无论我做什么,我都无法运行JS。我实际上尝试了多个代码源,但没有一个正常工作。

<html>
<head>
    <title>AOS Slide</title>
    <link rel="stylesheet" type="text/css" href="slide-test.css">
    <script src="slideTest.js"></script>
</head>

<div id="button" class="button">
    <div></div>
    <div></div>
    <div></div>
  </div>
  <div id="menu" class="none">
    <nav>
      <ul>
        <li class="menu-item">
          <a href="" class="link">Home</a>
        </li>
        <li class="menu-item">
           <a href="file:///Users/scatman01/Desktop/Code/AOS3.0/AOS-hike.html">Hike</a>
        </li>
        <li class="menu-item">
          <a href="file:///Users/scatman01/Desktop/Code/AOS3.0/AOS-bike.html">Bike</a>
        </li>
        <li class="menu-item">
          <a href="file:///Users/scatman01/Desktop/Code/AOS3.0/AOS-fishing.html">Fishing</a>
        </li>
        <li class="menu-item">
          <a href="file:///Users/scatman01/Desktop/Code/AOS3.0/AOS-run.html">Run</a>
          <li role="separator" class="divider"></li>
        </li><li class="menu-item">
          <a href="file:///Users/scatman01/Desktop/Code/AOS3.0/AOS-TripleCrown.html">Triple Crown</a>
          <li role="separator" class="divider"></li>
        </li><li class="menu-item">
          <a href="file:///Users/scatman01/Desktop/Code/AOS3.0/AOS-Gallery.html">Gallery</a>
        </li><li class="menu-item">
          <a href="" class="link">Contact</a>
        </li>
      </ul>
    </nav>
  </div>
  <div id="shadow" class="none"></div>
</body>
</html>

CSS:

html {
  height: 100%; 
}

body {
  height: 100%;
  margin: 0;
  padding: .1px;
  position: relative;
  background: url(http://www.adventuresofscatman.com/wp-content/uploads/2015/09/IMG_4273.jpg);
  background-size: cover;
  overflow-x: hidden;
}

.button{
  position: absolute;
  top: 30px;
  left: 25px;;
  width: 55px;
  height: 50px;
}

.button div {
  height: 20%;
  border-top: 7px solid rgb(103, 103, 103);
  cursor: pointer;
  transition: .5s;
}

.button:hover div {
  border-color: rgb(139, 139, 139); 
}

.menu-in,
.menu-out {
  padding: .1px;
  width: 240px;
  height: 100%;
  background: rgba(0, 0, 0, .65);
  overflow: hidden;
  position: absolute;
  top: 0;
}

.menu-in {
  -webkit-animation: menu-in .9s forwards ease;
}

.menu-out{
  -webkit-animation: menu-out .4s forwards ease-in; 
}

ul {
  margin: 68px 0 0 0;
  padding: 0;
}

.menu-item {
  list-style: none;
}

.link {
  display: block;
  text-decoration: none;
  color: rgb(190, 190, 190);
  font: 28px/50px arial;
  text-transform: uppercase;
  height: 50px;
  text-align: center;
  transition: .1s;
}

.link:hover {
  color: rgb(230, 230, 230);
  background: rgba(200, 200, 200, .1); 
}

.shadow-in,
.shadow-out{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  background: rgba(0, 0, 0, .4);
}

.shadow-in {
  -webkit-animation: shadow-in .9s forwards ease;
}

.shadow-out {
   -webkit-animation: shadow-out .4s forwards ease-in;
}

.none {
  display: none;
}

@-webkit-keyframes menu-in {
  0% {
    left: -240px;
  }
  100% {
    left: 0;
  }
}

@-webkit-keyframes menu-out {
  0% {
    left: 0;
  }
  100% {
    left: -240px;
  }
}  

@-webkit-keyframes shadow-in {
  0% {
    left: 0;
    opacity: 0;
  }
  100% {
    left: 240px;
    opacity: 1;
  }
}

@-webkit-keyframes shadow-out {
  0% {
    left: 240px;
    opacity: 1;
  }
  99%{
    height: 100%; 
  }
  100% {
    height: 0;
    left: 0;
    opacity: 0;
  }
}

JS:

document.getElementById('button').addEventListener('click', function() {
    showMenu();
});

document.getElementById('menu').addEventListener('click', function() {
    showMenu();
});

function showMenu() {
    console.log('click');
    var menu = document.getElementById('menu');
    var shadow = document.getElementById('shadow');
    var button = document.getElementById('button');

    menu.className = 'menu-in';
    shadow.className = 'shadow-in';
    button.className = 'none';
}

document.getElementById('shadow').addEventListener('click', function() {
    var menu = document.getElementById('menu');
    var shadow = document.getElementById('shadow');
    var button = document.getElementById('button');

    menu.className = 'menu-out';
    shadow.className = 'shadow-out';
    button.className = 'button';
});

1 个答案:

答案 0 :(得分:-1)

我自己对Javascript很陌生,但我必须说我遇到了类似你之前的类似问题。我无法让我的外部Javascript文件工作。我想我需要一个由Google托管在我的.html文件中的单独的jQuery库,以使其工作。

我不确定您需要哪一个(或者如果您需要的话),但是不能无伤害Google查找托管库并将<script></script>标记之间的链接粘贴在结束标记之上你的身体。

我的编码的一个小例子(可能会让你知道):

<!doctype html>
<html>

<head>
  <title></title>
  <link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>
  <div class="content">
    <div class="parallax-bg">
      <h1>Hello Everybody</h1>
    </div>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script type="text/javascript" src="js/scrolling.js"></script>
</body>

</html>