scrolltotopjs
Version:
Lightweight JavaScript code to create scrollToTop button.
15 lines • 1.26 kB
JavaScript
/*! Scroll To Top v1.0.0 | Copyright by rafifadlimulya | https://github.com/rafifadlimulya/scrollToTop.js/*/
function scrolltotop(s){
var default_options = {
pVertical: 'bottom',
pHorizontal: 'right',
backgroundColor: '#4242A0',
color: '#fff'
}
var option = Object.assign(default_options, s);
var button = document.createElement('scrolltotop');
button.setAttribute('style', `user-select: none; cursor: pointer; display: flex; justify-content: center; align-items: center; color: ${option.color}; position: fixed; ${option.pVertical}: 30px; ${option.pHorizontal}: 30px; height: 40px; width: 40px; background: ${option.backgroundColor};`);
button.setAttribute('onclick', 'document.body.scrollTop = 0; document.documentElement.scrollTop = 0;')
button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512" style="width: 12px;"><path fill="currentColor" d="M374.6 246.6C368.4 252.9 360.2 256 352 256s-16.38-3.125-22.62-9.375L224 141.3V448c0 17.69-14.33 31.1-31.1 31.1S160 465.7 160 448V141.3L54.63 246.6c-12.5 12.5-32.75 12.5-45.25 0s-12.5-32.75 0-45.25l160-160c12.5-12.5 32.75-12.5 45.25 0l160 160C387.1 213.9 387.1 234.1 374.6 246.6z"/></svg>'
document.body.appendChild(button);
}