简体   繁体   English

在不更改 position 的情况下淡入和淡出内联 div?

[英]Fading In and out inline div's without changing the position?

The question might be a little bit confusing.这个问题可能有点令人困惑。

I have an inline div and the first one is meant to be a close/delete button.我有一个内联 div,第一个是关闭/删除按钮。 I'm trying to fadeIn the close button on hover, although when it's faded out the rest div's move to the left.我正在尝试淡入 hover 上的关闭按钮,尽管当它淡出时 rest div 向左移动。

How would you prevent this?你将如何防止这种情况发生?

Here is a fiddle: http://jsfiddle.net/x2SEv/这是一个小提琴: http://jsfiddle.net/x2SEv/

Hover on top of 'Some text goes here' Hover 位于“这里有一些文字”

You'll have to absolutely position the element that you're fading, and move your text in a bit: http://jsfiddle.net/x2SEv/1/ .您必须绝对 position 您正在褪色的元素,并稍微移动您的文本: http://jsfiddle.net/x2SEv/1/

.parent {
    position: relative;
    padding-left: 40px;
}
.div-to-fade {
    display:none;
    position: absolute;
    left:0; top:0;
}

jQuery functions .show() and .hide() , use the CSS attribute display . jQuery 函数.show()和 .hide( .hide() ,使用 CSS 属性display

When attribute display is set to none, the space filled by HTML element which is hidden is remove.当属性显示设置为无时,被隐藏的 HTML 元素填充的空间被删除。 So your second div move to left.所以你的第二个 div 向左移动。

You have to use the CSS attribute visibility .您必须使用 CSS 属性visibility It does the same thing that display , but it keeps the space filled by HTML.它与display做同样的事情,但它保持由 HTML 填充的空间。

Let's try:我们试试看:

In your JS:在你的 JS 中:

$(".todo").mouseover(function() {
        $('.closebtn').css("visibility", "visible");
    });
    $(".todo").mouseout(function() {
        $('.closebtn').css("visibility", "hidden");
    });

And your CSS:还有你的 CSS:

.closebtn, .actions{visibility:hidden;}

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

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