Skip to content

Instantly share code, notes, and snippets.

@olivx
Last active August 11, 2020 13:58
Show Gist options
  • Save olivx/5e1a4eec35cad35ed69c2fe628a2c242 to your computer and use it in GitHub Desktop.
Save olivx/5e1a4eec35cad35ed69c2fe628a2c242 to your computer and use it in GitHub Desktop.
make simple scroll function with vanilla js
<script type="text/javascript">
function slide(src,nome){
document.getElementById(nome).removeAttribute('style');
document.getElementById(nome).setAttribute('style','background-image:url('+src+')');
}
function next (id,project) {
var width=document.getElementById(project).offsetWidth;
var container = document.getElementById(id);
sideScroll(container,'right',18,width,10);
};
function back(id,project) {
var width=document.getElementById(project).offsetWidth;
var container = document.getElementById(id);
sideScroll(container,'left',18,width,10);
};
function sideScroll(element,direction,speed,distance,step){
scrollAmount = 0;
var slideTimer = setInterval(function(){
if(direction == 'left'){
element.scrollLeft -= step;
} else {
element.scrollLeft += step;
}
scrollAmount += step;
if(scrollAmount > distance){
window.clearInterval(slideTimer);
}
}, speed);
}
function openNav(nav) {
document.getElementById(nav).style.width = "100%";
}
function closeNav(nav) {
document.getElementById(nav).style.width = "0%";
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment