【问题标题】:CSS hover to stay on mouse overCSS悬停以保持鼠标悬停
【发布时间】:2013-11-26 17:59:34
【问题描述】:

好的,如果你可以去的话;

http://jsfiddle.net/aled2305/UzM7U/4/

您会看到一个蓝色圆圈,当您将鼠标移到右侧时会出现一个红色方块。现在一切都按照我想要的方式进行了,但是我希望当用户将鼠标悬停在它上面时,红色框会保留下来。

现在,如果您将鼠标移到红色方块显示的位置,它会显示,因为

.down:hover
{
    opacity:100;
}

那么有没有办法让红色方块在鼠标悬停时保持不变,但只有在将鼠标悬停在蓝色圆圈上时才会激活它。

提前致谢

阿莱德

更新

抱歉,我想在鼠标移开后隐藏红色方块。

谢谢

【问题讨论】:

  • 我在示例中绝对看不到“红色方块”。
  • 你的 Fiddle 没有 JavaScript,也没有 CSS hover 选择器 - 我想你错过了一些东西......
  • 对不起各位,编辑后忘记更新代码了……链接已更新
  • 据我所知,您将需要 javascript。这是一个基本功能的小提琴,jsfiddle.net/Josh_Powell/UzM7U/7
  • 拜托,请参阅 W3C w3.org/wiki/CSS/Properties/opacity 上的此链接,特别是部分 =>“0.0(完全透明)到 1.0(完全不透明)范围之外的任何值都将被限制在此范围内。”没有不透明度:100

标签: html css hover opacity fade


【解决方案1】:

我的演示将在 circle 上悬停时淡入 square。从那里开始,当您将鼠标悬停在 正方形 上时,它将保持不透明。离开圆形方形后,方形会淡出。

让它工作的诀窍是为 squareopacityheightwidth 属性设置 2 个不同的转换,一个用于 hover ON 和一个用于 hover OFF,以及为过渡添加一个delay 属性。转换heightwidth 的原因是,它会阻止您在未先将鼠标悬停在上的情况下将鼠标悬停在正方形上。

方块的默认设置如下:opacity: 0height: 0width: 0

对于 hover ON 过渡,您希望 opacity 淡入超过 1 秒,但要能够看到,heightwidth 值需要为 40px在淡入过渡之前。为此,您需要在heightwidth 转换上设置0 秒的延迟。这样,正方形会立即处于其最大尺寸,从而可以看到淡入过渡。

hover OFF 过渡将恢复为默认设置。您希望opacity 在 1 秒内缓出,同时将 heightwidth 的值保持在 40px。否则,heightwidth 将立即恢复为 0,您将无法看到淡出过渡。要做到这一点,您需要在 heightwidth 转换上设置 1 秒的延迟。这样做时,opacity 会减少 1 秒以上,并且由于 heightwidth 的 1 秒延迟,此时,heightwidth 将恢复为 0。

See the jsFiddle demo


HTML

<div id="gravatar">
    <div id="circle"></div>
    <div id="square"></div>
</div>

CSS

#gravatar
{
    float: left;
}

#circle
{
    background-color: blue;
    float: left;
    height: 40px; 
    width: 40px;
    border-radius: 20px;
}

#square
{
    background-color: red;
    float: left;
    margin-left: 10px;
    height: 0;
    width: 0;
    opacity: 0;

    /* hover OFF */
    -webkit-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
    -moz-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
    -o-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
    -ms-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
    transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
}

#square:hover,
#circle:hover + #square
{
    height: 40px;
    width: 40px;
    opacity: 1;

    /* hover ON */
    -webkit-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -moz-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -o-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -ms-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
}


编辑

OP 留下了一条评论,指出向 square 添加内容会阻止转换正常工作。我通过将overflow: hidden 添加到正方形来更正它。

我还在 CSS 中添加了其他样式来说明 OP 添加的锚点。

See the jsFiddle demo


HTML

<div id="gravatar">
    <div id="circle"></div>
    <div id="square">
        <a href="http://techyoucation.com/?page_id=156">Profile Details</a> 
        <a href="http://techyoucation.com/?page_id=59">Account Details</a>
    </div>
</div>

CSS

#gravatar
{
    float: left;
}

#circle
{
    background-color: blue;
    float: left;
    height: 40px; 
    width: 40px;
    border-radius: 20px;
}

#square
{
    background-color: #2D3538;
    float:left;
    overflow: hidden;
    margin-left: 10px;
    height: 0;
    width: 0;
    opacity: 0;
        
    /* hover OFF */
    -webkit-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
    -moz-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
    -o-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
    -ms-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
    transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
}

#square > a
{
    display: block;
    font: 15px Verdana;
    color: #FFF;
    text-decoration: none;
    height: 15px;
    line-height: 15px;
    margin: 10px;
}

#square > a:last-child
{
    margin-top: 0;
}

#square > a:hover
{
    text-decoration: underline;
}

#square:hover,
#circle:hover + #square
{
    height: 60px;
    width: 135px;
    opacity: 1;
    
    /* hover ON */
    -webkit-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -moz-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -o-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -ms-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
}

【讨论】:

  • 1+ .. 比其他答案好。
  • @Scott 几乎完美,只是有什么改变可以让它像 MathiasaurusRex 一样淡出吗?感谢和抱歉 JoshC 而不是 Scott 回复你!
  • @Scott 抱歉,我已阅读该帖子但无法将其淡出?
  • @aled2305 - 你去!我得到了一切如你所愿的工作。我已经用完整的解决方案更新了我的答案。阅读我的回答,了解更改背后的原因。
  • @Scott 非常感谢你......你不会相信我是多么感激。谢谢:)
【解决方案2】:

Here's a Fiddle Using JS that follows the following logic:

  1. 悬停在蓝色圆圈上时显示红色框
  2. 当鼠标离开 Reds 时,Red Box 会隐藏

您可以通过添加一点 JQuery 并修改您的 CSS 来获得该效果:

JQuery:

$(".gravatar").hover(
  function () {
    $(".down").addClass('hoverDown');
  }
);

$(".down").mouseleave(
  function () {
    $(".down").removeClass('hoverDown');
  }
);

这是 CSS:

.gravatar {
    background-color:blue;
    float: left;
    margin-top: 2px;
    margin-right: 10px;
    margin-left:  2px;
    border-radius: 20px;
    width: 40px;
    height: 40px; 
}

.down
{
    float:left;
    width: 40px;
    height: 40px;
    background-color:Red;
    opacity:0;
    -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    -ms-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;


    }
.hoverDown
{
    opacity:1;

}

【讨论】:

  • 这行得通,但我没有看到 OP 在哪里提到他想要 jQuery。
  • @Scott 你说得对,斯科特我只想使用 CSS 和 HTML……这可能吗?
  • 抱歉忘了说我想在鼠标移开后隐藏红色方块。
  • 人们不会简单地假设 opacity 100 存在
  • @aled2305 等一下,如果您希望鼠标离开圆圈时效果消失,那么:hover 是怎么回事?
【解决方案3】:

您可以使用 javascript 或 CSS3 动画,这里是 CSS3 动画的示例...Make CSS Hover state remain after "unhovering"

CSS 2.1 不能做你想做的事。

【讨论】:

    【解决方案4】:

    对于鼠标移入和移出操作,您可以使用 mouseenter mouseleave jquery 函数

    $(".gravatar").mouseenter(
      function () {
        $(".down").addClass('hoverDown');
      }
    );
    $(".gravatar").mouseleave(
      function () {
        $(".down").removeClass('hoverDown');
      }
    );
    

    工作小提琴:

    http://jsfiddle.net/UzM7U/9/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2011-08-04
      • 2018-09-03
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2011-02-06
      相关资源
      最近更新 更多