【问题标题】:How can I get the width of a link be only the width of the text's width?如何让链接的宽度仅为文本宽度的宽度?
【发布时间】:2014-12-23 21:27:10
【问题描述】:

我设法编写了一个函数,其中链接的信息将在将鼠标悬停在链接的标题上时淡入,但是每当我将鼠标悬停在标题文本的剩余空间上时,它都会触发链接的信息淡入-在。有没有办法只获得链接标题的激活宽度?我也在寻找纯 CSS 的解决方案。如果您能想到一种更有效的方法来实现这一点,请告诉我:)

Fiddle here.

这是问题的可视化,因为我不太熟悉代码/开发术语:

非常感谢!

HTML:

<div class="page">
    <div class="text">
        <span class="title">Title &#40;More&#41;</span>
        <span class="info">
            Title Description over here
        </span>
    </div>
    <div class="image">
        <img src="..." alt="" />
    </div>
</div>

CSS:

html, body {
    margin: 0;
    padding: 0;
    width:100%;
    height:100%;
}
body {
    font-family: helvetica;
    font-size: 18px;
    line-height: 26px;
}
div.page {
    width:80%;
    max-width:960px;
    margin:0 auto;
    height:100%;
}
div.text {
    width:80%;
    max-width:960px;
    margin:0 auto;
}
div.text span.title {
    -webkit-transition: all .5s; 
    transition: all .5s;
}
div.text:hover span.title {
    opacity: 0;
    visibility: hidden;
}
div.text span.info {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;  
    opacity: 0;
    visibility: hidden;
    -webkit-transition: all .5s; 
    transition: all .5s;
}
div.text:hover span.info {
    opacity: 1;
    visibility: visible;
}
.image img {
    width: 100%;
    height: auto;
    padding-top: 10px;
}
.text{
    position: absolute;
    z-index: 9999;
}
.image{
    position:relative;
    top:20px;
}

【问题讨论】:

    标签: html css hover


    【解决方案1】:

    这是因为您的悬停事件在 div 上,这是一个块级 (width:100%) 元素。

    您需要将其设置为显示内联或自动宽度,这样它就不会占据整个宽度,然后在悬停时恢复:

    html,
    body {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
    }
    body {
      font-family: helvetica;
      font-size: 18px;
      line-height: 26px;
    }
    div.page {
      width: 80%;
      max-width: 960px;
      margin: 0 auto;
      height: 100%;
    }
    div.text {
      display: inline-block;
      max-width: 960px;
      margin: 0 auto;
    }
    div.text:hover {
      display: block;
      width: 80%;
    }
    div.text span.title {
      -webkit-transition: all .5s;
      transition: all .5s;
    }
    div.text:hover span.title {
      opacity: 0;
      visibility: hidden;
    }
    div.text span.info {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      opacity: 0;
      visibility: hidden;
      -webkit-transition: all .5s;
      transition: all .5s;
    }
    div.text:hover span.info {
      opacity: 1;
      visibility: visible;
    }
    .image img {
      width: 100%;
      height: auto;
      padding-top: 10px;
    }
    .text {
      position: absolute;
      z-index: 9999;
    }
    .image {
      position: relative;
      top: 20px;
    }
    <div class="page">
      <div class="text">
        <span class="title">Title &#40;More&#41;</span>
        <span class="info">
                Title Description over here<br/>
                More text<br/>
                More text<br/>
                And you know, text.<br/>
            </span>
      </div>
      <div class="image">
        <img src="http://www.graphicthoughtfacility.com/media/uploads/images/FW-SmallTray-WEB.jpg" alt="" />
        <img src="http://www.graphicthoughtfacility.com/media/uploads/images/FW-LargeBox-WEB.jpg" alt="" />
        <img src="http://www.graphicthoughtfacility.com/media/uploads/images/Formwork1_RGB-WEB.jpg" alt="" />
      </div>
    </div>

    【讨论】:

    • 你有一个小故障效果......悬停并向下移动你会看到的光标
    • @Danko - 很容易纠正,但很难知道 OP 提供的信息的最佳方法。
    • 感谢@SW4 的建议——但恐怕 Danko 是对的,您的更改直接影响了描述。
    • @SW4 啊,有没有比我建议的更好的方法?我一直难以执行这个想法,所以我非常愿意接受更好的方法来实现这一点。功能非常简单(理论上是这样)。当用户将鼠标悬停在标题上时,文本会被描述交换——当用户悬停在描述之外时,文本会淡出回到标题。
    【解决方案2】:

    div.text 的宽度为 80%,因此您可以将鼠标悬停在其右侧。给div.text 一个更小的宽度,给span.info 更大的宽度。

    div.text {
        width:15%;
        max-width:960px;
        margin:0 auto;
    }
    
    div.text span.info {
        width: 250px;       
        position: absolute;
        top: 0;
        right: 0;
        left: 0;  
        opacity: 0;
        visibility: hidden;
        -webkit-transition: all .5s; 
        transition: all .5s;
    }
    

    See Fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      相关资源
      最近更新 更多