Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
           CodeHim.com

Free Web Design Code & Scripts

       
  • Home
  • Code Snippets
    • Accordion
    • Creative
    • Carousel
    • Date & Time
    • Gallery
    • LightBox
    • Menu & Nav
    • Modal
    • Others
    • Portfolio
    • Social Media
    • Text & Input
    • Video Player
    • Zoom
  • HTML & CSS
  • Bootstrap
  • JavaScript
  • Collections
  • About
  • Contact
  • Buy Me a Coffee

Home / Vanilla JavaScript / Sticky Notes JavaScript Source Code

Sticky Notes JavaScript Source Code

January 22, 2024January 10, 2024 by Asif Mughal
Sticky Notes JavaScript Source Code
Share This:
Code Snippet:Draggable Sticky Notes with Vanilla JS :)
Author: Annkay
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 1,016
License: MIT
Edit Code online: View on CodePen
Read More
 Demo
Download (File path is not allowed!)

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.

Similar Code Snippets:

  • JavaScript Add to Cart with Local Storage

    JavaScript Add to Cart with Local Storage

  • JavaScript Digital Clock with Date

    JavaScript Digital Clock with Date

  • JavaScript Image Viewer with Zoom Pan Rotate and Flip

    JavaScript Image Viewer with Zoom Pan Rotate and Flip

  • Animated Weather Card in Vanilla JS

    Animated Weather Card in Vanilla JS

  • Dynamic Pagination in JavaScript

    Dynamic Pagination in JavaScript

  • Cookie Consent Popup in JavaScript

    Cookie Consent Popup in JavaScript

  • JavaScript Compress Image Before Upload

    JavaScript Compress Image Before Upload

  • JavaScript Multiple Choice Questions Code

    JavaScript Multiple Choice Quiz Questions Code

  • JavaScript Slide Up & Down div From Bottom

    JavaScript Slide Up & Down div From Bottom

Asif Mughal

I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.

Categories Vanilla JavaScript
JavaScript Calculate Working Days
Binary Clock in HTML CSS and JavaScript

Leave a Comment Cancel reply

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

  • Categories

    • Accordion 22
    • Bootstrap 81
      • Bootstrap Accordion 1
      • Bootstrap Buttons 4
      • Bootstrap Cards 2
      • Bootstrap Carousel 2
      • Bootstrap Dropdowns 2
      • Bootstrap Forms 6
      • Bootstrap Jumbotron 1
      • Bootstrap Modals 2
      • Bootstrap Navbars 6
      • Bootstrap Panels 1
      • Bootstrap Progress Bars 1
      • Bootstrap Text & Input 3
    • Carousel 58
    • Chart & Graph 15
    • Date & Time 49
    • Gallery 39
    • HTML5 & CSS3 201
    • Layout 17
    • LightBox 19
    • Menu & Nav 95
    • Modal 18
    • Others 58
    • Portfolio 8
    • Social Media 10
    • Text & Input 120
    • Vanilla JavaScript 163
    • Zoom 12

You May Like

Alert (7) Analog Clock (4) Analog Clocks (5) Back to Top (4) Calculator (16) Calendar (5) Chart (3) Color Picker (4) Contact Us Forms (5) Countdown Timer (6) Datepicker (9) Digital Clocks (10) Drag and Drop (4) Dropdown Menu (23) File Uploader (4) Hamburger Menu (9) Horizontal Menu (24) Hover Effects (5) HTML5 Canvas (8) Image Sliders (40) Mega Menu (11) Modal (5) Multi-Level Menu (9) News Ticker (4) Notification (9) Off-Canvas Menu (10) Pagination (5) Parallax (8) Popup Lightbox (20) Progress Bar (5) Range Sliders (4) Scrolling Effects (28) Search Box (4) Select Dropdown (4) Shopping Cart (3) Side Menu (16) Slideshow (19) Social Share Buttons (3) Tabs (9) Text (11) Timeline (5) Timer (4) Tooltip (4) Typing Effect (6) Vertical Menu (14)

Latest Code Snippets

  • Common Open Source Security Vulnerabilities and How to Mitigate ThemCommon Open Source Security Vulnerabilities and How to Mitigate Them
    October 12, 2024
  • Animating Grid-column on Hover Using CSSAnimating Grid-column on Hover Using CSS
    April 1, 2024
  • Star Trails Animation On Mousemove in JavaScriptStar Trails Animation On Mousemove in JavaScript
    March 31, 2024
  • Gravity Button Using Pure CSSGravity Button Using Pure CSS
    March 31, 2024
  • Padlock Toggle Switch in CSSPadlock Toggle Switch in CSS
    March 31, 2024
  • Scroll-driven Animations in Pure CSSScroll-driven Animations in Pure CSS
    March 31, 2024
  • Interactive Notification Center UI in Vanilla JSInteractive Notification Center UI in Vanilla JS
    March 31, 2024
  • Emoji Spinner in Vanilla JavaScriptEmoji Spinner in Vanilla JavaScript
    March 31, 2024
  • Reveal Secret Code Using CSSReveal Secret Code Using CSS
    March 31, 2024
  • Toggle Button in CSS With Stretchable Elastic EffectToggle Button in CSS With Stretchable Elastic Effect
    March 31, 2024

Popular

  • 65+ Login Page in HTML with CSS Code Sample Simple to Difficult - 947,415 views
  • 25+ Best JavaScript Shopping Cart Examples with Demo - 211,945 views
  • Bootstrap Multiselect Dropdown with Checkboxes - 150,951 views
  • Bootstrap 5 Buttons with Icon and Text Tutorial & Demo - 112,364 views
  • 19+ Bootstrap Select Dropdown with Search Box Tutorial & Examples - 101,049 views
  • Bootstrap 5 Sidebar Menu with Submenu Collapse/Hover Tutorial Demo - 88,107 views
  • 19+ Bootstrap 5 Mega Menu Responsive/Drop Down Examples - 86,397 views
  • 99+ Social Media Buttons HTML Code Sample & Tutorial - 86,393 views
  • Bootstrap 4 Modal Popup Login Form Tutorial & Demo - 82,150 views
  • Bootstrap Vertical Menu with Submenu on Click - 80,202 views

Advertisement

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...

CodeHim
  • About
  • Contact Us
  • Terms and Conditions
  • Privacy Policy
Top Categories
  • Bootstrap Snippets
  • JavaScript Snippets
  • HTML CSS Snippets
  • Menu & Nav Snippets
Get in Touch

Follow us on social media to be updated with latest web design code & scripts...

All Rights Reserved © 2026 - CodeHim Inc

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

F5
X