Jquery Scroll to Element不会为我的滚动设置动画

时间:2017-01-13 23:51:24

标签: javascript jquery html css

Jquery代码从不动画它在开始时滚动它,它只是传送到我指定给它的元素。 HTML代码:

        <div class="inner cover">
        <h1 class="cover-heading">Steampunk :: New vision</h1>
        <p class="lead">Steampunk :: New Vision is a Steampunk style RTS game developed by ATMOX.</p>
        <p class="lead">
          <a href="#Information" class="btn btn-lg btn-secondary">Learn more</a>
        </p>
      </div>

点击Anchor了解更多信息。它应该为这个元素设置动画:

<h3 class="margin20" id="Information">About the Game <br><h6> -and it's Developers</h6></h3>

然而它只是传送到它。我尝试设置延长动画的时间,但这也没有用。

Javacode:

$('a[href^="#"]').on('click', function(event) {
var target = $(this.getAttribute('href'));
if( target.length ) {
    event.preventDefault();
    $('html, body').stop().animate({
        scrollTop: target.offset().top
    }, 4000);
}
});

1 个答案:

答案 0 :(得分:0)

您正面临一个问题,因为您尚未添加Jquery,因为此锚点正在执行其默认操作。即跳转到目标元素。

以下是更新后的答案。

你的scroll.js中的

$(文档).ready(); 不正确,应该是。

   $(document).ready(function(){
    $('a[href="#Information"]').on('click', function(event) {
    var target = $(this.getAttribute('href'));
    if( target.length ) {
        event.preventDefault();
        $('html, body').animate({
            scrollTop: target.offset().top
        }, 4000);
    }
    });
});

第二个问题是你使用jquery.slim.min.js覆盖了jquery.min.js库,它没有很多功能,包括动画。

从index.html中删除这两行。

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>

之后一切都应该正常。

&#13;
&#13;
$('a[href="#Information"]').on('click', function(event) {
  var target = $(this.getAttribute('href'));
  if (target.length) {
    event.preventDefault();
    $('html, body').stop().animate({
      scrollTop: target.offset().top
    }, 4000);
  }
});
&#13;
/*
 * Globals
 */

#testcontainer {
  height: 1000px;
  width: 100%;
  background-color: transparent;
}
.paragraph {
  font-size: 18px;
  margin-left: 30px;
  margin-right: 30px;
}
.margin20 {
  margin-right: 20px;
}
.padding15 {
  padding-right: 15px;
}
.headerclass {
  width: 100%;
  height: 60px;
  background-color: #2B2B2B;
}
/* */

.titleclass {
  padding-top: 10px;
  margin-left: 20px;
  float: left;
}
/* Links */

a,
a:focus,
a:hover {
  color: #fff;
}
/* Custom default button */

.btn-secondary,
.btn-secondary:hover,
.btn-secondary:focus {
  color: #333;
  text-shadow: none;
  /* Prevent inheritance from `body` */
  background-color: #fff;
  border: .05rem solid #fff;
}
/*
 * Base structure
 */

html,
body {
  height: 100%;
  background-image: url("Images/xvLjyle.jpg");
  height: 500px;
  background-attachment: fixed;
  background-repeat: no-repeat;
}
table,
th,
td {
  border: 1px solid black;
}
body {
  color: #fff;
  text-align: center;
  text-shadow: 0 .05rem .1rem rgba(0, 0, 0, .5);
}
/* Extra markup and styles for table-esque vertical and horizontal centering */

.site-wrapper {
  display: table;
  width: 100%;
  height: 100%;
  /* For at least Firefox */
  min-height: 100%;
  -webkit-box-shadow: inset 0 0 5rem rgba(0, 0, 0, .5);
  box-shadow: inset 0 0 5rem rgba(0, 0, 0, .5);
}
.site-wrapper-inner {
  display: table-cell;
  vertical-align: top;
}
.cover-container {
  margin-right: auto;
  margin-left: auto;
}
/* Padding for spacing */

.inner {
  padding: 2rem;
  margin-top: 40px;
}
/*
 * Header
 */

.masthead {
  margin-bottom: 2rem;
}
.masthead-brand {
  margin-bottom: 0;
}
.nav-masthead .nav-link {
  margin-right: 40px;
  padding: .40rem 0;
  font-weight: bold;
  color: rgba(255, 255, 255, .5);
  background-color: transparent;
  border-bottom: .25rem solid transparent;
}
.nav-masthead .nav-link:hover,
.nav-masthead .nav-link:focus {
  border-bottom-color: rgba(255, 255, 255, .25);
}
.nav-masthead .nav-link + .nav-link {
  margin-left: 1rem;
}
.nav-masthead .active {
  color: #fff;
  border-bottom-color: #fff;
}
@media (min-width: 48em) {
  .masthead-brand {
    float: left;
  }
  .nav-masthead {
    float: right;
  }
}
/*
 * Cover
 */

.cover {
  padding: 0 1.5rem;
}
.cover .btn-lg {
  padding: .75rem 1.25rem;
  font-weight: bold;
}
/*
 * Footer
 */

.mastfoot {
  color: rgba(255, 255, 255, .5);
}
/*
 * Affix and center
 */

@media (min-width: 40em) {
  /* Pull out the header and footer */
  .masthead {
    position: fixed;
    top: 0;
  }
  .mastfoot {
    position: fixed;
    bottom: 0;
  }
  /* Start the vertical centering */
  .site-wrapper-inner {
    vertical-align: middle;
  }
  /* Handle the widths */
  .masthead,
  .mastfoot,
  .cover-container {
    width: 100%;
    /* Must be percentage or pixels for horizontal alignment */
  }
}
@media (min-width: 62em) {
  .masthead,
  .mastfoot,
  .cover-container {
    width: 42rem;
  }
}
&#13;
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="ATMOX">
  <meta name="author" content="ATMOX">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js">
  </script>
  <script type="text/javascript" src="java / scroll.js "></script>
  <title>Steampunk :: New Vision</title>
  <link href="../../dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="cover.css" rel="stylesheet">
</head>
<div class="inner cover">
  <h1 class="cover-heading">Steampunk :: New vision</h1>
  <p class="lead">Steampunk :: New Vision is a Steampunk style RTS game developed by ATMOX.</p>
  <p class="lead">


    <a href="#Information" class="btn btn-lg btn-secondary">Learn more</a>
  </p>
</div>
<div id="testcontainer">

</div>


<h3 class="margin20" id="Information">About the Game <br><h6> -and it's Developers</h6></h3>
&#13;
&#13;
&#13;