简体   繁体   English

FadeIn不适用于空DIV

[英]FadeIn doesn't work with empty DIV

I have button that shows a password by using fadein and fadeout. 我有一个按钮,可以使用淡入和淡出显示密码。 However, when the password is an empty string, the div doesn't fade-in. 但是,当密码为空字符串时,div不会淡入。

Html HTML

<div>    
    <div>********</div>
    <div class="password"></div>
    <input type="button" class="showPasswordButton" value="Show Password">
</div>

Jquery jQuery的

   $(".showPasswordButton", false).on("click", function () {   
    var passwordDiv = $(".password");
    passwordDiv.text("");
    passwordDiv.fadeIn(500).delay(3000).fadeOut(500, function () {
        passwordDiv.empty();
    });
});

Here is a JSfiddle link to play around with. 这是一个可玩的JSfiddle链接

If I replace the empty string in passwordDiv.text("") with any value except whitespace, the fade-in will work. 如果我用除空格以外的任何值替换passwordDiv.text(“”)中的空字符串,则淡入将起作用。 I've gotten around the issue by using a Japanese full-width whitespace character. 我已经通过使用日语全角空格字符解决了这个问题。 However, I'd like to get this working without an obvious hack. 但是,我希望没有明显的漏洞就可以使它正常工作。 Is there something wrong I'm doing here or a way to get it so passwordDiv will fade-in when the div text is an empty string? 我在这里做某事还是某种获取它的方法,以便当div文本为空字符串时passwordDiv会淡入?

It's working 工作正常

the matter of fact is that you can't see it. 事实是您看不到它。 Since by <div> contain nothing, hence it does not take space in the HTML. 由于by <div>包含任何内容,因此它不会在HTML中占用空间。

You can see your web console and you will find that opacity of the div get changed 您可以看到您的Web控制台,并且会发现div的不透明度发生了变化

JUST try adding the below height and width and you can see the change 只需尝试添加以下高度和宽度,即可看到更改

.password {
    height: 500px;
    width: 500px;
    background-color: #ff0000;  // background of the div, so that you can see the change
}

Other way is not to empty the <div> , just have a blank correct inserted in to the div , so that the <div> take some 另一种方法是不清空<div> ,只需将一个正确的空白插入div中,以便<div>占用一些

        passwordDiv.fadeIn(500).delay(3000).fadeOut(500, function () {
           passwordDiv.html('&nbsp;'); // inserting a blank correct , so that actually the div take some space
        });

your div is empty , so you can set border or something to see effect . 您的div是空的,因此您可以设置边框或其他效果。

.password{
width:50px;
    height:50px;
    border:solid 2px red;
}

DEMO 演示

Here is the jsfiddle i have create for you http://jsfiddle.net/Tushar490/LkxLzhmt/9/ 这是我为您创建的jsfiddle http://jsfiddle.net/Tushar490/LkxLzhmt/9/

you need to add dimensions(width,height) for the div having "password" class .As you want to see that fading effect for the empty string too , you need set the dimensions as well. 您需要为具有“ password”类的div添加尺寸(宽度,高度)。要查看空字符串的褪色效果,还需要设置尺寸。 What happen is when you give text to that div , the div takes width:auto and height:auto .But when you don't give any text to that div then width:auto and height:auto will show you nothing and that's purely logical . 发生的情况是,当您向该div输入文本时,该div会使用width:auto和height:auto。但是当您不向该div输入任何文本时,那么width:auto和height:auto将不会显示任何内容,这完全是逻辑。

just a CSS you want , and nothing else :- 只是您想要的CSS,而别无其他:-

.password{
display:none;
width:100px;
height:15px;
}

Hope my answer help you !! 希望我的回答对您有帮助!

You can also do this for showing empty string fade in-fade out effect :- 您也可以这样做,以显示空的字符串淡入淡出效果:-

$(".showPasswordButton", false).on("click", function () {   
var passwordDiv = $(".password");
passwordDiv.html("<br>");
passwordDiv.fadeIn(500).delay(3000).fadeOut(500, function () {
    passwordDiv.empty();
});
});

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

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