Sticky Notes JavaScript Source Code

Sticky Notes JavaScript Source Code
Code Snippet:Draggable Sticky Notes with Vanilla JS :)
Author: Annkay
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 505
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript source code helps you to create draggable sticky notes on a webpage. The code lets you add and drag sticky notes freely across the page. It allows you to input titles and text for each note and easily delete them. This functionality makes organizing information or reminders interactive and visual.

How to Create Sticky Notes Javascript Source Code

1. First of all, add the necessary HTML structure to your webpage. Include a container for the sticky notes and a form to input titles and text for new notes. Use the given classes and IDs as shown in the code.

<div id="stickies-container"></div>
        <div class="sticky-form">
            <label for="stickytitle">Title for your sticky:</label>
            <input type="text" name="stickytitle" id="stickytitle" />
            <label for="stickytext">Write something down:</label>
            <textarea
                name="stickytext"
                id="stickytext"
                cols="24"
                rows="10"
            ></textarea>
            <button class="button" id="createsticky">Stick it!</button>
        </div>

2. Use the following CSS styles to design the appearance of the sticky notes and the form. The CSS defines the colors, sizes, and positioning for the sticky notes and their form.

html {
  box-sizing: border-box;
  font-family: 'Courier New', Courier, monospace;
}
body {
  background: linear-gradient(to left bottom, #41d8dd, #5583ee) !important;
  min-height: 100vh;
  height: 100%;
  margin: 0;
  position: relative;
  /* overflow: hidden; */
}
#stickies-container {
  padding: 1rem;
}
.drag {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.sticky {
  background: linear-gradient(to left bottom, #d4fc78, #99e5a2);
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
  color: #00243f;
  /* 0 0 150px rgba(0, 0, 0, 0.2); */
  cursor: grab;
  display: inline-block;
  padding: 1rem;
  position: absolute;
  width: 12.5rem;
}
.sticky h3,
.sticky p {
  /* color: #0065b3; */
  color: #00243f;
  pointer-events: none;
}
.sticky h3 {
  border-bottom: dashed 2px #0085e8;
  margin: 0 0 1rem;
  min-height: 1.3rem;
  padding: 0 1.5rem 0.25rem 0;
}
.sticky p {
  margin: 0;
  min-height: 9rem;
}
.sticky .deletesticky {
  color: #0085e8;
  cursor: pointer;
  font-size: 2rem;
  position: absolute;
  right: 0.8rem;
  top: 0.4rem;
}

.sticky-form {
  bottom: 1rem;
  position: absolute;
  right: 1rem;
}
.sticky-form label,
.sticky-form input,
.sticky-form textarea {
  color: #fff;
  display: block;
}
.sticky-form input,
.sticky-form textarea {
  background-color: #f0f9ff44;
  background-clip: padding-box;
  border: 2px dashed #0065b3;
  border-radius: 0.25rem;
  color: #00243f;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  margin-bottom: 0.75rem;
  padding: 0.375rem 0.75rem;
  width: calc(100% - 1.5rem);
}
.sticky-form input:focus,
.sticky-form textarea:focus {
  border: 2px dashed #ffffff;
  outline: none;
}
button.button {
  -moz-user-select: none;
  -ms-user-select: none;
  -webkit-user-select: none;
  background-color: #d4fc78;
  border-radius: 0.25rem;
  border: 1px solid transparent;
  color: #0065b3;
  display: inline-block;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.5;
  padding: 0.375rem 0.75rem;
  text-align: center;
  user-select: none;
  vertical-align: middle;
}

3. Finally, implement the JavaScript functionality to enable the creation, dragging, and deletion of sticky notes. Place the following JavaScript code within a <script> tag at the end of the HTML file or in an external JavaScript file. Make sure the JavaScript is executed after the DOM content is loaded using an event listener (DOMContentLoaded).

'use strict';
document.addEventListener('DOMContentLoaded', () => {
const stickyArea = document.querySelector(
'#stickies-container'
);

const createStickyButton = document.querySelector(
'#createsticky'
);

const stickyTitleInput = document.querySelector('#stickytitle');
const stickyTextInput = document.querySelector('#stickytext');

const deleteSticky = e => {
e.target.parentNode.remove();
};

let isDragging = false;
let dragTarget;

let lastOffsetX = 0;
let lastOffsetY = 0;

function drag(e) {
if (!isDragging) return;

// console.log(lastOffsetX);

dragTarget.style.left = e.clientX - lastOffsetX + 'px';
dragTarget.style.top = e.clientY - lastOffsetY + 'px';
}

function createSticky() {
const newSticky = document.createElement('div');
const html = `<h3>${stickyTitleInput.value.replace(
/<\/?[^>]+(>|$)/g,
''
)}</h3><p>${stickyTextInput.value
.replace(/<\/?[^>]+(>|$)/g, '')
.replace(
/\r\n|\r|\n/g,
'<br />'
)}</p><span class="deletesticky">&times;</span>`;
newSticky.classList.add('drag', 'sticky');
newSticky.innerHTML = html;
//newSticky.style.backgroundColor = randomColor();
stickyArea.append(newSticky);
positionSticky(newSticky);
applyDeleteListener();
clearStickyForm();
}
function clearStickyForm() {
stickyTitleInput.value = '';
stickyTextInput.value = '';
}
function positionSticky(sticky) {
sticky.style.left =
window.innerWidth / 2 -
sticky.clientWidth / 2 +
(-100 + Math.round(Math.random() * 50)) +
'px';
sticky.style.top =
window.innerHeight / 2 -
sticky.clientHeight / 2 +
(-100 + Math.round(Math.random() * 50)) +
'px';
}

function editSticky() {}

function stripHtml(text) {
return text.replace(/<\/?[^>]+(>|$)/g, '');
}

function randomColor() {
const r = 200 + Math.floor(Math.random() * 56);
const g = 200 + Math.floor(Math.random() * 56);
const b = 200 + Math.floor(Math.random() * 56);
return 'rgb(' + r + ',' + g + ',' + b + ')';
}

function applyDeleteListener() {
let deleteStickyButtons = document.querySelectorAll(
'.deletesticky'
);
deleteStickyButtons.forEach(dsb => {
dsb.removeEventListener('click', deleteSticky, false);
dsb.addEventListener('click', deleteSticky);
});
}

window.addEventListener('mousedown', e => {
if (!e.target.classList.contains('drag')) {
return;
}
dragTarget = e.target;
dragTarget.parentNode.append(dragTarget);
lastOffsetX = e.offsetX;
lastOffsetY = e.offsetY;
// console.log(lastOffsetX, lastOffsetY);
isDragging = true;
});
window.addEventListener('mousemove', drag);
window.addEventListener('mouseup', () => (isDragging = false));

createStickyButton.addEventListener('click', createSticky);
applyDeleteListener();
});

That’s all! hopefully, you have successfully integrated this sticky notes JavaScript code into your project. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X