javascript - Jquery 代码未执行

标签 javascript jquery

我目前正在学习 jQuery。我在使用以下代码时遇到了一些问题。我正在尝试使用一些按钮让我的网站移动,但是一旦我点击它们,它似乎就不起作用了。下面是我的 jquery 代码。

jQuery(document).ready(function ($) {
    //initialise Stellar.js
    $(window).stellar();
    //Cache some variables
    var links = $('.navigation').find('li');
    slide = $('.slide');
    button = $('.button');
    mywindow = $(window);
    htmlbody = $('html,body');
    //Setup waypoints plugin
    slide.waypoint(function (event, direction) {
        //cache the variable of the data-slide attribute associated with each slide
        dataslide = $(this).attr('data-slide');
        //If the user scrolls up change the navigation link that has the same data-slide attribute as the slide to active and
        //remove the active class from the previous navigation link
        if(direction === 'down') {
            $('.navigation li[data-slide="' + dataslide + '"]').addClass('active').prev().removeClass('active');
        }
        // else If the user scrolls down change the navigation link that has the same data-slide attribute as the slide to active and
        //remove the active class from the next navigation link
        else {
            $('.navigation li[data-slide="' + dataslide + '"]').addClass('active').next().removeClass('active');
        }
    });
    //waypoints doesnt detect the first slide when user scrolls back up to the top so we add this little bit of code, that removes the class
    //from navigation link slide 2 and adds it to navigation link slide 1.
    mywindow.scroll(function () {
        if(mywindow.scrollTop() === 0) {
            $('.navigation li[data-slide="1"]').addClass('active');
            $('.navigation li[data-slide="2"]').removeClass('active');
        }
    });
    //Create a function that will be passed a slide number and then will scroll to that slide using jquerys animate. The Jquery
    //easing plugin is also used, so we passed in the easing method of 'easeInOutQuint' which is available throught the plugin.
    function goToByScroll(dataslide) {
        htmlbody.animate({
            scrollTop: $('.slide[data-slide="' + dataslide + '"]').offset().top
        }, 2000, 'easeInOutQuint');
    }
    //When the user clicks on the navigation links, get the data-slide attribute value of the link and pass that variable to the goToByScroll function
    links.click(function (e) {
        e.preventDefault();
        dataslide = $(this).attr('data-slide');
        goToByScroll(dataslide);
    });
    //When the user clicks on the button, get the get the data-slide attribute value of the button and pass that variable to the goToByScroll function
    button.click(function (e) {
        e.preventDefault();
        dataslide = $(this).attr('data-slide');
        goToByScroll(dataslide);
    });
});

如果有人能帮我解决我的问题就太好了。

最佳答案

将您的脚本放在结束标记正文 (</body>) 之前并包含 Jquery 库

关于javascript - Jquery 代码未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23102008/

相关文章:

javascript - 内部点击中的 JQuery 错误 ID

javascript - 在浏览器 (F11) 中检测到全屏模式时使标题消失

php - 如何使用正斜杠 dd/mm/yyyy 验证 DOB 字段

javascript - 如何在数据表中添加数据导出按钮

javascript - 导航/子导航列表,如何在页面重新加载后为点击的项目提供事件类

php - 浏览器性能问题

javascript - 通过JS在表中添加新行,如何包含变量?

javascript - Uncaught ReferenceError : Meteor is not defined

javascript - 属性中的 JS var

javascript - 我可以在 Bootstrap 单选按钮组中取消选择的项目上捕获什么事件