scrollbar

Create Custom Scrollbar Generator

Spread the love

In this blog, we use the following controls to customize the browser’s scrollbar and then copy the CSS to use in your own websites!

Note:- I recommend trying different browsers to see how your scrollbar will be styled elsewhere.

Custom Scrollbar Generator Live Example

See the Pen Create Custom Scrollbar Generator by khushal (@mr__khushal) on CodePen.

There are several steps to create a Custom Scrollbar Generator. Which are shown below

Step1:- Create index.html File.

<!DOCTYPE html>
<html>
<head>
    <!----Create By https://www.myprograming.com/---->
    <meta charset="utf-8">
    <title>Custom Scrollbar Generator</title>
    <link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
<style id="styleThumbColor"></style>
<style id="styleTrackColor"></style>
<style id="styleColor"></style>
<style id="styleWidthStyle"></style>
<style id="styleWidth"></style>
<style id="styleBorderRadius"></style>
<style id="styleBorder"></style>

<div class="mainhead">
  <h1>Custom Scrollbar Generator</h1> 
</div>
<div class="appContainer">

  <div class="app">
    <div class="scrollbarControls">
      <h2>Controls</h2>
      <ul>
        <li>
          <div>
            <label for="thumbColor">Scrollbar Thumb:</label>
          </div>
          <div>
            <input type="color" id="thumbColor" name="thumbColor" value="#8f54a0">
          </div>
        </li>
        <li>
          <div>
            <label for="trackColor">Scrollbar Track:</label>
          </div>
          <div>
            <input type="color" id="trackColor" name="trackColor" value="#606060">
          </div>
        </li>
        <li>
          <div>
            <label for="widthStyle">Scrollbar Width Style:</label>
          </div>
          <div>
            <select id="widthStyle" name="widthStyle">
              <option value="none">None</option>
              <option value="thin">Thin</option>
              <option value="auto" selected="selected">Auto</option>
            </select> (Firefox only)
          </div>
        </li>
        <li>
          <div>
            <label for="width">Scrollbar Width:</label>
          </div>
          <div>
            <input type="number" id="width" name="width" value="16"> px
          </div>
        </li>
        <li>
          <div>
            <label for="borderRadius">Scrollbar Border Radius:</label>
          </div>
          <div>
            <input type="number" id="borderRadius" name="borderRadius" value="10"> px
          </div>
        </li>
        <li>
          <div>
            <label for="borderWidth">Scrollbar Border Width:</label>
          </div>
          <div>
            <input type="number" id="borderWidth" name="borderWidth" value="3"> px
          </div>
        </li>
        <li>
          <div>
            <label for="borderStyle">Scrollbar Border Style:</label>
          </div>
          <div>
            <select id="borderStyle" name="borderStyle">
              <option value="none">None</option>
              <option value="dotted">Dotted</option>
              <option value="dashed">Dashed</option>
              <option value="solid" selected="selected">Solid</option>
              <option value="double">Double</option>
              <option value="groove">Groove</option>
              <option value="ridge">Ridge</option>
              <option value="inset">Inset</option>
              <option value="outset">Outset</option>
            </select>
          </div>
        </li>
        <li>
          <div>
            <label for="borderColor">Scrollbar Border Color:</label>
          </div>
          <div>
            <input type="color" id="borderColor" name="borderColor" value="#ffffff">
          </div>
        </li>
      </ul>
    </div>
    <div class="khushaloutput">
        <h1>OUTPUT</h1>
        <div class="scrollbarContainer">
      <h2>Scroll Me...</h2>
      <img width="100%" src="https://www.myprograming.com/wp-content/uploads/2021/09/scrollbar-up.jpg">
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
      tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
      quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
      consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
      cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
      proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
      tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
      quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
      consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
      cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
      proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
      tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
      quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
      consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
      cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
      proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
     
    </div>
    </div>
    <div class="code copySection">
      <h2>COPY THIS CODE</h2>

      <pre id="cssCode">/* ===== Scrollbar CSS ===== */
  /* Firefox */

  * {
    scrollbar-width: <span class="displayWidthStyle">thin</span>;
    scrollbar-color: <span class="displayThumbColor">blue</span> <span class="displayTrackColor">orange</span>;
  }

  /* Chrome, Edge, and Safari */
  *::-webkit-scrollbar {
    width: <span class="displayWidth">12</span>px;
  }

  *::-webkit-scrollbar-track {
    background: <span class="displayTrackColor">orange</span>;
  }

  *::-webkit-scrollbar-thumb {
    background-color: <span class="displayThumbColor">blue</span>;
    border-radius: <span class="displayBorderRadius">20</span>px;
    border: <span class="displayBorderWidth">3</span>px <span class="displayBorderStyle">solid</span> <span class="displayBorderColor">orange</span>;
  }</pre>
      <button class="copy">Click Here (Copy code)</button>
</div>

    <div class="code" id="debugger">
      <h2>Debugger</h2>
      <pre id="styleChecker"></pre>
    </div>
  </div>
</div>



<script >
        /* ===== Helper Functions ===== */
function copyToClipboard (inputText) {
  const el = document.createElement('textarea');
  el.value = inputText;
  el.setAttribute('readonly', '');
  el.style.position = 'absolute';
  el.style.left = '-9999px';
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
  
  /* Alert the copied text */
  alert("Copied the text: " + inputText);
};

function updateStyleChecker() {
  
  if (!(debug)) {
    
    let debuggerElement = document.getElementById('debugger');
    
    if (window.getComputedStyle(debuggerElement).display === "none") {
    
      return;
    
    } else {
      
      debuggerElement.style.display = 'none';  
      
    }
       
  }
  
  let styleCheckerContent = "";
  
  let styleElements = document.getElementsByTagName('style');
  
  for (let i = 0; i < styleElements.length; i++) {
    
    if (!(styleElements[i].id)) {
      
      continue;
      
    }
    
    styleCheckerContent += `id: ${styleElements[i].id}`;
    styleCheckerContent += styleElements[i].innerHTML;
    styleCheckerContent += '<br>';
    
  }
  
  document.getElementById('styleChecker').innerHTML = styleCheckerContent;
   
}

function getInputValue(inputId) {
  
  return document.getElementById(inputId).value;

}

function updateDisplayCSS(className, value) {
  
  let elements = document.getElementsByClassName(className);
  
  for (let i = 0; i < elements.length; i++) {
    
    elements[i].innerHTML = String(value).trim();
    
  }
  
}

function updateThumbColor() {
  
  thumbColor = getInputValue('thumbColor');
  
  document.getElementById('styleThumbColor').innerHTML = `
    *::-webkit-scrollbar-thumb {
      background-color: ${thumbColor};
    }
  `;
  
  document.getElementById('styleColor').innerHTML = `
    * {
      scrollbar-color: ${thumbColor} ${trackColor};
    }
  `;
  
  updateDisplayCSS('displayThumbColor', thumbColor);
  
  updateStyleChecker();
  
}

function updateTrackColor() {
  
  trackColor = getInputValue('trackColor');
  
  document.getElementById('styleTrackColor').innerHTML = `
    *::-webkit-scrollbar-track {
      background: ${trackColor};
    }
  `;
  
  document.getElementById('styleColor').innerHTML = `
    * {
      scrollbar-color: ${thumbColor} ${trackColor};
    }
  `;
  
  updateDisplayCSS('displayTrackColor', trackColor);
  
  updateStyleChecker();
  
}

function updateWidthStyle() {
  
  widthStyle = getInputValue('widthStyle');
  
  document.getElementById('styleWidthStyle').innerHTML = `
    * {
      scrollbar-width: ${widthStyle};
    }
  `;
  
  updateDisplayCSS('displayWidthStyle', widthStyle);
  
  updateStyleChecker();
  
}

function updateWidth() {
  
  width = getInputValue('width');
  
  document.getElementById('styleWidth').innerHTML = `
    *::-webkit-scrollbar {
      width: ${width}px;
    }
  `;
  
  updateDisplayCSS('displayWidth', width);
  
  updateStyleChecker();
  
}

function updateBorderRadius() {
  
  borderRadius = getInputValue('borderRadius');
  
  document.getElementById('styleBorderRadius').innerHTML = `
    *::-webkit-scrollbar-thumb {
      border-radius: ${borderRadius}px;
    }
  `;
  
  updateDisplayCSS('displayBorderRadius', borderRadius);
                   
  updateStyleChecker();
  
}

function updateBorderWidth() {
  
  borderWidth = getInputValue('borderWidth');
  
  document.getElementById('styleBorder').innerHTML = `
    *::-webkit-scrollbar-thumb {
      border: ${borderWidth}px ${borderStyle} ${borderColor};
    }
  `;
  
  updateDisplayCSS('displayBorderWidth', borderWidth);
                   
  updateStyleChecker();
  
}

function updateBorderStyle() {
  
  borderStyle = getInputValue('borderStyle');
  
  document.getElementById('styleBorder').innerHTML = `
    *::-webkit-scrollbar-thumb {
      border: ${borderWidth}px ${borderStyle} ${borderColor};
    }
  `;
  
  updateDisplayCSS('displayBorderStyle', borderStyle);
                   
  updateStyleChecker();
  
}

function updateBorderColor() {
  
  borderColor = getInputValue('borderColor');
  
  document.getElementById('styleBorder').innerHTML = `
    *::-webkit-scrollbar-thumb {
      border: ${borderWidth}px ${borderStyle} ${borderColor};
    }
  `;
  
  updateDisplayCSS('displayBorderColor', borderColor);
                   
  updateStyleChecker();
  
}

/* ===== Setup ===== */
let debug = false;

let thumbColor = getInputValue('thumbColor');
let trackColor = getInputValue('trackColor');
let widthStyle = getInputValue('widthStyle');
let width = getInputValue('width');
let borderRadius = getInputValue('borderRadius');
let borderWidth = getInputValue('borderWidth');
let borderStyle = getInputValue('borderStyle');
let borderColor = getInputValue('borderColor');

updateThumbColor();
updateTrackColor();
updateWidthStyle();
updateWidth();
updateBorderRadius();
updateBorderWidth();
updateBorderStyle();
updateBorderColor();

updateStyleChecker();

/* ===== Event Listeners ===== */
document.getElementById('thumbColor').addEventListener("change", updateThumbColor);
document.getElementById('trackColor').addEventListener("change", updateTrackColor);
document.getElementById('widthStyle').addEventListener("change", updateWidthStyle);
document.getElementById('width').addEventListener("change", updateWidth);
document.getElementById('borderRadius').addEventListener("change", updateBorderRadius);
document.getElementById('borderWidth').addEventListener("change", updateBorderWidth);
document.getElementById('borderStyle').addEventListener("change", updateBorderStyle);
document.getElementById('borderColor').addEventListener("change", updateBorderColor);

document.getElementsByClassName('copy')[0].addEventListener("click", function () {
  
  let copyText = document.getElementById('cssCode').innerText;
  
  copyToClipboard(copyText);
  
});

document.getElementsByClassName('info')[0].addEventListener("click", function () {
  
  let instructions = document.getElementsByClassName("instructions")[0];
  
  if (window.getComputedStyle(instructions).display === "none") {
    
    instructions.style.display = "inline";
    instructions.classList.add("slide-left");
    instructions.classList.remove("slide-right");
                                  
    
  } else {
    
    instructions.classList.add("slide-right");
    instructions.classList.remove("slide-left");
    setTimeout(function () {
      instructions.style.display = "none";
    }, 500);
     
  }
  
});
    </script>
</body>
</html>

Step2:- Create style.css File.



/* ===== App Styling ===== */
.info, .copy, input[type=color] {
  cursor: pointer;
}
 
.mainhead h1{
  text-align: center;
  text-transform: uppercase;
  text-shadow:2px 2px 5px rgba(0, 0, 0, 0.7);
}
.khushaloutput{
  text-align: center;
  background-color: #f5f5f5;
  padding: 5px;
  border-radius: 5px;
}
.info {
  font-size: 1.5em;
  padding: 0;
  width: 40px;
  height: 40px;
  border-radius: 100%;
  border: 4px solid #C8C6D2;
  color: #C8C6D2;
  background-color: inherit;
  transition: all 500ms;
}

.info:hover {
  color: #131313;
  background-color: #C8C6D2;
  font-weight: bold;
  
}

.appContainer {
  align-items: center;
  justify-content: flex-start;
  height: inherit;
  width: inherit;
  padding: 0 40px;
}

.app {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0;
  margin: 0;
}


.scrollbarControls {
  border: 1px solid #000;
  border-radius: 5px;
  padding: 0px;
  background-color: #606060;
 width: 35%;
  color: #000;
}

.scrollbarControls>ul {
  list-style: outside none none;
  margin: 0;
  padding: 0;
}

.scrollbarControls>h2 {
  text-align: center;
  text-transform: uppercase;
}

.copySection {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.copy {
  background-color: gray;
  font-size: 2em;
  border-radius: 5px;
  border: none;
  padding: 10px;
  transition: all 500ms;
}

.copy:hover {
  color: white;
  background-color: #BF9ACA;
}

.scrollbarControls label {
  font-size: 1em;
  text-align: right;
  display: block;
  width: 100%;
}

.scrollbarControls>ul>li {
  border-bottom: 1px solid #000;
  display:flex;
  align-items: center;
  justify-content: center;
}

.scrollbarControls>ul>li:first-child {
  border-top: 2px solid #C8C6D2;
}

.scrollbarControls>ul>li:last-child {
  border-bottom: none;
}

.scrollbarControls>ul>li>div {
  width: 50%;
  height: fit-content;
  display: flex;
  margin: 0;
  padding: 12px;
}

.scrollbarControls>ul>li>div:last-child {
  font-size: 0.8em;
  align-items: center;
  justify-content: left;
}

.scrollbarControls>ul>li>div:first-child {
  align-items: center;
  justify-content: right;
  font-weight: bold;
}

input {
  height: 30px;
  width: 70px;
}

select {
  height: 30px;
  width: 70px;
}
 

input[type=color] {
  border: none;
  margin: 0;
  padding: 0;
}

.scrollbarContainer {
  overflow: scroll;
  width: 200px;
  height: 400px;
  background-color:#f5f5f5;
  padding: 40px;
}
 
 

.code {
  padding: 5px;
  background-color: #f5f5f5;
  font-family: 'Source Code Pro';
  color: #000;
  font-size: 1.2em;
  height: fit-content;
}

 

Create Custom Scrollbar Generator Source Code Download


Spread the love

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *