简体   繁体   English

使用CSS和JQUERY在图像上显示标题

[英]display a caption over an image using CSS and JQUERY

I have the following jsfiddle but it doesnt seem to show the caption up: http://jsfiddle.net/KumcX/189/ 我有以下jsfiddle,但它似乎没有显示标题: http : //jsfiddle.net/KumcX/189/

HTML: HTML:

<ul>
<li>
    <div class="caption" style="width: 220px; height: 140px; display: block; opacity: 0.0524612;">this is my caption</div>
    <a href="#"><img src="http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367" alt=""></a></li>
    <li>
    <div class="caption" style="width: 220px; height: 140px; display: block; opacity: 0.0524612;">this is my caption</div>
    <a href="#"><img src="http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367" alt=""></a></li>
    <li>
    <div class="caption" style="width: 220px; height: 140px; display: block; opacity: 0.0524612;">this is my caption</div>
    <a href="#"><img src="http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367" alt=""></a></li>
    <li>
    <div class="caption" style="width: 220px; height: 140px; display: block; opacity: 0.0524612;">this is my caption</div>
    <a href="#"><img src="http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367" alt=""></a></li>
    <li>
    <div class="caption" style="width: 220px; height: 140px; display: block; opacity: 0.0524612;">this is my caption</div>
    <a href="#"><img src="http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367" alt=""></a></li>
    <li>
    <div class="caption" style="width: 220px; height: 140px; display: block; opacity: 0.0524612;">this is my caption</div>
    <a href="#"><img src="http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367" alt=""></a></li>
</ul>

CSS: CSS:

ul li{ float:left; padding:20px; border:solid gray 4px; margin:5px;}
ul li div{display:none; background:white; opacity:.5; position:absolute;}

JS: JS:

$('ul li').mouseenter(function(){
    var image= $(this).find('a img'),
        caption = $(this).find('div');

    caption.width(image.width());
    caption.height(image.height());
    caption.fadeIn();
}).mouseleave(function(){
     var image= $(this).find('img'),
        caption = $(this).find('div');

    caption.width(image.width());
    caption.height(image.height());
    caption.fadeOut();
});

It should show the caption, but its not.. Any ideas? 它应该显示标题,但不显示标题。有什么想法吗?

You probably copy the html with firebug, 您可能使用firebug复制了html,

delete the styles on the li elements. 删除li元素上的样式。

check this http://jsfiddle.net/kuyabiye/KumcX/194/ 检查此http://jsfiddle.net/kuyabiye/KumcX/194/

Refactored some of the JS, ditched the inline caption styling, and updated the css class 重构了一些JS,放弃了内联标题样式,并更新了CSS类

jQuery jQuery的

$(".caption").css('opacity', 0);

$('ul li').mouseenter(function() {    
    $(this).find(".caption").animate({ 'opacity': 0.9 });
}).mouseleave(function() {
    $(this).find(".caption").animate({ 'opacity': 0 });
});​

css 的CSS

ul li {
    float:left; 
    padding:20px; 
    border:solid gray 4px; 
    margin:5px;
}
.caption {
    width: 220px;
    background: #fff; 
    height: 150px;        
    position: absolute;
}​​​

example : http://jsfiddle.net/hunter/KumcX/201/ 例如http : //jsfiddle.net/hunter/KumcX/201/

Use animate effect, configuring opacity: 使用动画效果,配置不透明度:

$('ul li').mouseenter(function(){
    var image= $(this).find('a img'),
        caption = $(this).find('div');

    caption.width(image.width());
    caption.height(image.height());
    caption.animate({'opacity':0.5});
}).mouseleave(function(){
    var image= $(this).find('img'),
        caption = $(this).find('div');

    caption.width(image.width());
    caption.height(image.height());
    caption.animate({'opacity':0});
});

Just a couple of tweaks needed, first of all in your CSS you need to give your <div> a left and top position as well as some height and width which I've set to 100% to span the image and your <li> a position relative so the caption is nicely positioned over the image. 只需进行一些调整,首先在CSS中,您需要为<div>设置左和顶部的位置以及一些高度和宽度,我将其设置为100%以覆盖图像和<li>相对位置,因此字幕可以很好地定位在图像上。 Oh I also gave the <div> background an rgba color of 255,255,255,0.5 to achieve your transparent effect. 哦,我还给<div>背景设置了255,255,255,0.5的rgba颜色,以实现透明效果。

Your JS is fine but your problem in the fiddle is that you have lots of inline styles so delete those and everything should work ok from then on in. 您的JS很好,但是您摆弄的问题是您有很多内联样式,因此请删除这些样式,然后从那时开始一切正常。

Here's an updated fiddle: http://jsfiddle.net/KumcX/197/ 这是更新的小提琴: http : //jsfiddle.net/KumcX/197/

And the updated CSS: 以及更新的CSS:

ul li{ float:left; padding:20px; border:solid gray 4px; margin:5px;position:relative}
ul li div{display:none; background:rgba(255,255,255,.5); position:absolute;left:0;top:0;width:100%;height:100%;}

​Hope this helps. 希望这会有所帮助。

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

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