经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jquery实现照片墙
来源:cnblogs  作者:smile_or  时间:2019/4/28 9:00:38  对本文有异议

jquery照片墙

  • 由15张图片构成,大致思路:随机生成所有图片-->点击其中一张变为一张大图-->点击大图又变回多张
  • 主要用到jquery实现
  • 先来看看效果

Alt

html:

  1. <div class="wraper">
  2. <ul class="wraper-ul"></ul>
  3. </div>

css:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. }
  6. html,
  7. body,
  8. .wraper {
  9. width: 100%;
  10. height: 100%;
  11. background-color: #ececec;
  12. display: flex;
  13. justify-content: center;
  14. align-items: center;
  15. }
  16. .wraper-ul {
  17. width: 80%;
  18. height: 80%;
  19. position: relative;
  20. perspective: 800px;
  21. }
  22. .wraper-ul li {
  23. position: absolute;
  24. transform-style: preserve-3d;
  25. background-color: #fff;
  26. cursor: pointer;
  27. }
  28. .box {
  29. width: 100%;
  30. height: 100%;
  31. transform: scale(0.9);
  32. }
  33. .box img {
  34. width: 100%;
  35. height: 100%;
  36. }

js:

  1. class photos {
  2. constructor(className){
  3. this.wraper = $(className);
  4. this.ulW = parseInt(this.wraper.css('width'));
  5. this.ulH = parseInt(this.wraper.css('height'));
  6. this.liW = this.ulW /5;
  7. this.liH = this.ulH /3;
  8. this.change = true;
  9. this.creatImgs();
  10. }
  11. creatImgs(){
  12. //行
  13. for(let i =0;i<3;i++){
  14. //列
  15. for(let j=0;j<5;j++){
  16. let lis = $("<li><div class='box'><img src='' alt=''></div></li>")
  17. .css({
  18. width:this.liW +'px',
  19. height:this.liH +'px',
  20. left:j*this.liW +'px',
  21. top:i*this.liH + 'px',
  22. transform:'scale(0.9) rotate('+(Math.random() * 40 - 20)+'deg)'+
  23. 'translateX(' + (Math.random() * 100 - 50) + 'px)' +
  24. 'translateY(' + (Math.random() * 100 - 50) + 'px)' +
  25. 'translateZ(' + (Math.random() * 200 - 100) +'px)'
  26. })
  27. .find('img').attr('src','./img/'+(i*5+j+11) +'.jpg')
  28. .end()
  29. this.wraper.append(lis);
  30. }
  31. }
  32. this.changeImgs();
  33. }
  34. changeImgs(){
  35. this.wraper.find('li').on('click',(e)=>{
  36. if(this.change){ //多张变一张
  37. let bgImg = $(e.target).attr('src');
  38. let bgLeft =0;
  39. let bgTop =0;
  40. $('li').each((index,item)=>{
  41. $(item).delay(10 * index).animate({opacity:0},200,()=>{
  42. $(item).css({
  43. width: this.liW +'px',
  44. heigth:this.liH +'px',
  45. transition: '',
  46. opacity:'1',
  47. transform: 'scale(1) rotate(0deg)' +
  48. 'translateX(0px)' +
  49. 'translateY(0px)' +
  50. 'translateZ(0px)'
  51. })
  52. $(item).find('.box').css({
  53. transform:'scale(1)'
  54. })
  55. $(item).find('img').attr('src', bgImg).css({
  56. position:'absolute',
  57. width:this.ulW +'px',
  58. height:this.ulH +'px',
  59. top: -bgTop,
  60. left: -bgLeft
  61. });
  62. bgLeft += this.liW;
  63. if(bgLeft>=this.ulW){
  64. bgTop +=this.liH;
  65. bgLeft =0;
  66. }
  67. })
  68. })
  69. this.change = false;
  70. }else{ //一张变多张
  71. this.change = true;
  72. $('li').each((index, item) => {
  73. let j =index % 5;
  74. let i =Math.floor(index / 5);
  75. $(item).animate({ opacity: 0 }, 200, () => {
  76. $(item).find('img').css({
  77. position: 'absolute',
  78. width: '100%',
  79. height: '100%',
  80. top: 0,
  81. left: 0
  82. })
  83. $(item).find('img').attr('src', './img/' + (index+11) + '.jpg')
  84. $(item).find('.box').css({
  85. transform: 'scale(0.9)'
  86. })
  87. $(item).css({
  88. width: this.liW + 'px',
  89. height: this.liH + 'px',
  90. left: j * this.liW + 'px',
  91. top: i * this.liH + 'px',
  92. transition:'all,0.5s',
  93. opacity: '1',
  94. transform: 'scale(0.9) rotate(' + (Math.random() * 40 - 20) + 'deg)' +
  95. 'translateX(' + (Math.random() * 100 - 50) + 'px)' +
  96. 'translateY(' + (Math.random() * 100 - 50) + 'px)' +
  97. 'translateZ(' + (Math.random() * 200 - 100) + 'px)'
  98. })
  99. })
  100. })
  101. }
  102. })
  103. }
  104. }
  105. var photo = new photos('.wraper-ul');

参考自:腾讯课堂渡一教育

原文链接:http://www.cnblogs.com/sgs123/p/10775636.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号