【问题标题】:How to position an image above the content如何在内容上方放置图像
【发布时间】:2013-12-13 11:54:40
【问题描述】:

我正在尝试将图像放置在文本上方(不是之前)。以下是我到目前为止的代码。我还发布了我得到的结果和我想要达到的结果。我的目标是,当用户将鼠标悬停在<p> 上方时,我想在其上方显示图像,这意味着我需要使用绝对位置,因为我不希望将任何东西移动到其他任何地方,我只想在上方显示图像文本。

注意:图片没有定义高度,可能更长或更短。

 <div id="wrap" style="position:relative; height: 100px; width: 500px; background: red">
        <img src="img/cat.png" style="position:absolute; ">
        <p style="position:absolute; ">Some text here..</p>
</div>

这是我得到的结果,

这是我想要的结果。注意:我不想移动任何东西,我只想将图像悬停在文本上方,因此我不需要在任何地方移动任何东西。

【问题讨论】:

  • z-index:1000000;风格

标签: javascript jquery css position absolute


【解决方案1】:

工作示例:http://jsfiddle.net/Rhcp5/2/

HTML:

<div id="wrap">
        <img src="img/cat.png">
        <p>Some text here..</p>
</div>

CSS:

#wrap {
position:relative; 
height: 100px; 
width: 500px; 
background: red;
}

#wrap img {
display: none;
position: absolute;
bottom: 100%;
margin-bottom: 5px;
}

#wrap:hover img {
display: block;
}

这将处理可变高度的图像。

【讨论】:

  • 看看我给 David Aguirre 的回复,你发布了同样的答案
  • 试试看。这不是同一个答案。它处理可变高度的图像。
  • 你救了我的命,兄弟!
【解决方案2】:

给容器“包装”一个“相对”的位置 然后使用“绝对”定位图像,然后使用负“顶部”

<div id="wrap" style="position:relative; height: 100px; width: 500px; background: red">
    <img src="img/cat.png" style="position:absolute; ">
    <p style="position:absolute; ">Some text here..</p>
</div>

这是css

#wrap {
    position: relative;
}

#wrap img:hover {
    position: absolute;
    top: -50px;
    left: 0;
}

如果您不知道图像的高度,请使用 Jquery。未测试。

var imageHeight = $("#imgID").height();
$('#imgID').hover(function() {
    $('#imgID').css({
       'position': 'absolute',
       'top': '-' + imageHeight,
       'right': '0'
    }) 
});

【讨论】:

  • 这可行,但如果图像高度如此之大,那么 top: -50px 将无用。就像我说的,图像没有定义高度
  • 你不应该让图像超过特定的高度。为您的图像使用“最大高度”,使其不超过特定高度。
  • true,但是如果我将最大高度设置为 100 像素,并且图像低于 100 像素(如 40 像素),那么图像将远离文本。
  • 添加了一些可以解决问题的 JQuery。虽然没有经过测试,但它应该可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多