gui-image.vue 1.8 KB

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