简体   繁体   English

显示隐藏的jQuery

[英]Show if hidden jQuery

I have a div that I show if the mouse is hovering over a different div, then when the mouse leaves the div is hidden. 我有一个div,它显示鼠标是否悬停在另一个div上,然后当鼠标离开时div被隐藏。

In my mousemove callback function I say $('#divToShow').show() , however I'm wondering if this is inefficient as mousemove events are fired very frequently, and if that div is already shown then it's calling show() for no reason. 在我的mousemove回调函数中,我说$('#divToShow').show() ,但是我想知道这是否效率不高,因为mousemove事件被频繁触发,并且如果该div已经显示,那么它将调用show()没理由。

Would it be more efficient to check if the div is hidden, and only show it then? 检查div是否隐藏,然后仅显示它会更有效吗? Like this: 像这样:

if ($('#divToShow').is(":hidden")){
  $('#divToShow').show();
}

Or another solution would be to have a boolean variable that is set to true the first time the div is shown, then set to false on mouseleave . 或另一种解决方案是让一个布尔变量在第一次显示div时设置为true,然后在mouseleave上设置为false。

Does anyone have any information on an efficient way to show a div in a mousemove function? 有没有人有任何关于在mousemove函数中显示div的有效方式的信息?

Since you're using Jquery, a more efficient way to do what you want is using a .hover(handlerIn, handlerOut) callback, so you don't need to worry about creating a flag or something like that. 由于您使用的是Jquery,因此更有效的方法是使用.hover(handlerIn,handlerOut)回调,因此您无需担心创建标志或类似的事情。

The handlerIn will be triggered only one time, when the mouse enter on the target div (as you can see on the console.log('show') call). 当鼠标进入目标div时, handlerIn将仅触发一次(如在console.log('show')调用中所见)。 The handlerOut will also be executed only one time, when the mouse leaves the target div . 当鼠标离开目标div时, handlerOut也将仅执行一次。

On the example below, when the mouse hover #div-b the #div-a content will be visible and when it leaves, the content will be hidden: 在下面的示例中,当鼠标悬停在#div-b#div-a内容将可见,而当它离开时,该内容将被隐藏:

 $(function() { $('#div-b').hover( function() { console.log('show'); $('#div-a').show(); }, function() { console.log('hide'); $('#div-a').hide(); } ); }); 
 #div-a { display: none; padding: 20px; } .wrapper { margin: auto; background-color: darkcyan; color: white; height: 100px; margin-bottom: 10px; } #div-b { padding: 20px; height: 50px; background-color: black; color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div id="div-a"> I'm visible! </div> </div> <div id="div-b"> Hover me </div> 

Use mouseEnter to set a flag (or just show) and on mouseLeave set the flag to the opposite value (or just hide). 使用mouseEnter设置标志(或仅显示),并在mouseLeave上将标志设置为相反的值(或仅隐藏)。 mouseMove may not be best event since it may be fired more than desired. mouseMove可能不是最好的事件,因为它可能被触发过多。 You may not even keep track of a flag if you just want to show and hide an element. 如果您只想显示和隐藏元素,甚至可能无法跟踪标志。

First of All , i let you inspect source code of $.fn.show , then , see , at the end , my answer : 首先,我让您检查$.fn.show源代码 ,然后看最后的答案:

show: function() {
        return showHide( this, true );
    }

And showHide source code is : showHide源代码是:

function showHide( elements, show ) {
    var display, elem, hidden,
        values = [],
        index = 0,
        length = elements.length;

    for ( ; index < length; index++ ) {
        elem = elements[ index ];
        if ( !elem.style ) {
            continue;
        }

        values[ index ] = dataPriv.get( elem, "olddisplay" );
        display = elem.style.display;
        if ( show ) {

            // Reset the inline display of this element to learn if it is
            // being hidden by cascaded rules or not
            if ( !values[ index ] && display === "none" ) {
                elem.style.display = "";
            }

            // Set elements which have been overridden with display: none
            // in a stylesheet to whatever the default browser style is
            // for such an element
            if ( elem.style.display === "" && isHidden( elem ) ) {
                values[ index ] = dataPriv.access(
                    elem,
                    "olddisplay",
                    defaultDisplay( elem.nodeName )
                );
            }
        } else {
            hidden = isHidden( elem );

            if ( display !== "none" || !hidden ) {
                dataPriv.set(
                    elem,
                    "olddisplay",
                    hidden ? display : jQuery.css( elem, "display" )
                );
            }
        }
    }

    // Set the display of most of the elements in a second loop
    // to avoid the constant reflow
    for ( index = 0; index < length; index++ ) {
        elem = elements[ index ];
        if ( !elem.style ) {
            continue;
        }
        if ( !show || elem.style.display === "none" || elem.style.display === "" ) {
            elem.style.display = show ? values[ index ] || "" : "none";
        }
    }

    return elements;
}

So : 因此:

You don't need to check if it is hidden or not $().is(':hidden') because it is already checked in show function (See : if ( elem.style.display === "" && isHidden( elem ) ) { .... ) 您不需要检查它是否已隐藏$().is(':hidden')因为它已在show函数中检查过(请参阅: if ( elem.style.display === "" && isHidden( elem ) ) { ....

A better solution would be to use a mouseEnter and mouseLeave like this snippet below: 更好的解决方案是像下面的代码片段一样使用mouseEntermouseLeave

This is in javaScript but will give you a better idea. 这在javaScript中,但是会给您一个更好的主意。 If you want it in JQuery and having problems with it I can write it on Pluncker for you. 如果您希望在JQuery中使用它并且遇到问题,我可以在Pluncker上为您编写。 Hope this will help 希望这会有所帮助

  function show_hide(id) { var e = document.getElementById(id); if (e == null){ } else { if (!e.classList.contains('showClass')) e.className += " showClass"; else e.className = "myclass"; } } 
 .myclass { opacity: 0; margin-top: 25px; font-size: 21px; text-align: center; -webkit-transition: opacity 1s ease-in; -moz-transition: opacity 1s ease-in; -o-transition: opacity 1s ease-in; -ms-transition: opacity 1s ease-in; transition: opacity 1s ease-in; } .showClass{ opacity: 1 } 
 <div onmouseover="show_hide('deletebutton')" onmouseout="show_hide('deletebutton')"> // image <div class="myclass" id="deletebutton">DELETE</div> </div> 


Updated 更新

 function show() { $("#deletebutton").show(); } function hide() { $("#deletebutton").hide(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div onmouseover="show()" onmouseout="hide()"> // image <div class="myclass" id="deletebutton" style="display:none;">DELETE</div> </div> 

Can't you use .hover() and .toggle() functions? 您不能使用.hover().toggle()函数吗? is mousemove must? mousemove必须的吗?

https://jsfiddle.net/tvwpdxum/1/ https://jsfiddle.net/tvwpdxum/1/

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

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