繁体   English   中英

在右上角显示一个小图像

[英]Display a small image at the top-right corner

好的,所以基本上这就是我想做的事情 - 因为我不是这样的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;
}

工作小提琴

工作小提琴含图片

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM