简体   繁体   English

jQuery菜单幻灯片悬停故障

[英]jQuery menu slide hover glitch

I made a menu with < divs > and am using mouseover mouseout functions to append and remove a tongue (like a description for each menu icon). 我用<divs>制作了一个菜单,并且正在使用mouseover mouseout函数来添加和删除舌头(如每个菜单图标的描述)。 The issue I am having is when I hover over the tongue, it executes (correctly) the mouseout function for the button and removes the tongue. 我遇到的问题是,当我将鼠标悬停在舌头上时,它会(正确地)执行按钮的mouseout功能并移开舌头。

How can I prevent this? 我该如何预防? Thank you! 谢谢!

http://jsfiddle.net/QZ75D/ http://jsfiddle.net/QZ75D/

$('#wrapper').on('mouseover', '.1, .2, .3, .4, .5', function(){
    var tongue = '<div class = "tongue">'+$(this).attr('value')+'</div>';
    $(tongue).appendTo($(this)).animate({width:"toggle"});
});
$('#wrapper').on('mouseout', '.1, .2, .3, .4, .5', function(){
    var that = $(this);
    $('.tongue').animate({width:"toggle"},function(){
        that.children('.tongue').remove();
    });
}); 

Bonus: The text also is affected by the width animation. 奖励:文本也受宽度动画的影响。 Any way to fix that? 有什么办法解决吗?

DEMO DEMO

You can do the following to make your code work as expected. 您可以执行以下操作以使代码按预期工作。

  • Use mouseleave event instead of mouseout this will make sure child elements are detected as part of parent element and the event will only trigger when mouse leave element and it's child element. 使用mouseleave事件而不是mouseout可以确保将子元素检测为父元素的一部分,并且仅当鼠标离开元素及其子元素时才触发该事件。

  • Similarly change mouseover event with mouseenter this event will only trigger if cursor enter element or its child. 同样,使用mouseenter更改mouseover事件时,仅当光标输入元素或其子元素时才会触发此事件。

  • To avoid text from collapsing use white-space: nowrap; 为了避免文本折叠,请使用white-space: nowrap; on .tongue class. .tongue类上。

JS: JS:

$('#wrapper').on('mouseenter', '.1, .2, .3, .4, .5', function () {
    var tongue = '<div class = "tongue">' + $(this).attr('value') + '</div>';        
    $(tongue).appendTo($(this)).animate({
        width: "toggle"
    });
});
$('#wrapper').on('mouseleave', '.1, .2, .3, .4, .5', function () {
    var that = $(this);
    $('.tongue').animate({
        width: "toggle"
    }, function () {
        that.children('.tongue').remove();
    });
});


PS: Try to avoid class names like .1, .2, .3 they might work in java-script but they wont work in CSS. PS:尽量避免使用类名称,例如.1, .2, .3它们可能在Java脚本中起作用,但在CSS中却不起作用。

CSS does not allow class names which start with numbers, instead you can try .n_1, .n_2, .n_3 .... CSS不允许以数字开头的类名,而是可以尝试.n_1, .n_2, .n_3 ....

You don't really need JavaScript for this, depending on the level of support you need. 您实际上不需要JavaScript,具体取决于所需的支持级别。
I made an example in which I altered the HTML structure a bit, to make it a bit more semantic IMO. 我举了一个例子,其中我对HTML结构进行了一些改动,以使其更具语义IMO。
Animation wouldn't work in old IE, but functionality will (IE8). 动画无法在旧版IE中使用,但功能可以(IE8)。

HTML: HTML:

<ul class='wrapper'>
    <li class="button">Test</li>
    <li class="button">Test</li>
    <li class="button">Test</li>
    <li class="button">Test</li>
    <li class="button">Test</li>
</ul>

CSS: CSS:

.wrapper {
    width:50px;
    margin-left:40%;
    list-style: none;
    counter-reset: button-number
}

.button {
    counter-increment: button-number;
    width:50px;
    height:50px;
    background-color:#ccc;
    float: left;
    cursor:pointer;
    position: relative;
}

.button:before, .button:after {
    content:"Button number " counter(button-number);
    width:300px;
    height:50px;
    position:absolute;
    right: 50px;
    visibility: hidden;
}

.button:before {
    -webkit-transform: scaleX(0);
    -webkit-transform-origin: left;
    transform: scaleX(0);
    transform-origin: left;
    background-color:#000;
    color:#fff;
    -webkit-transition: -webkit-transform .4s, visibility 1s;
    transition: transform .4s, visibility 1s;
}

.button:hover:before {
    visibility: visible;
    -webkit-transform: scaleX(1);
}

.button:hover:after {
    width: 300px;
}

jsfiddle 的jsfiddle

Notes: 笔记:

  • The :after element is there to prevent the "tongue" from disappearing if the mouse leaves immediately to the left. :after元素可以防止鼠标立即离开左侧时“舌头”消失。
  • Animating transform instead of width is better for performance (element is not re-drawn) and eliminates weird text-wrapping during the animation. 动画transform ,而不是width为获得更好的性能(元素没有被重新绘制)和动画过程中消除了奇怪的文字环绕。

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

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