在右上角显示一个小图像

Display a small image at the top-right corner

本文关键字:一个 图像 右上角 显示      更新时间:2023-09-26

好的,所以基本上这就是我想要做的,但是-因为我不是一个CSS向导-我需要一些输入。。。

  • 我想创建一个新类
  • 当这个类应用于一个项目(div或其他任何东西-显然具有足够的尺寸以使其适合)时,一个小的可点击预定义图像会显示在该项目内部的右上角

我该怎么做?有什么想法吗?

<div class="hasicon">
    <img src="blah.jpg" class="icon" />
</div>

CSS:

.icon { display: none; }
.hasicon { position: relative; }
.hasicon .icon {
    display: block;
    position: absolute;
    top: 0;
    right: 0;
    width: 32px;
    height: 32px;
    cursor: pointer;
}

jQuery:

$('body').on('click', '.hasicon > .icon', function() {
    // do something
});

要使用它,只需将类hasicon添加到div中。没有该类的div将不会显示图标。

更新:

如果更适合您的要求,您可以尝试使用空跨度:

<div class="hasicon">
    <span></span>
</div>

CSS:

.hasicon { position: relative; }
.hasicon > span {
    display: block;
    position: absolute;
    top: 0;
    right: 0;
    width: 32px;
    height: 32px;
    cursor: pointer;
    background: url('myImage.png') center no-repeat;
}

jQuery:

$('body').on('click', '.hasicon > span', function() {
    // do something
});

未显示的图像:

<div class="DivWithImage">
    <img src="blah.jpg" class="thumbIMG">
</div>

正在显示的图像:

<div class="DivWithImage">
    <img src="blah.jpg" class="thumbIMG shown">
</div>

CSS:

.DivWithImage .thumbIMG{
    position: absolute;
    right: 0px;
    top: 0px;
    display:none;
}
.DivWithImage .thumbIMG.shown{
    display:block;
}

然后,您需要使用javascript来应用或删除类shown

您可以对动态添加的类使用:after:before伪选择器。

HTML

<div class="something dynamicallyAdded"></div>

CSS

.dynamicallyAdded {
    width: 300px;
    height: 400px;
    background-color: grey;
    position: relative;
}
.dynamicallyAdded:after {
    content:"";
    width: 100px;
    height: 100px;
    top: 0;
    right: 0;
    position:absolute;
    background-color: red;
} 
/* if you want the hover effect */
.dynamicallyAdded:hover:after {
    content:"";
    background-color: green;
}

工作Fiddle

工作Fiddle(带图像的