[英]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.