zmm-watermark.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. export default {
  18. data() {
  19. return {
  20. forLength: 0, //水印数量
  21. watermarkArea: 0 //水印大小(像素)
  22. };
  23. },
  24. props: {
  25. watermark: { //水印文字(支持html富文本)
  26. type: String,
  27. default: '水印文字'
  28. },
  29. color: { //水印文字默认颜色
  30. type: String,
  31. default: '#666'
  32. },
  33. fontSize: { //水印文字大小
  34. type: Number,
  35. default: 16
  36. },
  37. opacity: { //水印透明度
  38. type: Number,
  39. default: 0.15
  40. },
  41. margin: { //水印之间上下间距
  42. type: Number,
  43. default: 40
  44. },
  45. rotate: { //水印旋转角度
  46. type: Number,
  47. default: -21
  48. },
  49. column: { //水印列数
  50. type: Number,
  51. default: 2
  52. }
  53. },
  54. computed: {
  55. // 单个水印
  56. moldStyle() {
  57. return `width:${this.itemWidth}px;text-align: center;font-size:${this.fontSize}px;`
  58. },
  59. // 循环水印
  60. itemStyle() {
  61. 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;`
  62. },
  63. // 屏幕像素
  64. screenArea() {
  65. let height = uni.getSystemInfoSync().windowHeight + uni.getSystemInfoSync().windowTop
  66. let width = uni.getSystemInfoSync().windowWidth
  67. return Math.floor(height * width * 1.2)
  68. },
  69. // 单个水印最大长度
  70. itemWidth() {
  71. let windowWidth = uni.getSystemInfoSync().windowWidth
  72. return Math.floor(windowWidth / this.column - this.margin * 2)
  73. }
  74. },
  75. watch: {
  76. watermark: {
  77. handler(v) {
  78. if (v) {
  79. this.countForLength();
  80. }
  81. },
  82. deep: true
  83. }
  84. },
  85. mounted() {
  86. if (this.watermark) {
  87. this.countForLength();
  88. }
  89. },
  90. methods: {
  91. countForLength() { //计算水印数量
  92. // #ifndef APP-NVUE
  93. const query = uni.createSelectorQuery().in(this);
  94. query.select('#mold').boundingClientRect(data => {
  95. let width = data.width ? data.width : this.itemWidth
  96. let height = data.height ? data.height : 30
  97. let itemWidth = width + this.margin * 2
  98. let itemHeight = height + this.margin * 2
  99. this.watermarkArea = Math.floor(itemWidth * itemHeight)
  100. this.forLength = Math.floor(this.screenArea / this.watermarkArea)
  101. }).exec();
  102. // #endif
  103. // #ifdef APP-NVUE
  104. setTimeout(() => {
  105. const result = dom.getComponentRect(this.$refs.mold, (option) => {
  106. let size = option.size;
  107. let itemWidth = size.width + this.margin * 2
  108. let itemHeight = size.height + this.margin * 2
  109. this.watermarkArea = Math.floor(itemWidth * itemHeight)
  110. this.forLength = Math.floor(this.screenArea / this.watermarkArea)
  111. });
  112. }, 50);
  113. // #endif
  114. },
  115. }
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. .zmm-watermark {
  120. position: fixed;
  121. overflow: hidden;
  122. z-index: 999;
  123. left: 0;
  124. top: 0;
  125. right: 0;
  126. bottom: 0;
  127. /* #ifndef APP-NVUE */
  128. pointer-events: none;
  129. /* #endif */
  130. }
  131. .zmm-watermark-content {
  132. position: fixed;
  133. left: 0;
  134. right: 0;
  135. top: 0;
  136. bottom: 0;
  137. overflow: hidden;
  138. display: flex;
  139. flex-direction: row;
  140. flex-wrap: wrap;
  141. justify-content: space-around;
  142. }
  143. .zmm-watermark-mold {
  144. position: fixed;
  145. left: 0;
  146. top: 0;
  147. opacity: 0;
  148. }
  149. </style>