javascript - 我如何修复我的 js 代码,以便它与 Bootstrap 4 Collapse-div 一起使用?

标签 javascript jquery twitter-bootstrap

正如您在下面的代码片段中看到的,当您单击该链接时,它不起作用。它应该显示一个带有文本的隐藏 div。但是,如果删除 JS 代码,它就可以工作。

我的问题是我需要使用这个 JS 代码才能在整个页面中平滑地向下滚动(例如:当有人单击链接时,它会将您发送到页面末尾等......)

有什么方法可以让我的 JS 和这个 bootstrap 4 代码一起工作吗?它们完全分开工作。

$(function() {
  $('a[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});
<!DOCTYPE html>
<html lang="en">
<head>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
  <link rel="stylesheet" href="font-awesome-animation.min.css">
</head>
<body>
  <div style="margin: 0 auto; text-align: center; display: block">
    <i aria-hidden="true" class="fa fa-chevron-down yarrow"></i>  <a aria-controls="work-exp-collapse" aria-expanded="false" class="SeeMore2 aa-gray-line" data-toggle="collapse" href="#work-exp-collapse">view more experience</a>
  </div>
  <div class="collapse" id="work-exp-collapse">
    <div class="card card-block">
      Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
</body>
</html>

谢谢

最佳答案

来自jQuery release notes :

Slim build

Sometimes you don’t need ajax, or you prefer to use one of the many standalone libraries that focus on ajax requests. And often it is simpler to use a combination of CSS and class manipulation for all your web animations. Along with the regular version of jQuery that includes the ajax and effects modules, we’ve released a “slim” version that excludes these modules.

因此,根据评论,并且由于您使用的是 animate 函数,因此您需要包含基本 jQuery 而不是 SLIM 版本:

https://code.jquery.com/jquery-3.1.1.min.js

示例:

$(function() {
  $('a[href*="#"]:not([href="#"])').click(function(e) {
    if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length) {
        e.preventDefault();
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
      }
    }
  });
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome-animation/0.0.10/font-awesome-animation.min.css">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>

<div style="margin: 0 auto; text-align: center; display: block">
    <i aria-hidden="true" class="fa fa-chevron-down yarrow"></i>  <a aria-controls="work-exp-collapse" aria-expanded="false" class="SeeMore2 aa-gray-line" data-toggle="collapse" href="#work-exp-collapse">view more experience</a>
</div>
<div class="collapse" id="work-exp-collapse">
    <div class="card card-block">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
    </div>
</div>

关于javascript - 我如何修复我的 js 代码,以便它与 Bootstrap 4 Collapse-div 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42027361/

相关文章:

javascript - pm2 在集群模式下弃用了 worker

php - yii 使用下拉列表对 CListView 进行排序

javascript - 是否可以在它之外使用ajax响应?

html - 在 Eclipse 中开发时在哪里存储 twitter bootstrap

javascript - Bootstrap 跳转到折叠面板内的链接

javascript - 如何显示显示为 : none in a CSS rule? 的元素

javascript - 自动为当前URL生成goo.gl短URL

javascript - 单击父级打开子选项卡

javascript - 如何在 if 语句中包含鼠标滚轮滚动和单击事件?

css - Bootstrap : form-control size doesnt change based on parent div col size