javascript - jquery scroll 无法监听脚本文件中的事件

标签 javascript jquery html css

我的代码如下所示:

index.html

$(document).ready(function() {
    $(document).on("scroll", onScroll);
    alert('i am called');

    //smoothscroll
    $('a[href^="#"]').on('click', function(e) {
        e.preventDefault();
        $(document).off("scroll");
        $('a').each(function() {
            $(this).removeClass('active');
        })
        $(this).addClass('active');

        var target = this.hash,
            menu = target;
        $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top + 2
        }, 500, 'swing', function() {
            window.location.hash = target;
            $(document).on("scroll", onScroll);
        });
    });
});

function onScroll(event) {
    alert('i am clickked');
    var scrollPos = $(document).scrollTop();
    $('#q-nav a').each(function() {
        var currLink = $(this);
        var refElement = $(currLink.attr("href"));
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
            $('#q-nav ul li a').removeClass("active");
            currLink.addClass("active");
        } else {
            currLink.removeClass("active");
        }
    });
}
 html,
 body {
     margin: 0;
     padding: 0;
 }
 
 html {
     font-size: 14px !important;
 }
 
 body {
     font-family: "Open Sans";
     width: 100%;
     height: 100%;
     overflow-x: hidden;
     overflow-y: scroll;
 }
 
 .container {
     padding: 0;
     width: 100vw;
     height: 100vh;
     background-color: #ffffff;
 }
 
 #q-nav {
     width: 100%;
     position: absolute;
     top: 0;
     padding-top: 3rem;
     color: #ffffff;
 }
 
 #q-nav ul {
     display: flex;
     flex-direction: row;
     justify-content: flex-end;
 }
 
 #q-nav .q-nav-about {
     position: relative;
     color: #ffffff;
     text-decoration: none;
     font-weight: bold;
     padding: 0rem 2rem 0 2rem;
 }
 
 #q-nav ul li {
     list-style: none;
 }
 
 #q-nav .q-nav-work {
     position: relative;
     color: #ffffff;
     text-decoration: none;
     font-weight: bold;
     padding: 0rem 2rem 0 2rem;
 }
 
 #q-nav .q-nav-contact {
     position: relative;
     font-weight: bold;
     color: #ffffff;
     text-decoration: none;
     padding: 0rem 8rem 0 2rem;
 }
 
 .q-intro-text {
     height: 100%;
     position: absolute;
     top: 0;
     flex-direction: column;
     justify-content: center;
     display: flex;
 }
 
 .q-intro-text .q-mobile-app-text {
     margin: 0 0 0 10rem;
     color: #ffffff;
 }
 
 #about-us {
     width: 100%;
     height: 100%;
     background-color: green;
 }
 
 #work {
     width: 100%;
     height: 100%;
     background-color: purple;
 }
 
 #contact-us {
     width: 100%;
     height: 100%;
     background-color: blue;
 }
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans" />
    <link rel="stylesheet" href="../test/css/index.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <div id="myCarousel" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators">
                <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                <li data-target="#myCarousel" data-slide-to="1"></li>
                <li data-target="#myCarousel" data-slide-to="2"></li>
            </ol>

            <!-- Wrapper for slides -->
            <div class="carousel-inner">
                <div class="item active">
                    <img src="../test/images/slider1.png" alt="Los Angeles" style="
                    height:100vh;width:100vw;">
                </div>

                <div class="item">
                    <img src="../test/images/slider-2.png" alt="Chicago" style="
                    height:100vh;width:100vw;">
                </div>

                <div class="item">
                    <img src="../test/images/slider-3.png" alt="New york" style="
                    height:100vh;width:100vw;">
                </div>
            </div>
        </div>
    </div>

    <div id="q-nav">
        <ul>
            <li><a class="active q-nav-about" href="#about-us">ABOUT US</a>
            </li>
            <li><a class="q-nav-work" href="#work">WORK</a>
            </li>
            <li><a class="q-nav-contact" href="#contact-us">CONTACT US</a>
            </li>
            </li>
        </ul>

    </div>

    <div class="q-intro-text">
        <div class="q-mobile-app-text">MOBILE APP TEXT<br/>All the work and no play makes jack a dull boy!
            <br/> Join Libr8 cricket league. Unleash the <br/> sportsman in you</div>
    </div>

    <div id="#about-us">
        <img src="../test/images/slider-3.png" alt="New york" style="
                    height:100vh;width:100vw;">
    </div>
    <div id="#work">
        <img src="../test/images/slider-2.png" alt="Chicago" style="
                    height:100vh;width:100vw;">
    </div>
    <div id="#contact-us">
        <img src="../test/images/slider1.png" alt="Los Angeles" style="
                    height:100vh;width:100vw;">
    </div>
    <script src="../test/scripts/scrolling.js"></script>

</body>

</html>

在这里,我试图将代码甚至放入 html 文件中,但不知何故,当我向下滚动或单击 a-href 链接时,我无法滚动到那个特定的 div。那么jquery文件或导航菜单有什么问题吗?

最佳答案

从 div 标签的 id 中删除 #

<div id="about-us"> 
<img src="../test/images/slider-3.png" alt="New york" style=" height:100vh;width:100vw;"> 
</div> 
<div id="work"> 
<img src="../test/images/slider-2.png" alt="Chicago" style=" height:100vh;width:100vw;">
 </div> 
<div id="contact-us">
 <img src="../test/images/slider1.png" alt="Los Angeles" style=" height:100vh;width:100vw;"> 
</div>

关于javascript - jquery scroll 无法监听脚本文件中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053313/

相关文章:

javascript - 单击功能以显示下一个元素

javascript - 单个 WordPress 页面上的单个 JavaScript 函数

python - 如何在 Django 模板中使用模板?

javascript - 指望 fetch 来使用缓存是个坏主意吗?

php - 我需要将一个 php 变量传递给 DataTables.Editable sUpdateURL

javascript - php根据html切换开关显示数据库中的数据

javascript - CSS3 根据它们具有的属性转换多个 div

html - 在没有 javascript 或 jquery 帮助的情况下在选择选项标签中创建 href 链接

javascript - select2 ajax显示结果但无法选择

javascript - 在循环中分配给变量之前评估右侧表达式