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

标签 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>

但它只是将自己传送到它那里。我尝试设置动画应该花费更长的时间,但这也没有用。

Java代码:

$('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);
}
});

最佳答案

你遇到了一个问题,因为你没有添加 Jquery,因为这个 anchot 正在执行它的默认操作。即跳转到目标元素。

这是更新后的答案。

在你的 scroll.js 中 $(document).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>

在那之后一切都应该正常工作。

$('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);
  }
});
/*
 * 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;
  }
}
<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>

关于javascript - Jquery Scroll to Element 不会为我的滚动设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41645202/

相关文章:

javascript - 将数据传递给 jQuery 模式

html - 如何在 css 中为两个相邻单元格设置单个边框

javascript - 使用javascript在鼠标悬停时查看内容顶部的放大图像

javascript - 如何在执行函数之前加载一些图像

php - 验证其中之一

javascript - 引导日期时间选择器 : $ is not defined

javascript - 显示加载.. 在 bootstrap 4 中使用 jquery 和数据加载文本

javascript - 检查事件轮播项目是否具有值为 1 的属性

html - 如何异步加载 CSS

javascript - 到达英雄部分底部时,将静态导航栏更改为固定在滚动条上