如何让我的网页显示与Codepen上显示的相同?

时间:2017-10-29 19:21:44

标签: jquery html css twitter-bootstrap

所以,我使用Bootstrap 4和JQuery快速添加在Codepen上启动了我的项目。我能够获得完美的结果,但是当我将完全相同的代码复制到我的文本编辑器并从那里运行时,页面的格式是关闭的。我尝试使用相同的Bootstrap和Jquery CDN链接,但没有运气。如何让我的代码以与Codepen.io相同的方式运行?我需要包含哪些链接?

这是理想的结果:

enter image description here

这是我在Firefox / Chrome浏览器中获得的结果。现在没有太大的区别,除了我的大部分Bootstrap 4样式都没有。此外,我现在在导航栏中遇到链接问题。当我尝试在text-decoration: none;元素和按钮上使用<a>时,我的JQuery代码不再有效。

enter image description here

这是我在Internet Explorer中获得的结果。显然,这些都是最糟糕的结果。我尝试移动我的链接,如Moe kanan建议,但我觉得有些东西我仍然缺少。

enter image description here

$(document).ready(function() {
  $('a').on('click', function() {
    $(this).parents().siblings().children().removeClass('active');
    $(this).addClass('active');
  });
  
 $('.menu-icon').on('click', function() {
    $('.nav-bar').toggleClass('hide-nav');
    $('main').toggleClass('expand-main');
 });
});
* {
  /*box-sizing: border-box; */
  padding: 0;
  margin: 0;
}

body, html {
  height: 100%;
  overflow: hidden; 
}

.main {
  background-image: url('http://hdimages.org/wp-content/uploads/2016/10/css-background-image-HD6-1.jpg');
  width: 75%;
  height: 100%;
  float: right;
}

.nav-bar {
  background-color:   #4a235a;
  width: 25%;
  height: 100%;
  opacity: 0.9;
}

ul {
  padding: 10% 30%;
}

ul li {
  color: white;
  text-transform: uppercase;
  list-style-type: none;
  padding: 20%;
  border-bottom: solid 1px gray;
  font-size: 1.2em;
}

ul li:hover {
  color: gray;
  font-weight: bold;
}

h3 {
  padding: 3%;
  font-size: 1.3em;
  color: lemonchiffon;
  font-weight: bold;
}

.active {
  color: navy;
  font-weight: bold;
}

h1 {
  font-family: Raleway;
  font-size: 2.6em;
  line-height: 1.4em;
  padding-left: 20%;
  padding-right: 20%;
  padding-top: 14%;
  padding-bottom: 4%;
  color: snow;
}

.button {
  color: white;
  font-size: 1.3em;
  text-align: center;
  border: solid 1px white;
  padding: 0.5em 0.7em;
}

.button:hover {
  color: #2c3e50;
  background-color:  #c39bd3;
  opacity: 0.9;
}

a {
  color: white;
}

a:hover {
  color: gray;
  text-decoration: none;
}


.menu-icon {
  width: 2.5%;
  margin: 1.2%;
  padding: 0.05% 0.02%;
}

img:hover {
  border: solid 1px #17202a;
  border-radius: 0.5em;
}

.hide-nav {
  display: none;
}

