I've recently made a page loader in VanillaJS for a project, just wanted to share it as, all the other answers, are jQuery based. It's a plug and play, one-liner.
let settings={backgroundColor:"#2774ab",filterBrightness:2,timeOnScreen:100},u=document.querySelector("*"),s=document.createElement("style"),a=document.createElement("div"),m="http://www.w3.org/2000/svg",g=document.createElementNS(m,"svg"),c=document.createElementNS(m,"circle");document.head.appendChild(s),s.innerHTML="@keyframes swell{to{transform:rotate(360deg)}}",a.setAttribute("style","background-color:"+settings.backgroundColor+";color:"+settings.backgroundColor+";display:flex;align-items:center;justify-content:center;position:fixed;top:0;height:100vh;width:100vw;z-index:2147483647"),document.body.prepend(a),g.setAttribute("style","height:50px;filter:brightness("+settings.filterBrightness+");animation:.3s swell infinite linear"),g.setAttribute("viewBox","0 0 100 100"),a.prepend(g),c.setAttribute("cx","50"),c.setAttribute("cy","50"),c.setAttribute("r","35"),c.setAttribute("fill","none"),c.setAttribute("stroke","currentColor"),c.setAttribute("stroke-dasharray","165 57"),c.setAttribute("stroke-width","10"),g.prepend(c),u.style.pointerEvents="none",u.style.userSelect="none",u.style.cursor="wait",window.onload=(()=>{setTimeout(()=>{u.style.pointerEvents="",u.style.userSelect="",u.style.cursor="",a.remove()},settings.timeOnScreen)});
Fundamentals
- Generate a
<script>
element appended to the <head>
element, containing any required styling.
- Generate a
<div>
element, acting as overlay, prepended to the <body>
element.
- Generate a
<svg>
element, acting as loader, prepended to the previously generated <div>
element.
- On
window.onload
self generated elements are automatically removed.
The latest version is available on my GitHub.
Settings
let settings = {
backgroundColor: "#2774ab",
filterBrightness: 2,
timeOnScreen: 100
}, //...
Option |
Description |
backgroundColor |
Refer to MDN Web Docs color for acceptable values. The background-color CSS property sets the background color of an element. Default to Wordpress deep blue accent #2774ab |
filterBrightness |
number or percentage. The brightness of the svg loader element (brightness() CSS function). A value under 100% darkens the loader, while a value over 100% brightens it. The lacuna value for interpolation is 1 . Default to 2 . |
timeOnScreen |
Positive integer . The time on screen is appended to the page loading time. Default to 100 milliseconds. |