简体   繁体   English

简单的jQuery fadeIn fadeOut动画 - 如何显示/隐藏DIVS?

[英]Simple jQuery fadeIn fadeOut animation - How to show / hide DIVS?

SCENARIO: Have 2 links inside a list of ul, li: MY INFO and LOG IN One link must show a box (with fadeIn animation ) with a form inside when used HOVER on the link. 场景:在ul,li列表中有2个链接:MY INFO和LOG IN一个链接必须在链接上使用HOVER时显示一个带有表格的框(带有fadeIn动画)。 Other link shows a button inside a box (with fadeIn animation) when you HOVER on the link. 当您在链接上进行操作时,其他链接会在框内显示一个按钮(带有淡入淡出动画)。

The form must fadeOut when user MOUSEOUT of the link "LOG IN" OR mouseout of the form. 当用户MOUSEOUT链接“LOG IN”或鼠标输出表格时,表格必须fadeOut。 The box with button, must fadeOut when user MOUSEOUT of the link "MY INFO" OR mouseout the box. 带有按钮的框,当用户MOUSEOUT链接“MY INFO”或鼠标移动框时必须fadeOut。

PROBLEM (same for both): The form disapears when MOUSEOUT link MY INFO. 问题(两者都相同):当MOUSEOUT链接MY INFO时,表单消失。 The button inside the div disapears when MOUSEOUT link LOG IN. 当MOUSEOUT链接LOG IN时,div内的按钮消失。

NOTE: 1/The form MUST BE VISIBLE WHEN MOUSE IS ON THE LINK OR ON THE FORM 2/The box with button MUST BE VISIBLE WHEN MOUSE IS ON THE LINK OR ON BOX with button 注意:1 /当小鼠在链接上或表格上时,表格必须是可见的2 /当小鼠在链接上时,带按钮的盒子必须是可见的,或者在按钮上有按钮

REFERENCE: check www.conforama.fr on the very-top of the screen, link is "Compte" with an icon, it has a class: "with-hover-menu" . 参考:在屏幕的最上方查看www.conforama.fr,链接是带有图标的“Compte”,它有一个类:“with-hover-menu”。 When you mouseover it, the form appears. 将鼠标悬停在其上时,将显示该表单。 When you mouseout the link OR the form, the form disapears. 当您将链接或表单删除时,表单会消失。 I need the same but with fadeIn. 我需要相同但有fadeIn。

Right now you can look at the code below in this jsfiddle: http://jsfiddle.net/75HYL/19/ but it doesnt work at all. 现在你可以看看这个jsfiddle中的代码: http//jsfiddle.net/75HYL/19/但它根本不起作用。 I don't know how to achieve this. 我不知道如何实现这一目标。 Would like to understand and learn...!! 想了解和学习...... !!

<ul class="links">
    <li class="classA"><a><span>My info</span></a></li>
    <li class="classB"><a><span>Log in</span></a></li>
</ul>

<div id="userInfo">USER MUST BE ABLE TO CLICK THE BUTTON BELOW, SO THIS BOX MUST STAY VISIBLE<br/>
    <input type ="button" value="OKAY"/>
    <div id="login" >
        <div class="form">
            <form>
                <input type="textfield" value="enter your email"></input>
                <input type="checkbox"><option>remember me? </option><input>
                <input type="button" value="Click me"/>
            </form>
        </div>
    </div>
