Lightweight and simple notification library in Vanilla JavaScript

SimpleNotification

SimpleNotification is a library to display simple yet customizable notifications. You can stylize text with a simple syntax, add buttons to make the notifications interactable and add callbacks for some events during the life of a notification.

SimpleNotification weighs 11.12 Kb (3.57 Kb gzipped), with no dependencies.

You can find a live demo here: https://notification.nikurasu.org/

Installation

You simply need to include simpleNotification.css, simpleNotification.js (or the minified versions) and you’re ready to go!

How to use

SimpleNotification has static methods to display notifications with 5 default templates. You can call each of the templates by their name directly on SimpleNotification without instantiating it, like so: SimpleNotification.success(...).

The five templates are success, error, info, warning and message.

SimpleNotification.success({
    title: 'Title', // The title of the notification
    image: 'url', // Optional image displayed inside the notification
    text: 'Content', // Content of the notification
    // Optional list of buttons to interact with the notification
    buttons: [{
        value: 'Confirm', // The text inside the button
        type: 'success', // The type of the button, same as for the notifications
        onClick: (notification) => {
            // The onClick function receive the notification from which the button has been clicked
            // You can call notification.remove(), notification.close() or notification.closeFadeout()
            // if you wish to remove the notification by clicking on  the buttons
        }
    }]
}, options);

All keys in the first parameter are optional, but at least one is required.

You can still update the content and state of any notification after it’s been created with these functions:

setType(type), setTitle(title), setImage(image), setText(text), addButton(button), removeButtons(), disableButtons()

You can use custom classes and make your own design by using SimpleNotification.custom(classes, content, options) where classes is an array of CSS classes that will be added to the body of each notification.

You can jump lines inside the notification content by using any linebreak character (\r, \n or \r\n).

Options

There are a few options that you can set by using SimpleNotification.options(object) or more specifically for a single notification on the third parameter.

Name Description Default
duration The time (in ms) that the notification is displayed. 4000
position Valid positions: top-left, top-center, top-right, bottom-left, bottom-center and bottom-right. “top-right”
sticky If true, the notification will not disappear until the user clicks it or its close button. false
closeButton If true, a close button will be added either on the title or the content. true
closeOnClick If true, clicking anywhere in the notification will close it. true
removeAllOnDisplay If true, all notifications will be cleared before the new one is added to the screen. false
maxNotifications If >0, notifications (starting with oldest) will clear out until the number displayed is less than or equal to the specified option. 0
events Object with events functions, see Events.
insertAnimation Object with CSS class name and duration, see Animations. { name: 'default-insert', duration: 250 }
removeAnimation Object with CSS class name and duration, see Animations. { name: 'fadeout', duration: 400 }
display Display the notification when creating it. true

If a notification is sticky and closeOnClick is disabled, closeButton is set to true to always have a way to close a notification.

Events

There are four events during the process of displaying every notification:

  • onCreate(notification) called when the notification node is created but empty.
  • onDisplay(notification) called when the notification node is appended to its wrapper.
  • onDeath(notification) called when the duration timer has expired.
    • If you set the onDeath function you need to call notification.close(), notification.remove() or notification.closeFadeout() or else the notification won’t disappear.
  • onDisplay(notification) after the notification has been closed.

Animations

You can customize the insert and remove animations of a notification by giving a value to the insertAnimation (or removeAnimation) option. The option take an object like the following:

{
    name: "default-insert", // See the list below
    duration: 250 // In ms
}

The insert animations are: insert-[left|top|right|bottom], fadein, scalein and rotatein.

By default the animation is default-insert which is a special value that automatically choose the corresponding insert animation after the notification position.

The remove animations are: fadeout, scaleout and rotateout.

You can add your own animations by adding them in your own CSS files and setting the name of the animation as the parameter.

Markdown-like tags

You can insert links, images and stylize text by using tags that resemble Markdown. Most of these tags can be nested to combine their effects.

Name Description
Inline code code
Header (h1) # Header 1\n
Header (h2) ## Header 2\n
Link `{{title
Image `![title
Bold **http://www.example.org/**
Italic *http://www.example.org/*
Separator \n---\n

Tags work by looking for an open token, an optional separator if there is a title, and the close token.
If the tag can have a title you need to use | as the separator with the content.

You can add custom tags easily by adding them to SimpleNotification.tags or by using SimpleNotification.addTag(name, object).
A tag object can have the following properties:

{
    type: 'span', // The node type, e.g <span>
    class: ['class1', 'class2'], // Optional class list as an array or string to use
    attributes: {
        name: value
    }, // Optional attributes to set
    textContent: "$content", // textContent of the created node, see below for variables
                             // If textContent is defined and not false the content cannot have childs (nested other tags)
    title: false, // See "Title" below
    open: '{{', // The opening token - any length
    close: '}}' // The closing token - can be linebreak by using \n - can also be empty
}

Variables

There are two usable variables inside attribute values textContent and title:

  • $content: the content found between the open and close token, without the title if there is one.
  • $title: the title found, if there is none it is replaced by the same value as $content.

Download Details:

Author: Glagan

Live Demo: https://notification.nikurasu.org/

GitHub: https://github.com/Glagan/SimpleNotification

#javascript #programming

What is GEEK

Buddha Community

Lightweight and simple notification library in Vanilla JavaScript

JavaScript Snake Game Tutorial - Develop a Simple Snake Game

Work on real-time JavaScript Snake game project and become a pro

Snake game is an interesting JavaScript project for beginners. Snake game is a single-player game, which we’ve been playing for a very long time. The game mainly consists of two components – snake and fruit. And we just need to take our snake to the food so that it can eat and grow faster and as the number of fruits eaten increases, the length of snake increases which makes the game more interesting. While moving if the snake eats its own body, then the snake dies and the game ends. Now let’s see how we can create this.

JavaScript Project Prerequisites

To implement the snake game in JavaScript you should have basic knowledge of:

1. Basic concepts of JavaScript

2. HTML

3. CSS

Download Project Code

Before proceeding ahead please download source code of Snake Game: Snake Game in JavaScript

Steps to Build the Project – JavaScript Snake game

1. Create Html file

HTML builds the basic structure. This file contains some basic HTML tags like div, h1, title, etc. also we’ve used bootstrap (CDN is already included).

index.html:

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DataFlair Snake game</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link rel="stylesheet" href="static/style.css">
</head>
<body>
    <div class="container">
        <div class ="Jumbotron">
        <h1>DataFlair Snake game using vanilla JavaScript</h1>

            <h2 class="btn btn-info"> 
                Score:   0 
            </h2>

            <div class="containerCanvas">
                <canvas id="canvas" width="500" height="500" class="canvasmain"> </canvas>
            </div>
        </div>
    </div>
    <script src="static/fruit.js"></script>
    <script src="static/snake.js"></script>
    <script src="static/draw.js"></script>
</body>
</html>

We have used simple HTML tags except tag. This new tag was introduced in HTML5, which is used to draw graphics, via JavaScript. It is nothing without JavaScript so we will use it in our JavaScript file to actually draw the board and the objects required. We’ll see its implementation in JS files.

#javascript tutorial #javascript project #javascript snake game #simple snake game #javascript

Santosh J

1622036598

JavaScript compound assignment operators

JavaScript is unarguablly one of the most common things you’ll learn when you start programming for the web. Here’s a small post on JavaScript compound assignment operators and how we use them.

The compound assignment operators consist of a binary operator and the simple assignment operator.

The binary operators, work with two operands. For example a+b where + is the operator and the a, b are operands. Simple assignment operator is used to assign values to a variable(s).

It’s quite common to modify values stored in variables. To make this process a little quicker, we use compound assignment operators.

They are:

  • +=
  • -+
  • *=
  • /=

You can also check my video tutorial compound assignment operators.

Let’s consider an example. Suppose price = 5 and we want to add ten more to it.

var price = 5;
price = price + 10;

We added ten to price. Look at the repetitive price variable. We could easily use a compound += to reduce this. We do this instead.

price += 5;

Awesome. Isn’t it? What’s the value of price now? Practice and comment below. If you don’t know how to practice check these lessons.

Lets bring down the price by 5 again and display it.
We use console.log command to display what is stored in the variable. It is very help for debugging.
Debugging let’s you find errors or bugs in your code. More on this later.

price -= 5;
console.log(price);

Lets multiply price and show it.

price *=5;
console.log(price);

and finally we will divide it.

price /=5;
console.log(price);

If you have any doubts, comment below.

#javascript #javascript compound assignment operators #javascript binary operators #javascript simple assignment operator #doers javascript

CSS Boss

CSS Boss

1606912089

How to create a calculator using javascript - Pure JS tutorials |Web Tutorials

In this video I will tell you How to create a calculator using javascript very easily.

#how to build a simple calculator in javascript #how to create simple calculator using javascript #javascript calculator tutorial #javascript birthday calculator #calculator using javascript and html

Rahul Jangid

1622207074

What is JavaScript - Stackfindover - Blog

Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.

Who invented JavaScript?

JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.

What is JavaScript?

JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.

Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.

JavaScript Hello World Program

In JavaScript, ‘document.write‘ is used to represent a string on a browser.

<script type="text/javascript">
	document.write("Hello World!");
</script>

How to comment JavaScript code?

  • For single line comment in JavaScript we have to use // (double slashes)
  • For multiple line comments we have to use / * – – * /
<script type="text/javascript">

//single line comment

/* document.write("Hello"); */

</script>

Advantages and Disadvantages of JavaScript

#javascript #javascript code #javascript hello world #what is javascript #who invented javascript

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.

Does your business need an interactive website or app?

Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.

The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.

Get your business app with JavaScript

For more inquiry click here https://bit.ly/31eZyDZ

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers