PreviewImage.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. export default class PreviewImage {
  2. constructor(Dcat) {
  3. this.dcat = Dcat;
  4. Dcat.previewImage = this.preview
  5. }
  6. preview(src, width, title) {
  7. let Dcat = this.dcat,
  8. img = new Image(),
  9. win = Dcat.helpers.isset(window.top) ? top : window,
  10. clientWidth = Math.ceil(win.screen.width * 0.6),
  11. clientHeight = Math.ceil(win.screen.height * 0.8);
  12. img.style.display = 'none';
  13. img.style.height = 'auto';
  14. img.style.width = width || '100%';
  15. img.src = src;
  16. document.body.appendChild(img);
  17. Dcat.loading();
  18. img.onload = function () {
  19. Dcat.loading(false);
  20. let srcw = this.width,
  21. srch = this.height,
  22. width = srcw > clientWidth ? clientWidth : srcw,
  23. height = Math.ceil(width * (srch/srcw));
  24. height = height > clientHeight ? clientHeight : height;
  25. title = title || src.split('/').pop();
  26. if (title.length > 50) {
  27. title = title.substr(0, 50) + '...';
  28. }
  29. win.layer.open({
  30. type: 1,
  31. shade: 0.2,
  32. title: false,
  33. maxmin: false,
  34. shadeClose: true,
  35. closeBtn: 2,
  36. content: $(img),
  37. area: [width+'px', (height) + 'px'],
  38. skin: 'layui-layer-nobg',
  39. end: function () {
  40. document.body.removeChild(img);
  41. }
  42. });
  43. };
  44. img.onerror = function () {
  45. Dcat.loading(false);
  46. Dcat.warning('预览失败');
  47. };
  48. }
  49. }