简体   繁体   English

Click事件可防止mouseout触发

[英]Click event prevents mouseout from triggering

FINAL EDIT: I found a better solution and more simpler on this codepen . 最后编辑:我发现了一个更好的解决方案,并在此更简单codepen A demo of the working functionality. 工作功能的演示

EDIT: I found where the bug is coming from you can see an example here . 编辑:我发现bug来自哪里你可以在这里看到一个例子。 When you click on lets say the About tab and hover over and out on contact the content should be hidden. 当您单击时,可以说“关于”选项卡并将鼠标悬停在联系人上,应隐藏内容。 But you go back to hover over About and out the content stays visible, which is not. 但是你回过头来盘旋,内容保持可见,但事实并非如此。 How do I ensure the mouseout event is being triggered after clicked? 如何确保单击后触发mouseout事件?

EDIT 2: So I noticed the unbind() method prevents that. 编辑2:所以我注意到unbind()方法阻止了这一点。 When I remove it I can't seem to get the content area to stay active when clicked as the mouseout method overrides it. 当我删除它时,我似乎无法让内容区域在单击时保持活动状态,因为mouseout方法会覆盖它。

I did some research on this but could not find a solution as to why on hover the removeclass does not work. 我对此做了一些研究,但无法找到解决方案,为什么在悬停时删除类不起作用。 I have encountered a bug with addClass() and removeClass() functions. 我遇到了addClass()和removeClass()函数的错误。 The thing is I have those function firing on hover or mouseover/mouseout and on click so it gets a bit confusing. 问题是我有这些功能在悬停或鼠标悬停/鼠标移动和点击时触发,所以它有点令人困惑。 Here is a demo of what I'm working with: JSFiddle . 这是我正在使用的演示: JSFiddle

Full screen for better view. 全屏以获得更好的视图。

My JavaScript can be kind of messy but ultimately the way this is suppose to work: 我的JavaScript可能有点混乱,但最终这种方式可以起作用:

1. If you hover over a dot on the map the content on the left red box should reveal what's relevant to the location as well as a 'tooltip' of the location name. 1.如果将鼠标悬停在地图上的某个点上,左侧红色框中的内容应显示与该位置相关的内容以及位置名称的“工具提示”。 (this part works) (这部分有效)

2. You mouse out it's suppose to go back to the list of locations and the tooltip disappears. 2.您鼠标出它想回到的位置列表和工具提示消失。 Almost like a reset. 几乎像重置一样。

3. Now if you click on the dot, both the tooltip and the content on the left should remain active. 3.现在,如果单击该点,则工具提示和左侧的内容都应保持活动状态。 Until you either click on the "Back to the list" link on the red box or hover over the other dots. 直到您点击红色框上的“返回列表”链接或将鼠标悬停在其他点上。 (this also works) (这也有效)

The bug I encountered is if you click around the list panel and hover over a couple of the location dots after a certain while the hover state stays active when you hover over a couple of the locations (which is not suppose to happen). 我遇到的错误是你点击列表面板并在一段时间后悬停在几个位置点上,而当你悬停在几个位置上时悬停状态保持活动状态(不会发生这种情况)。 Everything is suppose to go back the list panel when you hover out of the location dot on the map. 当您将鼠标悬停在地图上的位置点之外时,一切都可以返回列表面板。

    $('a.location').click(function (event) {
    var loc = this.id;
    if ($('div.panel').hasClass('list')) {
        $('div.' + loc).removeClass('current');
        $('.list').addClass('current');
    }
    $('.list').removeClass('current');
    $('div.panel.' + loc).addClass('current');
    event.preventDefault();
}); //click function
$('.back-list').click(function (e) {
    $('.panel').removeClass('current');
    $('.list').addClass('current');
    $('div.location-title.show').removeClass('show').addClass('hide');
    $('div.location-title.view').removeClass('view');
    e.preventDefault();
}); //back button