</div>
<style>
    .links li { display:inline-block;cursor:pointer; }
    .links li { padding:0 4px 0 1px; }
    .links li.classA {width:147px; height:77px;background:url(../images/sprites01.png) no-repeat -226px 0px;}
    .links li.classB {width:147px; height:77px;background:url(../images/sprites01.png) no-repeat -226px 0px;}
    .links li.classA span {}
    .links li.classB span {}
    .links li.classA:hover {background:url(../images/sprites01.png) no-repeat -226px -80px;}
    .links li.classB:hover {background:url(../images/sprites01.png) no-repeat -226px -80px;}
    .links li.classA a {color:#fff;text-transform:uppercase;background:#00aaff;padding:5px 5px 0 20px;margin:5px 0 0;font-size:1em;height:50px;display:block;}
    .links li.classB a {color:#00aaff;text-transform:uppercase;background:orange;padding:5px 5px 0 20px;margin:5px 0 0;font-size:1em;height:50px;display:block;}
    #login {display:none;width:250px;height:250px; background:#bbb;color:#000;border:1px solid red;}
    #userInfo{display:none;width:250px;height:250px; background:#bbb;color:#000;border:1px solid red;}
</style>

<script>
    $("li.classA").hover(function() {
        $("#userInfo").fadeIn('fast').css('display', 'block');
    });
    $("#login, .classA").mouseleave(function() {
        $("#userInfo").fadeOut('fast').css('display', 'none');
    });

    $("li.classB").hover(function() {
        $("#login").fadeIn('fast').css('display', 'block');
    });
    $("#login, .classA").mouseleave(function() {
        $("#login").fadeOut('fast').css('display', 'none');
    });
</script>

A bit like this ? 有点像这样 Cleaned up a few syntax errors and change logic a bit 清除了一些语法错误并稍微改变了逻辑

This is what you want? 这是你想要的?

http://jsfiddle.net/WcUFe/3/ http://jsfiddle.net/WcUFe/3/

I've only altered the js, to be easier to see the differences. 我只改变了js,更容易看出差异。 For an easier life the cssand/or html should be altered, and all the code could be put in a separate plugin, that will control the whole show. 为了更轻松的生活,应该更改cssand /或html,并且所有代码都可以放在一个单独的插件中,这将控制整个节目。

Basically i use a timer to allow the user ~100ms to move the mouse from the LI element to the displayed container.... all the rest is cruft to maintain the state and to make sure we never have the 2 containers visible at any moment. 基本上我使用一个计时器允许用户~100ms将鼠标从LI元素移动到显示的容器....所有其余部分都是为了保持状态并确保我们在任何时候都看不到2个容器。

Well your problem is that the mouse currently HAS to leave your links to get to the boxes. 那么你的问题是,鼠标目前还没有离开你的链接到框。 Therefore the boxes MUST dissapear for the functionality to work as you have stated above. 因此,如上所述,框必须消失,以使功能正常工作。

A few solutions come to mind (I'm not great with jQuery so there might be better options); 我想到了一些解决方案(我对jQuery不是很好,所以可能有更好的选择); 1 is to add a bool and some logic to say if the box has opened and then only listen for mouseout events on the box (rather than the link and the box). 1是添加一个bool和一些逻辑来说明盒子是否已经打开然后只监听盒子上的mouseout事件(而不是链接和盒子)。 And another is to debounce the events so that there's a bit of a time delay before you listen for the mouseout event of the link. 另一个是去抖动事件,以便在你监听链接的mouseout事件之前有一点时间延迟。

  • looks like @AbstractChaos has cleared this up now, as I suspected there is a better option to my suggestions! 看起来像@AbstractChaos现在已经解决了这个问题,因为我怀疑我的建议有更好的选择!

Hope this helps. 希望这可以帮助。

or 要么

$("li.classA").hover(function() {
    $("#login").css('display', 'none');
    $("#userInfo").fadeIn('fast');
});
$("#userInfo").mouseleave(function() {
    $("#userInfo").fadeOut('fast');
});


$("li.classB").hover(function() {
    $("#userInfo").css('display', 'none');
    $("#login").fadeIn('fast');
});
$("#login").mouseleave(function() {
    $("#login").fadeOut('fast');
});

Try this: http://jsfiddle.net/QR49h/ 试试这个: http//jsfiddle.net/QR49h/

I moved the divs to be hidden/shown to inside the <li> elements: 我将div移动隐藏/显示在<li>元素内:

<ul class="links">
    <li class="classA"><a><span>My info</span></a>
        <div id="userInfo">USER MUST BE ABLE TO CLICK THE BUTTON BELOW, SO THIS BOX MUST STAY VISIBLE
            <br/>
            <input type="button" value="OKAY" />
        </div>
    </li>
    <li class="classB"><a><span>Log in</span></a>
        <div id="login">
            <div class="form">
                <form>
                    <input type="textfield" value="enter your email" />
                    <input type="checkbox" />
                    <option>remember me?</option>
                        <input />
                    <input type="button" value="Click me" />
                </form>
            </div>
        </div>
    </li>
</ul>

I also fixed a bug in the js (ClassA vs ClassB) and fiddled the css slightly so the second <li> item can keep it's position when the first is expanded. 我还修复了js中的一个错误(ClassA vs ClassB)并稍微摆弄了css,这样第二个<li>项目可以在第一个扩展时保持它的位置。 Oh and fixed some element closing issues in the form div. 哦,并修复了表单div中的一些元素关闭问题。

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

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