You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
1.7 KiB

6 months ago
  1. <template>
  2. <!-- #ifndef H5 -->
  3. <view class="lazy-image">
  4. <image :class="['real-image', cname]" @load="onLoaded" :src="realSrc" mode="aspectFill"></image>
  5. <view :class="[loaded ? 'loaded' : '', cname]" :style="{ background: placeholdColor }">
  6. <image class="placehold-image" :src="placeholdSrc" mode="aspectFill"></image>
  7. </view>
  8. </view>
  9. <!-- #endif -->
  10. <!-- #ifdef H5 -->
  11. <view :class="['lazy-img', cname]">
  12. <image :src="realSrc" mode="aspectFill" @load="onLoaded"></image>
  13. <view :class="[loaded ? 'loaded' : '', 'lazy-mask']" :style="{ background: placeholdColor }"></view>
  14. </view>
  15. <!-- #endif -->
  16. </template>
  17. <script>
  18. export default {
  19. props:{
  20. cname: String,
  21. realSrc:{
  22. type:String,
  23. default:""
  24. },
  25. placeholdColor:{
  26. type:String,
  27. default:"#eee"
  28. },
  29. placeholdSrc:{
  30. type:String,
  31. default:""
  32. }
  33. },
  34. data(){
  35. return {
  36. loaded: false
  37. }
  38. },
  39. methods:{
  40. onLoaded(){
  41. this.loaded = true
  42. }
  43. }
  44. }
  45. </script>
  46. <style lang="scss">
  47. .lazy-image{
  48. height: 100%;
  49. width: 100%;
  50. flex: 1;
  51. display: flex;
  52. flex-direction: column;
  53. align-items: center;
  54. position: relative;
  55. .real-image{
  56. width: 100%;
  57. }
  58. view{
  59. position: absolute;
  60. z-index: 2;
  61. top: 0;
  62. left: 0;
  63. height: 100%;
  64. width: 100%;
  65. flex: 1;
  66. display: flex;
  67. flex-direction: column;
  68. align-items: center;
  69. transition: opacity .1s linear;
  70. .placehold-image{
  71. width: 100%;
  72. }
  73. }
  74. .loaded{
  75. opacity: 0;
  76. }
  77. }
  78. .lazy-img {
  79. position: relative;
  80. overflow: hidden;
  81. image {
  82. @include size(100%);
  83. }
  84. .lazy-mask {
  85. position: absolute;
  86. top: 0;
  87. left: 0;
  88. z-index: 2;
  89. @include size(100%);
  90. transition: opacity .1s linear;
  91. &.loaded {
  92. opacity: 0;
  93. }
  94. }
  95. }
  96. </style>