简体   繁体   English

JS滚动似乎在我的网站上不起作用

[英]JS Scrolling doesn't seem to work on my website

So I am trying to use the smooth scrolling animation used in this template: 因此,我尝试使用此模板中使用的平滑滚动动画:

https://blackrockdigital.github.io/startbootstrap-scrolling-nav/ https://blackrockdigital.github.io/startbootstrap-scrolling-nav/

After adding the js files to my directory, including the basic JQuery files, I've seen the custom .js file that adds the scrolling uses the .class parameter in an anchor tag to detect if clicking it should trigger the smoothscrolling. 在将js文件(包括基本的JQuery文件)添加到我的目录之后,我已经看到添加滚动的自定义.js文件使用锚标记中的.class参数来检测单击它是否应该触发平滑滚动。 So I added those to my anchor tags. 所以我将它们添加到了我的锚标签中。

Below is the relevant code. 以下是相关代码。

I will include a live preview too. 我还将提供实时预览。

index.html file index.html文件

<!-- Navigation section -->
<section class="nav" id="nav">
    <div class="container">
        <div class="row" style="width: 100%; text-align: center;">
            <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                <a class="js-scroll-trigger btn btn-lg btn-nav" href="#about">About me</a>
            </div>

            <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                <a class="js-scroll-trigger btn btn-lg btn-nav" href="#work">My work</a>
            </div>

            <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                <a class="js-scroll-trigger btn btn-lg btn-nav" href="#contact">Contact</a>
            </div>
        </div>
    </div>  
</section>

Script imports in index.html index.html中的脚本导入

<!-- Import js -->
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<script src="js/scrolling-nav.js"></script>

Scrolling-nav.js Scrolling-nav.js

(function($) {
  "use strict"; // Start of use strict

  // Smooth scrolling using jQuery easing
  $('a.js-scroll-trigger[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, "easeInOutExpo");
        return false;
      }
    }
  });

})(jQuery); // End of use strict

I have nearly no experience with JQuery / JS, so it's hard for me to understand where it might be going wrong. 我几乎没有使用JQuery / JS的经验,所以我很难理解它可能出了什么问题。

Here is a live preview of the website with above code in it: 这是上面带有代码的网站的实时预览:

Live preview 实时预览

Full code: 完整代码:

Github link Github链接

If there's any information missing, let me know. 如果缺少任何信息,请告诉我。

jQuery is missing, and you're using jQuery in your click event... 缺少jQuery,并且您在点击事件中使用了jQuery ...

在此处输入图片说明

Add this code in you jquery 在您的jquery中添加此代码

    $(document).ready(function(){
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM