• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Simple Moving Watermark for websites using JavaScript

tay_0b

Indie Game Explorer
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
39
Likes
133
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
In this article, we will tackle about Creating a Simple Moving Watermark for websites using JavaScript. Here, I will be providing a simple and free JavaScript plugin or module that generates a simple site watermark. The main goal of this article is to provide IT/CS students and new programmers a reference for enhancing their knowledge and skill using JavaScript Object-Oriented Programming.

How to Create a Simple Moving Watermark using JavaScript?

Creating a watermark for websites is not that complicated. In fact, we can simply add the watermark by adding the HTML script of the watermark on your site interface and adding some styles script for the design. Using JavaScript, we can create a simple Text/Image Watermark for websites by Manipulating the DOM (Document Object Model) and adding some event listener to move the water from the current location to another.

Simple JavaScript Plugin

Below is the script of a simple JS Plugin that generates a moving watermark for websites. The script was written using JS OOP Approach.

  1. class

    SiteWatermark{
  2. constructor(

    id,

    _html,

    defaultLocation =

    'bottom-right'

    ,

    styleOptions =

    {

    }

    ,

    styleConfig,

    _containerStyles,

    _container,

    currentLocation)

    {
  3. /**
  4. * Watermark Styles
  5. */
  6. this

    .styleConfig

    =

    {
  7. width :

    styleOptions.width

    ||

    'auto'

    ,
  8. background :

    styleOptions.background

    ||

    '#e1e1e1'

    ,
  9. padding :

    styleOptions.padding

    ||

    '0.5em 1.5em'

    ,
  10. border :

    styleOptions.border

    ||

    '1px solid #bbafaf'

    ,
  11. position :

    styleOptions.position

    ||

    'fixed'

    ,
  12. "z-index"

    :

    styleOptions.zIndex

    ||

    '99'

    ,
  13. "border-radius"

    :

    styleOptions.borderRadius

    ||

    '15px'

    ,
  14. "min-width"

    :

    '15px'

    ,
  15. opacity :

    styleOptions.opacity

    ||

    '.5'

    ,
  16. "text-align"

    :

    styleOptions.textAlign

    ||

    'left'

    ,
  17. }
  18. defaultLocation =

    defaultLocation.toLowerCase

    (

    )
  19. /**
  20. * set Default window location
  21. */
  22. this

    .setLocation

    (

    defaultLocation)

  23. // Container styles
  24. this

    ._containerStyles =

    this

    ._containerStyles

  25. // Watermark Container Element
  26. this

    ._container =

    document.createElement

    (

    'div'

    )

  27. // Watermark Container Element ID
  28. this

    .id

    =

    id

  29. // Watermark Container Element Content/innerHTML
  30. this

    ._html =

    _html
  31. this

    .currentLocation

    =

    defaultLocation

  32. // Watermark Container Element hover Event Listener
  33. this

    ._container.addEventListener

    (

    'mouseenter'

    ,

    function

    (

    e)

    {
  34. e.preventDefault

    (

    )

    ;
  35. if

    (

    this

    .currentLocation

    ==

    'bottom-right'

    )

    {
  36. this

    .currentLocation

    =

    'bottom-left'
  37. }

    else

    if

    (

    this

    .currentLocation

    ==

    'bottom-left'

    )

    {
  38. this

    .currentLocation

    =

    'top-left'
  39. }

    else

    if

    (

    this

    .currentLocation

    ==

    'top-left'

    )

    {
  40. this

    .currentLocation

    =

    'top-right'
  41. }

    else

    if

    (

    this

    .currentLocation

    ==

    'top-right'

    )

    {
  42. this

    .currentLocation

    =

    'bottom-right'
  43. }
  44. this

    .setLocation

    (

    this

    .currentLocation

    )
  45. this

    .init

    (

    )
  46. }

    .bind

    (

    this

    )

    )
  47. }
  48. /**
  49. * Set Watermark Location
  50. * @param {"bottom-right" || "bottom-left" || "top-left" || "top-right"} location
  51. */
  52. setLocation(

    location)

    {
  53. /**
  54. * Reset container position
  55. */
  56. if

    (

    !!

    this

    .styleConfig

    .top

    )
  57. delete

    this

    .styleConfig

    .top

    ;
  58. if

    (

    !!

    this

    .styleConfig

    .bottom

    )
  59. delete

    this

    .styleConfig

    .bottom

    ;
  60. if

    (

    !!

    this

    .styleConfig

    .right

    )
  61. delete

    this

    .styleConfig

    .right

    ;
  62. if

    (

    !!

    this

    .styleConfig

    .left

    )
  63. delete

    this

    .styleConfig

    .left

    ;

  64. /**
  65. * Update container position
  66. */
  67. if

    (

    location ==

    "bottom-right"

    )

    {
  68. this

    .styleConfig

    .bottom

    =

    ".5em"

    ;
  69. this

    .styleConfig

    .right

    =

    ".5em"

    ;
  70. }

    else

    if

    (

    location ==

    "top-right"

    )

    {
  71. this

    .styleConfig

    .top

    =

    "4em"

    ;
  72. this

    .styleConfig

    .right

    =

    ".5em"

    ;
  73. }

    else

    if

    (

    location ==

    "top-left"

    )

    {
  74. this

    .styleConfig

    .top

    =

    "4em"

    ;
  75. this

    .styleConfig

    .left

    =

    ".5em"

    ;
  76. }

    else

    if

    (

    location ==

    "bottom-left"

    )

    {
  77. this

    .styleConfig

    .bottom

    =

    ".5em"

    ;
  78. this

    .styleConfig

    .left

    =

    ".5em"

    ;
  79. }

    else

    {
  80. this

    .styleConfig

    .bottom

    =

    ".5em"

    ;
  81. this

    .styleConfig

    .right

    =

    ".5em"

    ;
  82. }
  83. }
  84. /**
  85. * Initialize Watermark
  86. */
  87. init(

    )

    {
  88. this

    ._containerStyles =

    Object

    .assign

    (

    [

    ]

    ,

    Object

    .keys

    (

    this

    .styleConfig

    )

    .map

    (

    k =>

    {

    return

    `${

    k}

    :

    ${

    this

    .styleConfig

    [

    k]

    }

    ` }

    )

    )
  89. this

    ._containerStyles =

    this

    ._containerStyles.join

    (

    ";"

    )
  90. this

    ._container.setAttribute

    (

    'style'

    ,

    this

    ._containerStyles)
  91. this

    ._container.setAttribute

    (

    'id'

    ,

    this

    .id

    )
  92. this

    ._container.innerHTML

    =

    this

    ._html
  93. document.body

    .appendChild

    (

    this

    ._container)
  94. }
  95. /**
  96. * Update Watermark Container Styles
  97. * @param {str} style
  98. * @param {str} value
  99. */
  100. setStyle(

    style,

    value)

    {
  101. this

    .styleConfig

    [

    style]

    =

    value
  102. this

    .init

    (

    )
  103. }
  104. /**
  105. * Update Watermark Content
  106. * @param {HTML or string} html
  107. */
  108. setHTML(

    html)

    {
  109. this

    ._html =

    html
  110. this

    .init

    (

    )
  111. }

  112. }
  113. /**
  114. * This simple project was developed by:
  115. * [email protected]
  116. *
  117. * SourceCode has been published @:
  118. * https://sourcecodester.com
  119. *
  120. * 01-07-2023
  121. */

Using the JS Plugin above, developers can simply initiate or generate a watermark for the site that they are building or managing. The created watermarks move around the four (4) corners of the site window when hovered. Each time that the end-users puts the mouse cursor on the watermark, the watermark is automatically moved or transferred to the other corner of the window. Developers can also initiate the watermark with their desired style.

Syntax

Here's a sample snippet that demonstrates the usage of the Simple JS Watermark Plugin.

  1. /**
  2. * new SiteWatermark()
  3. * @param {string} id
  4. * @param {html || string} _html
  5. * @param {"bottom-right" || "bottom-left" || "top-left" || "top-right"} defaultLocation
  6. * @param {object(width, background, padding, border, position, zIndex, borderRadius, opacity, textAlign)} styleOptions
  7. */
  8. var

    styles =

    {
  9. background:

    '#DAEAF1'

    ,
  10. width :

    '300px'

    ,
  11. opacity :

    '.9'

    ,
  12. textAlign :

    'center'

    ,
  13. }
  14. let wm =

    new

    SiteWatermark(

    'SampleWaterMark'

    ,

    "SourceCodester"

    ,

    "top-right"

    ,

    styles)
  15. //Initialize Watermark
  16. wm.init

    (

    )

Change Watermark Container Style/CSS

You can also manipulate or update the Watermark container style using the setStyle() method. See the following snippet for the usage of this method.

  1. /**
  2. * setStyle(style, value)
  3. * Update Watermark Container Styles
  4. * @param {str} style
  5. * @param {str} value
  6. */
  7. wm.setStyle

    (

    'background'

    ,

    '#bbafaf'

    )

