AS POPUP JS
AsPopup.js is a javascript library for creating beautiful, customizable popups, alerts, confirms, prompts, toasts.
Normal Alert
alert("You clicked the button");
AsPopup.js Alert
as.alert({
icon: true,
title: "You clicked the button"
});
😍 Installation
You can install it on your html page by adding a script tag with src given below.
https://asbros.github.io/popup.js/popup.js
Popup
You can create Popups.
as.popup({
title: "You have passed the test !"
});
A popup with title with text.
as.popup({
title: "Here's the title",
text: "This is text."
});
A popup with an icon, title, text.
You can use 5 predefined icons - success, error, warning, info, question OR you can put custom url.
as.popup({
title: "Hurray !",
text: "You have passed the test.",
icon: "success"
});
A popup with an big emoji.
as.popup({
emoji: "😍",
title: "Smile Please",
text: "This is a popup with a big emoji."
});
A popup with custom image, custom width, height and image alt also.
as.popup({
image: "https://placeholder.pics/svg/300x1500",
imageHeight: 1500,
imageWidth: 280,
imageAlt: "A tall image.",
title: "Here's the title",
text: "This is text."
});
A popup with html and javascript.
HTML example
My container has an id of "html-example"
You can give id of an element or you can write html inside html argument.
as.popup({
html: document.getElementById("html-example"),
script: "//you can add script here.;"
});
Close button, Close Icon, Overlay Click
as.popup({
closeBtn: false,
closeIcon: true,
overlayClick: false,
title: "Smile Please",
text: "This is a popup with a big emoji."
});
A popup with auto close timer.
as.popup({
timer: 2000,
title: "Auto Close Popup",
text: "I will auto close after 2000 milliseconds.",
closeBtn: false,
overlayClick: false
});
A custom positioned popup.
There are 6 positions you can give - top-left, top-center, top-right, bottom-left, bottom-center, bottom-right
as.popup({
icon: "success",
position: "top-center",
title: "Your changes have been saved.",
closeBtn: false,
timer: 2000
});
A popup with custom color, background, font, font-size, width, padding.
You can also use color for giving the same color for title & text.
as.popup({
background: "url('https://images.unsplash.com/photo-1644307358784-c6c589c9dcac?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw3fHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=1100&q=60') center",
overlayBackground: "rgba(0,54,255,0.3)",
textColor: "white",
titleColor: " yellow",
font: "roboto",
titleSize: 30,
textSize: 20,
width: 250,
padding: "30px",
title: "Here's the title",
text: "This is text."
});
A popup with buttons.
as.popup({
title: "Custom Buttons",
closeBtn: false,
buttons: [
{html: "Button"},
{html: "Success",type: "success"},
{html: "Error",type: "error"},
{html: "Warning",type: "warning"},
{html: "Info",type: "info"},
{html: "Question",type:"question"},
{html: "Dark",type: "dark"},
{html: "I cannot close Popup.",color: "white",bg: "chocolate",id: "btn-id",close: false}
]
});
You have to create an object for each button in buttons array.
html(string - html): Inner HTML of the button.
close(boolean): If false, then the popup will not close after clicking the button. Default is true.
click(function): function on click the button.
type(string): There are 6 types for buttons :- "success", "error", "warning", "info", "question", "dark".
id(string): id for the button.
bg(string - color): background-color for the button.
color(string - color): color: for the button.
html(string - html): Inner HTML of the button.
close(boolean): If false, then the popup will not close after clicking the button. Default is true.
click(function): function on click the button.
type(string): There are 6 types for buttons :- "success", "error", "warning", "info", "question", "dark".
id(string): id for the button.
bg(string - color): background-color for the button.
color(string - color): color: for the button.
Buttons with click functions.
You can add icons in buttons by adding fontawesome or Bootstrap or Google icons in your page.
as.popup({
title: "Buttons with click functions.",
closeBtn: false,
buttons: [
{html: "WhatsApp",type: "success",click: function(){window.open("https://whatsapp.com/","_blank");}},
{html: "Facebook",type: "info",click: function(){window.open("https://facebook.com/","_blank");}},
]
});
Toast
You can create toasts.
If you will not give timer, the toast will not disappear.
as.toast({
title: "You have passed the test !",
timer: 2500
});
clickToClose toasts.
If clickToClose is true, then if you click the toast, it will disappear.
as.toast({
clickToClose: true,
title: "You have passed the test !",
timer: 2500
});
Types of toasts.
There are 5 types of toasts except default - success, error, warning, info, question
as.toast({
type: "success",
title: "Your changes have been saved.",
timer: 2000
});
A custom positioned toast.
There are 6 positions you can give - top-left, top-center, top-right, bottom-left, bottom-center, bottom-right
as.toast({
position: "top-center",
title: "Your changes have been saved.",
timer: 2000
});
Loader
You can create loading popups.
var loader = as.loader();
loader.show({
timer: 3000,
onHide: function(){
as.toast({
title: "Loader disappeared.",
timer: 1500
});
}
});
loader.show() arguments:-
title(string): Title of the loader, default is "Loading..."
timer(number): The time, after which the loader will disappear. onHide(function): Function called after the timer ends.
icon(string - image source): Image for Loader.
title(string): Title of the loader, default is "Loading..."
timer(number): The time, after which the loader will disappear. onHide(function): Function called after the timer ends.
icon(string - image source): Image for Loader.
loader.hide();
You can give function inside loader.hide(), that function will be executed when the loader disappears.
var loader = as.loader();
loader.show();
fetch("https://api.github.com/users/google")
.then(x => {return x.json();})
.then(x => {
loader.hide(function (){
as.popup({
image: x.avatar_url,
title: x.name,
text: x.type
});
});
});
Alert
You can create Popup Alerts.
as.alert({
title: "Hello, I am title",
text: "I am text",
icon: true
});
You can also give the value of icon false.
Alert with image url.
as.alert({
title: "Hello, I am title",
text: "I am text",
icon: false,
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/OOjs_UI_icon_alert-warning.svg/1024px-OOjs_UI_icon_alert-warning.svg.png"
});
Confirm
You can create Confirmation Popups.
as.confirm({
title: "Are you a web developer ?",
text: 'Click "OK" to yes & click "Cancel" to no.',
icon: true,
onConfirm: function(){
as.popup({
icon: "success",
text: "Wow, this library will help you to build your website"
});
},
onCancel: function(){
as.popup({
icon: "error",
text: "Sorry, this library is only available for web."
});
}
});
You can also give the value of icon false.
Alert with image url.
as.confirm({
title: "Is there a dog ?",
icon: false,
image: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQMxBBstFvh6rsvxD8JgNAayA95dYmhv6ZZDQ&usqp=CAU",
okBtnHtml: "Yes",
cancelBtnHtml: "No",
onConfirm: function(){
as.toast({
title: "Correct",
timer: 2000,
type: "success"
});
},
onCancel: function(){
as.toast({
title: "Wrong",
timer: 2000,
type: "error"
});
}
});
Prompt
You can create Prompt Popups.
as.prompt({
title: "Welcome",
text: " Please enter your name",
icon: true
});