分享

使用jQuery设置图像字幕

 昵称11196537 2012-12-15
在本教程中,我们将使用jQuery一个漂亮的半透明图像标题。悬停图像的标题将被触发。我们将使用jQuery函数fadeTo()函数来实现半透明度。它有一个非常好的效果。
图片

HTML代码
  1. <!-- wrapper div -->  
  2. <div class='wrapper'>  
  3.     <!-- image -->  
  4.     <img src='wolf.jpg' />  
  5.     <!-- description div -->  
  6.     <div class='description'>  
  7.         <!-- description content -->  
  8.         <div class='description_content'>The pack, the basic unit of wolf social life, is usually a family group. It is made up of animals related to each other by blood and family ties of affection and mutual aid. </div>  
  9.         <!-- end description content -->  
  10.     </div>  
  11.       < - !最终描述的格-  >
  12. </div>  
  13. <! - 最终包装的div  - >
CSS代码
  1. div.wrapper{  
  2.     position:relative/*重要(所以我们当前完全可以定位描述的分区*/  
  3. }  
  4. div.description{  
  5.     position:absolute/*绝对位置(这样我们当前就可以定位我们当前想要)*/  
  6.     bottombottom:0px/ *位置将是在底部* /  
  7.     left:0px;  
  8.     display:none/* hide it */  
  9.     /* styling bellow */  
  10.     background-color:black;  
  11.     font-family'tahoma';  
  12.     font-size:15px;  
  13.     color:white;  
  14. }  
  15. div.description_content{  
  16.     padding:10px;  
  17. }  
jQuery代码

  1. //We are using $(window).load here because we want to wait until the images are loaded  
  2. $(window).load(function(){  
  3.     //for each description div...  
  4.     $('div.description').each(function(){  
  5.         //...set the opacity to 0...  
  6.         $(this).css('opacity', 0);  
  7.         //..set width same as the image...  
  8.         $(this).css('width', $(this).siblings('img').width());  
  9.         //...get the parent (the wrapper) and set it's width same as the image width... '  
  10.         $(this).parent().css('width', $(this).siblings('img').width());  
  11.         //...set the display to block  
  12.         $(this).css('display''block');  
  13.     });  
  14.   
  15.     $('div.wrapper').hover(function(){  
  16.         //when mouse hover over the wrapper div  
  17.         //get it's children elements with class description '  
  18.         //and show it using fadeTo  
  19.         $(this).children('.description').stop().fadeTo(500, 0.7);  
  20.     },function(){  
  21.         //when mouse out of the wrapper div  
  22.         //use fadeTo to hide the div  
  23.         $(this).children('.description').stop().fadeTo(500, 0);  
  24.     });  
  25.   
  26. });  
总结
 
很简单,效果不错,不是吗?如果您有任何问题随时分享它。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约