项目作者: funinps

项目描述 :
Add text/HTML as watermark to DOM node, customizable content and style, automatically calculate size and position
高级语言: JavaScript
项目地址: git://github.com/funinps/dom-mark.git
创建时间: 2020-05-16T16:25:46Z
项目社区:https://github.com/funinps/dom-mark

开源协议:MIT License

下载


dom-mark

@rotick/dom-mark"">NPM version |
中文文档

Add text/HTML as watermark to DOM node

  • automatically calculate size and position
  • 4kb mini library
  • support IE 9+ and all mobile browser
  • customizable HTML content and style
  • observe dom changes and rerender with MutationObserver

Demo

ESM | UMD | mobile

demo

Install

  1. npm i @rotick/dom-mark -S

In browser, download dist/dom-mark.js to your project:

  1. <script src="/path/to/dom-mark.js"></script>

Useage

  1. import DomMark from '@rotick/dom-mark'
  2. const domMark = new DomMark('body', {
  3. content: '水印', // html,text
  4. fontSize: 'inherit',
  5. fontFamily: 'inherit',
  6. color: 'inherit',
  7. opacity: .3,
  8. padding: 20,
  9. zIndex: 6000,
  10. minMargin: [40, 20],
  11. rotate: -15,
  12. observe: true
  13. })
  14. domMark.render()

update:

  1. domMark.update({
  2. content: '<img src="/watermark.png" width="60" />'
  3. })

important: if you want to use setInterval or setTimeout to update, don’t forget clearInterval or clearTimeout, otherwise maybe it will cause a crash(e.g. webpack HMR)

destroy:

  1. domMark.destroy()

API

  1. const dm = new DomMark(selector, options)
  2. dm.render()
  3. dm.update(options)
  4. dm.destroy()

selector can be a HTMLElement object or document.querySelector’s parameter, default: body

options

option type default introduction
content String - HTML string or text
fontSize String inherit font size, e.g. 16px,2rem
fontFamily String inherit font family
color String inherit font color
opacity Number .3 opacity of content, 0~1
padding Number 20 the parent element padding, unit: px
zIndex Number 6000 z-index of centent
minMargin Array [40, 20] [vertical, horizontal], margin of item unit: px
rotate Number -15 rotation angle of item
observe Boolean true observe the dom change and rerender, prevent user delete watermarks, work with MutationObserver, see caniuse

Change log

v1.1.0 2020/05/17

rerender when hot module reload, rerender only when watermark change in MutationObserver

Questions & Suggestions

Please open an issue here.

License

MIT