线路位移与fadein

时间:2014-04-14 00:42:41

标签: javascript css

我有点坚持创建一个字符串的淡入淡出。我想要做的是当用户将鼠标悬停在字符串上时,会在字符串旁边显示一个条形。像这样:

| string

但问题是,当|出现时,字符串向右移位,我将如何解决此问题?这是我目前的CSS和Java代码。

.details{
    font-size: 2.1em;
    font-weight: bold;
    line-height: 5em;
    /*display:none;*/
}

.line{
    border-style:solid;
    border-left: double-thick;
    border-color: #336699;
    display: none;
    margin-right: .4em;
}

JS:

$(document).ready(function(){
    $('.details').mouseenter(function(){
        $('.line').remove();
        $(this).prepend('<a class="line"></a>');
        $('.line').fadeIn(250);
    });
    $('.details').mouseleave(function(){
        $('.line').fadeOut(250);
    });
});

2 个答案:

答案 0 :(得分:0)

假设每一行(.line和.details的父元素)是相对定位的,请尝试使用

.details{
font-size: 2.1em;
font-weight: bold;
line-height: 5em;
position: relative;
left: 10px; /*play with this*/
/*display:none;*/
}

.line{
border-style:solid;
border-left: double-thick;
border-color: #336699;
position: absolute; /* */
left: 0px; /* */
display: none;
margin-right: .4em;
}

这将推送细节元素10px或者需要多少空间,同时防止线元素推送细节

答案 1 :(得分:0)

完成没有任何JavaScript,你能相信吗?

Fiddle

p::before {
    content: '|';
    position: absolute;
    margin-left: -5px;
    opacity: 0;
    transition: opacity 250ms;
}

p:hover::before {
    opacity: 1;
}

啊,CSS3的力量