鼠标悬停以使div显示在图像的边框内

时间:2021-03-22 23:43:18

When I hover over an img within div.hover-group, the div div.hover-toggle becomes visible, and becomes invisible when the mouse leaves the image. The div.hover-toggle should appear within the area of the img it belongs to, aligned with its bottom border and each of the button.btns of equal width.

当我将鼠标悬停在div.hover-group中的img上时,div div.hover-toggle变为可见,当鼠标离开图像时变为不可见。 div.hover-toggle应出现在它所属的img区域内,与其下边框和每个宽度相等的button.btns对齐。

To illustrate:

no mouseover:

|-----------|
|           |
|    img    |
|           |
|           |
|           |
|-----------|

is mouseover:

|-----------|
|           |
|    img    |
|           |
|-----------|
| 1 | 2 | 3 |
|-----------|

I have already accomplished the hover toggling using jQuery:

我已经使用jQuery完成了悬停切换:

$(selector1).mouseover(function(){$(selector2).hide();});
$(selector1).mouseout(function(){$(selector2).show();});

However, have been unable to align div.hover-toggle with the bottom of the image, as illustrated, it appears below the image instead.

但是,无法将div.hover-toggle与图像底部对齐,如图所示,它显示在图像下方。

How can this be done?

如何才能做到这一点?

Thanks!


The DOM I am manipulating above appears below. It uses twitter bootstrap.

我正在操作的DOM如下所示。它使用twitter bootstrap。

<ul class="thumbnails">
    <li class="span3">
        <div class="hover-group thumbnail">
            <img src="http://placehold.it/260x180" alt="">
            <div class="hover-toggle btn-group">
                <button class="btn">1</button>
                <button class="btn">2</button>
                <button class="btn">3</button>
            </div>
            <h5>Thumbnail label</h5>
            <p>Thumbnail caption right here...</p>
        </div>
    </li>
...
</ul>

2 个解决方案

#1


6  

There is no need for JS here, you can accomplish everything with simple CSS and a little markup change (I wrapped the image and the .hover-toggle div in a .image-wrapper div):

这里不需要JS,你可以通过简单的CSS和一点标记更改完成所有事情(我将图像和.hover-toggle div包装在.image-wrapper div中):

<ul class="thumbnails">
    <li class="span3">
        <div class="hover-group thumbnail">
            <div class="image-wrapper">
              <img src="http://placehold.it/260x180" alt="">
              <div class="hover-toggle btn-group">
                  <button class="btn">1</button>
                  <button class="btn">2</button>
                  <button class="btn">3</button>
              </div>
            </div>
            <h5>Thumbnail label</h5>
            <p>Thumbnail caption right here...</p>
        </div>
    </li>
...
</ul>

Now the styling it needs is simple:

现在它需要的样式很简单:

.hover-group .image-wrapper {
  /* We need this to make the .hover-toggle div relative to .image-wrapper */
  position: relative;
}

.hover-group .image-wrapper .hover-toggle {
  /* set it at the bottom of .image-wrapper */
  position: absolute;      
  bottom: 0;
  /* and don't display it */
  display: none;
}

.hover-group .image-wrapper:hover .hover-toggle {
  /* only display it when .image-wrapper is being hovered on */
  display: block;
}

If you want it to show when you hover over the whole .hover-group, use this CSS instead of the last line:

如果您希望将鼠标悬停在整个.hover-group上时显示,请使用此CSS代替最后一行:

.hover-group:hover .image-wrapper .hover-toggle {
  display: block;
}

#2


2  

If you want to keep your structure unchanged, you could set height of div.hover-toggle then set margin of the image as below:

如果你想保持你的结构不变,你可以设置div.hover-toggle的高度,然后设置图像的边距如下:

margin:0 0 -30px 0;

which 30 is an appropriate number depends on the height of div.hover-toggle.

哪个30是一个合适的数字取决于div.hover-toggle的高度。

Live Demo: http://jsfiddle.net/4geFu/

现场演示:http://jsfiddle.net/4geFu/

#1


6  

There is no need for JS here, you can accomplish everything with simple CSS and a little markup change (I wrapped the image and the .hover-toggle div in a .image-wrapper div):

这里不需要JS,你可以通过简单的CSS和一点标记更改完成所有事情(我将图像和.hover-toggle div包装在.image-wrapper div中):

<ul class="thumbnails">
    <li class="span3">
        <div class="hover-group thumbnail">
            <div class="image-wrapper">
              <img src="http://placehold.it/260x180" alt="">
              <div class="hover-toggle btn-group">
                  <button class="btn">1</button>
                  <button class="btn">2</button>
                  <button class="btn">3</button>
              </div>
            </div>
            <h5>Thumbnail label</h5>
            <p>Thumbnail caption right here...</p>
        </div>
    </li>
...
</ul>

Now the styling it needs is simple:

现在它需要的样式很简单:

.hover-group .image-wrapper {
  /* We need this to make the .hover-toggle div relative to .image-wrapper */
  position: relative;
}

.hover-group .image-wrapper .hover-toggle {
  /* set it at the bottom of .image-wrapper */
  position: absolute;      
  bottom: 0;
  /* and don't display it */
  display: none;
}

.hover-group .image-wrapper:hover .hover-toggle {
  /* only display it when .image-wrapper is being hovered on */
  display: block;
}

If you want it to show when you hover over the whole .hover-group, use this CSS instead of the last line:

如果您希望将鼠标悬停在整个.hover-group上时显示,请使用此CSS代替最后一行:

.hover-group:hover .image-wrapper .hover-toggle {
  display: block;
}

#2


2  

If you want to keep your structure unchanged, you could set height of div.hover-toggle then set margin of the image as below:

如果你想保持你的结构不变,你可以设置div.hover-toggle的高度,然后设置图像的边距如下:

margin:0 0 -30px 0;

which 30 is an appropriate number depends on the height of div.hover-toggle.

哪个30是一个合适的数字取决于div.hover-toggle的高度。

Live Demo: http://jsfiddle.net/4geFu/

现场演示:http://jsfiddle.net/4geFu/