[关闭]
@EncyKe 2016-05-09T02:18:42.000000Z 字数 1967 阅读 2079

CSS3 + JS:图片展示(卡片式蒙版)

前端 JavaScript



一、JS原生方法

HTML部分

  1. <ul id="picList">
  2. <li>
  3. <img src="1.jpg" />
  4. <a href='#'>
  5. 学会html5 绝对的屌丝逆袭<br/>
  6. 本课程由浅入深,逐步推进,以示例为主线,详细完整地介绍HTML5的新功能与新特征
  7. </a>
  8. </li>
  9. </ul>

CSS部分

  1. *{
  2. transition: all .2s ease-in;
  3. /* 可增加动效,但兼容性不佳; */
  4. }
  5. body, ul, li, ol {
  6. list-style: none;
  7. padding: 0px;
  8. margin: 0px;
  9. font-size: 12px;
  10. font-family: 微软雅黑;
  11. }
  12. img {
  13. border: 0px;
  14. }
  15. ul {
  16. width: 100%;
  17. float: left;
  18. }
  19. li{
  20. width: 300px;
  21. height: 200px;
  22. position: relative;
  23. overflow: hidden;
  24. float: left;
  25. margin: 10px 0 0 10px;
  26. }
  27. img, a {
  28. position: absolute;
  29. top: 0px;
  30. left: 0px;
  31. }
  32. img {
  33. width: 100%;
  34. }
  35. a {
  36. display: block;
  37. width: 280px;
  38. height: 180px;
  39. top: 160px;
  40. background:#000;
  41. color: white;
  42. font-style: normal;
  43. line-height: 25px;
  44. padding: 10px;
  45. text-decoration: none;
  46. filter:alpha(Opacity=80);
  47. -moz-opacity:0.8;
  48. -ms-opacity: 0.8;
  49. -khtml-opacity: 0.8;
  50. -webkit-opacity: 0.8;
  51. -0-opacity: 0.8;
  52. opacity: 0.8;
  53. }

JS部分

  1. function imgDisplay(){
  2. var li = document.getElementById('picList').getElementsByTagName('li');
  3. var liHeight = 160;
  4. for(var i = 0; i < li.length; i++){
  5. li[i].onmouseover = showMeg;
  6. li[i].onmouseout = hideMeg;
  7. }
  8. function showMeg(){
  9. this.getElementsByTagName('a')[0].style.top = 0;
  10. }
  11. function hideMeg(){
  12. this.getElementsByTagName('a')[0].style.top = liHeight+'px';
  13. }
  14. }
  15. imgDisplay();

二、纯CSS3方法

HTML部分同上,CSS代码如下:

  1. *{
  2. transition: all .1s ease-in;
  3. }
  4. body, ul, li, ol {
  5. list-style: none;
  6. padding: 0px;
  7. margin: 0px;
  8. font-size: 12px;
  9. font-family: 微软雅黑;
  10. }
  11. img {
  12. border: 0px;
  13. }
  14. ul {
  15. width: 100%;
  16. float: left;
  17. }
  18. li{
  19. width: 300px;
  20. height: 200px;
  21. position: absolute;
  22. float: left;
  23. top: 50%;
  24. left: 50%;
  25. margin: -150px 0 0 -100px;
  26. }
  27. img, a {
  28. position: absolute;
  29. top: 0px;
  30. left: 0px;
  31. }
  32. img {
  33. width: 100%;
  34. }
  35. a {
  36. display: block;
  37. width: 280px;
  38. height: 150px;
  39. top: auto;
  40. bottom: 60px;
  41. background:#000;
  42. color: white;
  43. font-style: normal;
  44. line-height: 25px;
  45. padding: 10px;
  46. text-decoration: none;
  47. filter:alpha(Opacity=0);
  48. -moz-opacity:0;
  49. -ms-opacity: 0;
  50. -khtml-opacity: 0;
  51. -webkit-opacity: 0;
  52. -o-opacity: 0;
  53. opacity: 0;
  54. }
  55. img:hover+a,
  56. a:hover{
  57. bottom: 30px;
  58. filter:alpha(Opacity=80);
  59. -moz-opacity:0.8;
  60. -ms-opacity: 0.8;
  61. -khtml-opacity: 0.8;
  62. -webkit-opacity: 0.8;
  63. -o-opacity: 0.8;
  64. opacity: 0.8;
  65. }

附:参考

慕课:图片展示特效
Demo下载
梨山宾馆 >> 梨山风华 >> 周边景点

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注