zmm-watermark.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view class="zmm-watermark">
  3. <!-- 单个用于计算 -->
  4. <view class="zmm-watermark-mold" ref="mold" id="mold">
  5. <rich-text :style="moldStyle" :nodes="watermark"></rich-text>
  6. </view>
  7. <!-- 循环 -->
  8. <view class="zmm-watermark-content" :style="{opacity:opacity}">
  9. <rich-text :nodes="watermark" :style="itemStyle" v-for="(item,index) in forLength" :key="index"></rich-text>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. // #ifdef APP-NVUE
  15. const dom = weex.requireModule("dom");
  16. // #endif
  17. /**
  18. * zmm-watermark 页面水印组件
  19. * @description 页面水印组件 支持富文本、自动计算所需水印数量不卡顿、自定义旋转角度等
  20. * @tutorial https://ext.dcloud.net.cn/plugin?id=4587
  21. * @property {String} watermark 水印内容,支持html富文本
  22. * @property {String} color 水印文字默认颜色
  23. * @property {String} fontSize 水印文字默认大小
  24. * @property {Number} opacity 水印透明度
  25. * @property {Number} margin 水印之间上下间距
  26. * @property {Number} rotate 水印旋转角度
  27. * @property {Number} column 水印列数
  28. */
  29. export default {
  30. data() {
  31. return {
  32. forLength: 0, //水印数量
  33. watermarkArea: 0 //水印大小(像素)
  34. };
  35. },
  36. props: {
  37. watermark: {
  38. type: String,
  39. default: '水印文字'
  40. },
  41. color: {
  42. type: String,
  43. default: '#666'
  44. },
  45. fontSize: {
  46. type: Number,
  47. default: 16
  48. },
  49. opacity: {
  50. type: Number,
  51. default: 0.15
  52. },
  53. margin: {
  54. type: Number,
  55. default: 10
  56. },
  57. rotate: {
  58. type: Number,
  59. default: -21
  60. },
  61. column: {
  62. type: Number,
  63. default: 3
  64. }
  65. },
  66. computed: {
  67. // 单个水印
  68. moldStyle() {
  69. return `width:${this.itemWidth}px;text-align: center;font-size:${this.fontSize}px;`
  70. },
  71. // 循环水印
  72. itemStyle() {
  73. return `color:${this.color};font-size:${this.fontSize}px;margin:${this.margin}px;width:${this.itemWidth}px;transform:rotate(${this.rotate}deg);text-align: center;`
  74. },
  75. // 屏幕像素
  76. screenArea() {
  77. let height = uni.getSystemInfoSync().windowHeight + uni.getSystemInfoSync().windowTop
  78. let width = uni.getSystemInfoSync().windowWidth
  79. return Math.floor(height * width * 1.2)
  80. },
  81. // 单个水印最大长度
  82. itemWidth() {
  83. let windowWidth = uni.getSystemInfoSync().windowWidth
  84. return Math.floor(windowWidth / this.column - this.margin * 2 - 20 / this.column)
  85. }
  86. },
  87. watch: {
  88. watermark: {
  89. handler(v) {
  90. if (v) {
  91. this.countForLength();
  92. }
  93. },
  94. deep: true
  95. }
  96. },
  97. mounted() {
  98. if (this.watermark) {
  99. this.countForLength();
  100. }
  101. },
  102. methods: {
  103. countForLength() { //计算水印数量
  104. // #ifndef APP-NVUE
  105. const query = uni.createSelectorQuery().in(this);
  106. query.select('#mold').boundingClientRect(data => {
  107. let width = data.width ? data.width : this.itemWidth
  108. let height = data.height ? data.height : 30
  109. let itemWidth = width + this.margin * 2
  110. let itemHeight = height + this.margin * 2
  111. this.watermarkArea = Math.floor(itemWidth * itemHeight)
  112. this.forLength = Math.floor(this.screenArea / this.watermarkArea)
  113. }).exec();
  114. // #endif
  115. // #ifdef APP-NVUE
  116. setTimeout(() => {
  117. const result = dom.getComponentRect(this.$refs.mold, (option) => {
  118. let size = option.size;
  119. let itemWidth = size.width + this.margin * 2
  120. let itemHeight = size.height + this.margin * 2
  121. this.watermarkArea = Math.floor(itemWidth * itemHeight)
  122. this.forLength = Math.floor(this.screenArea / this.watermarkArea)
  123. });
  124. }, 50);
  125. // #endif
  126. },
  127. }
  128. }
  129. </script>
  130. <style lang="scss" scoped>
  131. .zmm-watermark {
  132. position: fixed;
  133. overflow: hidden;
  134. z-index: 999;
  135. left: 0;
  136. top: 0;
  137. right: 0;
  138. bottom: 0;
  139. /* #ifndef APP-NVUE */
  140. pointer-events: none;
  141. /* #endif */
  142. }
  143. .zmm-watermark-content {
  144. position: fixed;
  145. left: 0;
  146. right: 0;
  147. top: 0;
  148. bottom: 0;
  149. overflow: hidden;
  150. display: flex;
  151. flex-direction: row;
  152. flex-wrap: wrap;
  153. justify-content: space-around;
  154. }
  155. .zmm-watermark-mold {
  156. position: fixed;
  157. left: 0;
  158. top: 0;
  159. opacity: 0;
  160. }
  161. </style>