.expand-main {
  float: none;
  width: 100%;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Gab's Tech Space</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <link href="Gab's Tech Space Template.css" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Raleway" type="text/css" rel="stylesheet">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  </head>
  
  <body>
    <div class="main">
  <a href=#><img class="menu-icon" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Ic_menu_36px.svg/2000px-Ic_menu_36px.svg.png"></a>
  <h1 class="text-center">Hello! My name is <span>Gabby</span> and I create beautiful, professional, and responsive websites.</h1>
  <center><a class="button text-center" href="#"><span class="learn-more">Learn More</span></a></center>
</div>

<div class="nav-bar">
  <h3>Gab's Tech <br><span class="space text-center">Space</span></h3>
  <ul class="text-center">
    <li><a href=# class="active">Home</a></li>
    <li><a href=#>About</a></li>
    <li><a href=#>Projects</a></li>
    <li><a href=#>Contact</a></li>
  </ul>
</div>
    <script src="Gab's Tech Space Template.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

1。 重命名javaScript文件并删除空格,然后编辑脚本src。

Gab的Tech Space Template.js 更改为 gabsTechSpaceTemplate.js

改变这个:

<script src="Gab's Tech Space Template.js"></script>

对此:

<script src="gabsTechSpaceTemplate.js"></script>

注意:确保您的javaScript文件位于根文件夹中(与index.html文件相同)

2。 首先移动jQuery CDN链接: 您需要先导入库,然后再使用它。在您的情况下,您在导入之前就开始使用jQuery。因此,在其他javaScript代码或文件之前,始终将jQuery CDN放在顶部。

这应该先行:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

第二:

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

你的javaScript文件最后:

<script src="gabsTechSpaceTemplate.js"></script>

答案 1 :(得分:1)

在文件夹中创建这3个文件,然后将下面的代码复制/粘贴到每个文件中。

  1. 的index.html
  2. 的style.css
  3. index.js
  4. <强>的index.html

    <!DOCTYPE html>
    <html >
    <head>
      <meta charset="UTF-8">
      <title>Website Template 3 with Navbar</title>
    
      <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css'>
    
      <link rel="stylesheet" href="style.css">
    
      <link href="https://fonts.googleapis.com/css?family=Raleway" type="text/css" rel="stylesheet">
    
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    
    </head>
    
    <body>
    <div class="main">
      <a href=#><img class="menu-icon" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Ic_menu_36px.svg/2000px-Ic_menu_36px.svg.png"></a>
      <h1 class="text-center">Hello! My name is <span>Gabby</span> and I create beautiful, professional, and responsive websites.</h1>
      <center><a class="button text-center" href="#"><span class="learn-more">Learn More</span></a></center>
    </div>
    
    <div class="nav-bar">
      <h3>Gab's Tech <br><span class="space text-center">Space</span></h3>
      <ul class="text-center">
        <li><a href=# class="active">Home</a></li>
        <li><a href=#>About</a></li>
        <li><a href=#>Projects</a></li>
        <li><a href=#>Contact</a></li>
      </ul>
    </div>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
    
      <script  src="index.js"></script>
    
    </body>
    </html>
    

    <强>的style.css

    * {
      /*box-sizing: border-box; */
      padding: 0;
      margin: 0;
    }
    
    body, html {
      height: 100%;
      overflow: hidden; 
    }
    
    .main {
      background-image: url('http://hdimages.org/wp-content/uploads/2016/10/css-background-image-HD6-1.jpg');
      width: 75%;
      height: 100%;
      float: right;
    }
    
    .nav-bar {
      background-color:   #4a235a;
      width: 25%;
      height: 100%;
      opacity: 0.9;
    }
    
    ul {
      padding: 10% 30%;
    }
    
    ul li {
      color: white;
      text-transform: uppercase;
      list-style-type: none;
      padding: 20%;
      border-bottom: solid 1px gray;
      font-size: 1.2em;
    }
    
    ul li:hover {
      color: gray;
      font-weight: bold;
    }
    
    h3 {
      padding: 3%;
      font-size: 1.3em;
      color: lemonchiffon;
      font-weight: bold;
    }
    
    .active {
      color: navy;
      font-weight: bold;
    }
    
    h1 {
      font-family: Raleway;
      font-size: 2.6em;
      line-height: 1.4em;
      padding-left: 20%;
      padding-right: 20%;
      padding-top: 14%;
      padding-bottom: 4%;
      color: snow;
    }
    
    .button {
      color: white;
      font-size: 1.3em;
      text-align: center;
      border: solid 1px white;
      padding: 0.5em 0.7em;
    }
    
    .button:hover {
      color: #2c3e50;
      background-color:  #c39bd3;
      opacity: 0.9;
    }
    
    a {
      color: white;
    }
    
    a:hover {
      color: gray;
      text-decoration: none;
    }
    
    
    .menu-icon {
      width: 2.5%;
      margin: 1.2%;
      padding: 0.05% 0.02%;
    }
    
    img:hover {
      border: solid 1px #17202a;
      border-radius: 0.5em;
    }
    
    .hide-nav {
      display: none;
    }
    
    .expand-main {
      float: none;
      width: 100%;
    }
    

    <强> index.js

    $(document).ready(function() {
      $('a').on('click', function() {
        $(this).parents().siblings().children().removeClass('active');
        $(this).addClass('active');
      });
    
     $('.menu-icon').on('click', function() {
        $('.nav-bar').toggleClass('hide-nav');
        $('main').toggleClass('expand-main');
     });
    });