单击事件停止工作

Click event stops working

本文关键字:停止工作 事件 单击      更新时间:2023-09-26

我希望有人能帮助我,我对这个问题有点恼火,因为我一直在解决我试图在这个网站上实现的滚动行为的其他问题。。。正如你所看到的,当你滚动时,我的子菜单(在左边(会跟随窗口位置,或者你可以点击子菜单上的任何选项来触发动画,好吧,问题似乎是每次我点击第二次它都不起作用,直到我再次点击它,它才会起作用。。。

这是我的代码

变量

var startDistance = 210;
var $scrollingDiv = $("#sub-menu");
var position = $("#footer").position();
var height = $("#sub-menu").height();
var pos = position.top - (height + 460);

这是不同于用户和动画滚动

$("body,html").bind("scroll mousedown DOMMouseScroll mousewheel keyup", function(e){
            if ( e.which > 0 || e.type === "mousedown" || e.type === "mousewheel"){
                 if($(window).scrollTop() > startDistance && $(window).scrollTop() < pos) {
                    $scrollingDiv.stop().animate({
                        paddingTop: ($(window).scrollTop() - 75) + "px"
                    }, 'slow');
                }
                if($(window).scrollTop() == 0) {
                    $scrollingDiv.stop().animate({
                        paddingTop: 0
                    }, 'slow');
                }
            }
     });

点击行为

$("#sub-menu ul li a").live('click', function(ev) {
            var $anchor = $(this);
            console.log($anchor.attr('href'));
            $('html, body').stop().animate({
                scrollTop: $($anchor.attr('href')).offset().top
            }, 1500,'easeInOutExpo', function() {
                $scrollingDiv.stop().animate({
                    paddingTop: ($($anchor.attr('href')).offset().top - 556) + "px"
                }, 'slow');
            });
        event.preventDefault();
    });

这可能是最愚蠢的事情,但我已经看了最后一个小时的屏幕,没有意识到发生了什么

编辑:我也发布了我的滚动代码行为,因为我觉得这可能是一个全球性的问题,不仅与我的点击代码有关

提前谢谢!

[22:00:25.137] ReferenceError: event is not defined @ http://altivamedia.com/pruebas/romulos/wp/wp-content/themes/romulos/_/js/functions.js:45

您不小心写了event而不是ev:

        });
    event.preventDefault();
});

由于尚未定义event,因此会抛出ReferenceError。只需使用正确的变量:

        });
    ev.preventDefault();
});

备注

为了自己查找此类错误,请使用浏览器的错误控制台。