$('ul.locations li > a').hover(function () {
//show location hover
var dot = this.id;
$('div.location-title.' + dot).removeClass('hide').addClass('show');

}, function () {
    var dot = this.id;
    //hide location hover
    $('div.location-title.' + dot).removeClass('show').addClass('hide');
}).click(function (event) {
    var dot = this.id;
    if (!$('div.location-title.' + dot).hasClass('hide')) {
        $('div.location-title.' + dot).addClass('view');
    } else {
        $('div.location-title.' + dot).removeClass('view');
    }
    event.preventDefault();
});

$('.map__container > span').on({
mouseover: function () { //mouseover
    var loc = $(this).attr('class');
    $('.panel').siblings().removeClass('current'); //resets all classes that have current
    $('.list').removeClass('current');
    $('div.panel.' + loc).addClass('current');
    $('div.show').removeClass('show').addClass('hide');
    $('div.location-title.' + loc).removeClass('hide').addClass('show');
    var asb = $('.location-title').siblings();
    $('div.location-title').siblings().removeClass('view');
},
mouseout: function () { //mouseout
    var loc = $(this).attr('class');
    $('div.' + loc).removeClass('current');
    $('div.location-title.' + loc).removeClass('show').addClass('hide');
    if (!$('div.' + loc).hasClass('current')) {
        $('.list').addClass('current');
    } else {
        $('.list').addClass('current');
    }
},
click: function () {
    $(this).off('mouseout');
    var loc = $(this).attr('class');
    $('div.location-title.show').removeClass('show').addClass('hide');
    $('div.location-title.' + loc).removeClass('hide').addClass('show');
}
});

Also if you have better suggestions to clean up my JavaScript I'm all ears. 此外,如果您有更好的建议来清理我的JavaScript,我会全力以赴。 Thanks so much! 非常感谢!

If i understand right, you might want to try with the event Mouseleave, and i would use to modularize the function toggleClass: 如果我理解正确,你可能想尝试使用事件Mouseleave,我将使用模块化函数toggleClass:

I hope this helps you . 我希望这会对你有所帮助 Salutations! 敬礼!

FINAL EDIT: I found a better solution and more simpler on this codepen . 最后编辑:我发现了一个更好的解决方案,并在此更简单codepen A demo of the working functionality. 工作功能的演示

My problem was in the code example above the $(this).off('mouseout'); 我的问题是在$(this).off('mouseout');上面的代码示例中$(this).off('mouseout'); was removing the mouseout when clicked. 点击时删除了mouseout。 So if you were to hover back to that dot on the map and mouseout the 'tooltip' would stay active, it won't disappear when you mouseout, which it should disappear. 因此,如果你要回到地图上的那个点并且鼠标移开'工具提示'将保持活动状态,它将不会在鼠标输出时消失,它应该消失。 I couldn't find a way to bind it again so the toggleClass() was much better. 我找不到再次绑定它的方法,所以toggleClass()要好得多。 I been pulling my hair on this! 我一直在拉我的头发!

 $('.map__container span').click(function(mapIcon){
                    mapIcon.preventDefault();
                    var icon = $(this).attr('class');  
                    var panel = $(this).attr('class');  

                    $('.panel').removeClass('clicked');
                    $('.location-title').removeClass('clicked');
                    $('.panel.' + panel).addClass('clicked');
                    $('.location-title.' + icon).addClass('clicked');
                });                
                //Show bubbles over dots on map
                $('.map__container span').hover(function(){
                    var hoverdot = $(this).attr('class');

                    $('.location-title.' + hoverdot).toggleClass('selected');
                });
                //Show bubbles on hover over anchor links
                $('a.location').hover(function(){
                    var hoverlink = this.id;

                    $('.location-title.' + hoverlink).toggleClass('selected');
                });
                //Anchor links show panels and bubbles
                $('a.location').click(function(e){
                    e.preventDefault();
                    var panel = this.id;
                    var icon = this.id;

                    $('.panel').removeClass('clicked');
                    $('.location-title').removeClass('clicked');
                    $('.panel.' + panel).addClass('clicked');
                    $('.location-title.' + icon).addClass('clicked');                        
                });
                //back button
                $('.back-list').click(function(backButton) {
                     backButton.preventDefault();

                    $('.panel').removeClass('clicked');
                    $('.location-title').removeClass('clicked');
                    $('.list').addClass('clicked');                              
                });

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

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