123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <template>
- <view class="zmm-watermark">
- <!-- 单个用于计算 -->
- <view class="zmm-watermark-mold" ref="mold" id="mold">
- <rich-text :style="moldStyle" :nodes="watermark"></rich-text>
- </view>
- <!-- 循环 -->
- <view class="zmm-watermark-content" :style="{opacity:opacity}">
- <rich-text :nodes="watermark" :style="itemStyle" v-for="(item,index) in forLength" :key="index"></rich-text>
- </view>
- </view>
- </template>
- <script>
- // #ifdef APP-NVUE
- const dom = weex.requireModule("dom");
- // #endif
- export default {
- data() {
- return {
- forLength: 0, //水印数量
- watermarkArea: 0 //水印大小(像素)
- };
- },
- props: {
- watermark: { //水印文字(支持html富文本)
- type: String,
- default: '水印文字'
- },
- color: { //水印文字默认颜色
- type: String,
- default: '#666'
- },
- fontSize: { //水印文字大小
- type: Number,
- default: 16
- },
- opacity: { //水印透明度
- type: Number,
- default: 0.15
- },
- margin: { //水印之间上下间距
- type: Number,
- default: 40
- },
- rotate: { //水印旋转角度
- type: Number,
- default: -21
- },
- column: { //水印列数
- type: Number,
- default: 2
- }
- },
- computed: {
- // 单个水印
- moldStyle() {
- return `width:${this.itemWidth}px;text-align: center;font-size:${this.fontSize}px;`
- },
- // 循环水印
- itemStyle() {
- 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;`
- },
- // 屏幕像素
- screenArea() {
- let height = uni.getSystemInfoSync().windowHeight + uni.getSystemInfoSync().windowTop
- let width = uni.getSystemInfoSync().windowWidth
- return Math.floor(height * width * 1.2)
- },
- // 单个水印最大长度
- itemWidth() {
- let windowWidth = uni.getSystemInfoSync().windowWidth
- return Math.floor(windowWidth / this.column - this.margin * 2)
- }
- },
- watch: {
- watermark: {
- handler(v) {
- if (v) {
- this.countForLength();
- }
- },
- deep: true
- }
- },
- mounted() {
- if (this.watermark) {
- this.countForLength();
- }
- },
- methods: {
- countForLength() { //计算水印数量
- // #ifndef APP-NVUE
- const query = uni.createSelectorQuery().in(this);
- query.select('#mold').boundingClientRect(data => {
- let width = data.width ? data.width : this.itemWidth
- let height = data.height ? data.height : 30
- let itemWidth = width + this.margin * 2
- let itemHeight = height + this.margin * 2
- this.watermarkArea = Math.floor(itemWidth * itemHeight)
- this.forLength = Math.floor(this.screenArea / this.watermarkArea)
- }).exec();
- // #endif
- // #ifdef APP-NVUE
- setTimeout(() => {
- const result = dom.getComponentRect(this.$refs.mold, (option) => {
- let size = option.size;
- let itemWidth = size.width + this.margin * 2
- let itemHeight = size.height + this.margin * 2
- this.watermarkArea = Math.floor(itemWidth * itemHeight)
- this.forLength = Math.floor(this.screenArea / this.watermarkArea)
- });
- }, 50);
- // #endif
- },
- }
- }
- </script>
- <style lang="scss" scoped>
- .zmm-watermark {
- position: fixed;
- overflow: hidden;
- z-index: 999;
- left: 0;
- top: 0;
- right: 0;
- bottom: 0;
- /* #ifndef APP-NVUE */
- pointer-events: none;
- /* #endif */
- }
- .zmm-watermark-content {
- position: fixed;
- left: 0;
- right: 0;
- top: 0;
- bottom: 0;
- overflow: hidden;
- display: flex;
- flex-direction: row;
- flex-wrap: wrap;
- justify-content: space-around;
- }
- .zmm-watermark-mold {
- position: fixed;
- left: 0;
- top: 0;
- opacity: 0;
- }
- </style>
|