简体   繁体   English

点击事件停止工作

[英]Click event stops working

I wish someone could help me, I'm a little bit irked about this problem since I've been resolving other issues with this scroll behavior that I'm trying to implement on this site ... as you see my sub-menu (on the left) follows the window position as you scroll or you can click any option on the sub-menu wich will trigger an animation, well what seems to be the problem is that everytime I click a second time it won't work, until I re-click it, it will work... 我希望有人可以帮助我,因为我一直在尝试解决此滚动行为的其他问题,因此我对此感到有点生气,这是我试图在本网站上实现的...如您所见,我的子菜单(左侧)跟随您滚动时的窗口位置,或者您可以单击子菜单上的任何选项,这将触发动画,这似乎是问题在于,每次我单击第二次均不起作用,直到我重新单击它,它将起作用...

Here's my code 这是我的代码

Variables 变数

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

This is to differ between users and animation scroll 用户和动画滚动之间有所不同

$("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');
                }
            }
     });

Click behavior 点击行为

$("#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();
    });

It might be the silliest thing but I've been watching my screen for last hour w/o being able to realize what's going on. 这可能是最愚蠢的事情,但是我最近一个小时一直在看屏幕,却无法意识到正在发生的事情。

Edit: I'm posting my scroll code behavior too because I feel that It might be a global problem and not only related to my click code 编辑:我也发布了滚动代码行为,因为我认为这可能是一个全球性问题,不仅与我的点击代码有关

Thank you in advance! 先感谢您!

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

You've accidentally wrote event instead of ev : 您不小心写了event而不是ev

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

Since event hasn't been defined a ReferenceError gets thrown. 由于尚未定义event因此将引发ReferenceError Simply use the correct variable: 只需使用正确的变量:

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

Remark 备注

In order to find such errors on your own use your browser's error console. 为了自己找到此类错误,请使用浏览器的错误控制台。

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

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