gui-image.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <view
  3. class="gui-img gui-bg-gray gui-dark-bg-level-3"
  4. :style="{
  5. width:width+'rpx',
  6. height:height == 0 ? imgHeight+'rpx' : height+'rpx'
  7. }">
  8. <image
  9. :src="src"
  10. @load="imgLoad"
  11. @error="error"
  12. :mode="mode"
  13. :style="{
  14. width:width+'rpx',
  15. height:imgHeight+'rpx',
  16. borderRadius:borderRadius,
  17. opacity:opacity
  18. }"></image>
  19. <text
  20. class="gui-img-loading gui-icons gui-color-gray"
  21. :class="[animate?'gui-fade-out':'']"
  22. v-if="isLoading"
  23. :style="{
  24. width:width+'rpx',
  25. height:height == 0 ? imgHeight+'rpx' : height+'rpx',
  26. lineHeight:height == 0 ? imgHeight+'rpx' : height+'rpx',
  27. borderRadius:borderRadius}">&#xe63a;</text>
  28. <text
  29. class="gui-img-loading gui-icons gui-color-gray"
  30. :class="[animate?'gui-fade-out':'']"
  31. v-if="failed"
  32. :style="{
  33. width:width+'rpx',
  34. height:height == 0 ? imgHeight+'rpx' : height+'rpx',
  35. lineHeight:height == 0 ? imgHeight+'rpx' : height+'rpx',
  36. borderRadius:borderRadius}">&#xe65b;</text>
  37. </view>
  38. </template>
  39. <script>
  40. export default{
  41. name : "gui-image",
  42. props : {
  43. src : {type:String, default:''},
  44. width : {type:Number, default:300},
  45. height : {type:Number, default:0},
  46. timer : {type:Number, default:200},
  47. borderRadius : {type:String, default:'0rpx'},
  48. mode : {type:String, default:'widthFix'}
  49. },
  50. data() {
  51. return {
  52. isLoading : true,
  53. imgHeight : 180,
  54. opacity : 0,
  55. animate : false,
  56. failed : false
  57. }
  58. },
  59. methods:{
  60. imgLoad : function (e) {
  61. var scale = e.detail.width / e.detail.height;
  62. if(this.mode == "widthFix"){
  63. this.imgHeight = this.width / scale;
  64. }else{
  65. this.imgHeight = this.height;
  66. }
  67. this.animate = true;
  68. setTimeout(() => {this.isLoading = false; this.opacity = 1;}, this.timer);
  69. },
  70. error : function(){
  71. this.isLoading = false;
  72. this.failed = true;
  73. }
  74. }
  75. }
  76. </script>
  77. <style scoped>
  78. .gui-img{width:500rpx; height:500rpx; overflow:hidden; font-size:0; position:relative;}
  79. .gui-img-loading{position:absolute; left:0; top:0; font-size:55rpx; text-align:center;}
  80. </style>