Change Watermark Content

You can also manipulate or update the Watermark container content using the setHTML() method. See the following snippet for the usage of this method.

  1. /**
  2. * setHTML(html)
  3. * Update Watermark Content
  4. * @param {HTML or string} html
  5. */
  6. wm.setHTML

    (

    `<

    h2 style=

    "text-center"

    >

    Sample Watermark</

    h2>

    `)

Change Watermark Container Position

You can also manipulate or update the Watermark container position using the setLocation() method. See the following snippet for the usage of this method.

  1. /**
  2. * Set Watermark Location
  3. * @param {"bottom-right" || "bottom-left" || "top-left" || "top-right"} location
  4. */
  5. wm.setHTML

    (

    `<

    h2 style=

    "text-center"

    >

    Sample Watermark</

    h2>

    `)
  6. wm.init

    (

    )

Watermark JS's styleOptions object

Here are the following valid styles that can be set by default using the styleOptions.

  • width = container width
  • background = container background
  • padding = container padding
  • zIndex = container z-index
  • borderRadius = container border-radius
  • opacity = container opacity
  • textAlign = container text-align

If your desired CSS option is not valid for styleOptions, you set it using the setStyle() method.

Example

Here's an example source code script that demonstrates the usage of the Simple Watermark JS plugin.

Interface

index.html

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    Moving Site Watermark using JS</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"

    integrity=

    "sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    /

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"

    integrity=

    "sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"

    crossorigin=

    "anonymous"

    >

  10. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"

    integrity=

    "sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    ></

    script

    >
  11. <script

    src

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"

    integrity=

    "sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"

    crossorigin=

    "anonymous"

    ></

    script

    >
  12. <style

    >
  13. html, body{
  14. height: 100%;
  15. width: 100%;
  16. }
  17. body{
  18. display: flex;
  19. flex-direction: column;
  20. }
  21. body>nav{
  22. flex-shrink: 1;
  23. }
  24. body>#main{
  25. flex-grow: 1;
  26. }
  27. </

    style

    >
  28. </

    head

    >
  29. <body

    >
  30. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"

    >
  31. <div

    class

    =

    "container"

    >
  32. <a

    class

    =

    "navbar-brand"

    href

    =

    "./"

    >

    Moving Site Watermark using JS</

    a

    >
  33. <div

    >
  34. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

    "text-light fw-bolder h6 text-decoration-none"

    target

    =

    "_blank"

    >

    SourceCodester</

    a

    >
  35. </

    div

    >
  36. </

    div

    >
  37. </

    nav>
  38. <div

    class

    =

    "container-fluid px-5 my-3 d-flex h-100 justify-content-center flex-column"

    id

    =

    "main"

    >
  39. <div

    class

    =

    "col-lg-10 col-md-11 col-sm-12 col-xs-12 mx-auto"

    >
  40. <div

    class

    =

    "card rounded-0"

    >
  41. <div

    class

    =

    "card-body bg-white border-3"

    >
  42. <div

    class

    =

    "container-fluid"

    >
  43. <h1

    class

    =

    "text-center"

    >

    Simple Moving when Hovered Watermark for sites using JavaScript</

    h1

    >
  44. </

    div

    >
  45. </

    div

    >
  46. </

    div

    >
  47. </

    div

    >
  48. </

    div

    >
  49. <!-- <div id="0623">
  50. </div> -->
  51. <script

    src

    =

    "./watermark.js"

    ></

    script

    >
  52. <script

    >
  53. var wm;
  54. window.onload = function(){
  55. styles = {
  56. background:'#DAEAF1',
  57. width : '300px',
  58. opacity : '.9',
  59. textAlign : 'center',
  60. }
  61. wm = new SiteWatermark('testWatermark','<a

    class

    =

    "text-decoration-none fw-bolder text-muted"

    href

    =

    "https://sourcecodester.com"

    >

    sourcecodester.com</

    a

    >

    ','top-right' , styles)
  62. wm.init()
  63. }
  64. </

    script

    >
  65. </

    body

    >
  66. </

    html

    >

Result

I have also provided the sample working source code zip file that I created that demonstrates the Simple Watermark JS Plugin. You can download it by clicking the Download button located below.

That's it! I hope this Simple Watermark JS plugin for websites will be useful for your current and future projects.

Happy Coding=)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

325,905

325,913

Top