Stats Range Slider using HTML CSS SASS and JavaScript

Web Design

Description:

In this tutorial we will create some range sliders using HTML5 CSS SASS and Javascript.

The HTML part is not log so our main focus is on the CSS/ SASS part of the project and also just a tinch of DOM manipulation using JavaScript by selecting element from the DOM with the help of queryselector and queryselectorAll in order to grab on to some ID`s from the html .

I particularly like SASS because it gives my the possibility to create partial elements which are reusable in other later projects.

Code for this tutorial

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="/dist/css/style.min.css" />
    <title>Stats Slider</title>
  </head>
  <body>
    <h1 class="page_title">
      Stats Slider using <span>HTML</span>,<span> CSS</span
      ><span> SASS</span> and <span>JavaScript</span>
    </h1>
    <main class="main_container">
      <section class="Selection_container">
        <h2>User Name</h2>
        <div>
          <img src="/img/azamat-zhanisov-wO4gtmdlW5Y-unsplash.jpg" alt="" />
        </div>
      </section>
      <section id="Sliders">
        <div class="container_slider">
          <input id="slider_Str" type="range" min="0" max="100" value="50" />
          <div id="selector">
            <div id="SelectorBtn"></div>
            <div id="SelectorValue" class="SelectorValue_Str"></div>
          </div>
          <div id="SelectorBar" class="Str"></div>
        </div>
        <div class="container_slider">
          <input id="slider_Agi" type="range" min="0" max="100" value="50" />
          <div id="selector">
            <div id="SelectorBtn"></div>
            <div id="SelectorValue" class="SelectorValue_Agi"></div>
          </div>
          <div id="SelectorBar" class="Agi"></div>
        </div>
        <div class="container_slider">
          <input id="slider_Int" type="range" min="0" max="100" value="50" />
          <div id="selector">
            <div id="SelectorBtn"></div>
            <div id="SelectorValue" class="SelectorValue_Int"></div>
          </div>
          <div id="SelectorBar" class="Int"></div>
        </div>
      </section>
    </main>
    <script src="app.js"></script>
  </body>
</html>

CSS

*,
::before,
::after {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
  font-size: 16px;
  font-family: sans-serif;
  color: #ffffff;
}

body {
  background-color: #333;
}

body .page_title,
body span {
  text-align: center;
  font-size: 5rem;
  margin-top: 3rem;
}

body .page_title span:first-child,
body span span:first-child {
  color: #e44d26;
}

body .page_title span:nth-child(2),
body span span:nth-child(2) {
  color: #264de4;
}

body .page_title span:nth-child(3),
body span span:nth-child(3) {
  color: #c69;
}

body .page_title span:nth-child(4),
body span span:nth-child(4) {
  color: #ffcf2a;
}

.main_container {
  display: -ms-grid;
  display: grid;
  -ms-grid-columns: (1fr)[2];
      grid-template-columns: repeat(2, 1fr);
  width: 900px;
  height: 600px;
  margin: 15% auto;
  background-color: rgba(0, 0, 0, 0.3);
}

.main_container .Selection_container h2 {
  font-size: 3rem;
  padding: 2rem;
}

.main_container .Selection_container img {
  height: 400px;
  width: 250px;
  position: relative;
  margin-left: 2rem;
  -webkit-box-shadow: 0 0 15px 5px rgba(0, 0, 0, 0.8);
          box-shadow: 0 0 15px 5px rgba(0, 0, 0, 0.8);
}

#Sliders {
  display: -ms-grid;
  display: grid;
  -ms-grid-rows: (1fr)[3];
      grid-template-rows: repeat(3, 1fr);
  gap: 5rem;
  padding: 25% 2rem;
}

#Sliders .container_slider {
  position: relative;
  width: 100%;
  font-family: bold;
  letter-spacing: 2px;
}

#Sliders .container_slider:first-child::before {
  content: "Strenght";
  position: relative;
  top: 20px;
  left: -30%;
}

#Sliders .container_slider:nth-child(2)::before {
  content: "Agility";
  position: relative;
  top: 20px;
  left: -30%;
}

#Sliders .container_slider:nth-child(3)::before {
  content: "Intelligence";
  position: relative;
  top: 20px;
  left: -30%;
}

#Sliders #slider_Str,
#Sliders #slider_Agi,
#Sliders #slider_Int {
  -webkit-appearance: none;
  width: 100%;
  height: 7px;
  outline: none;
  border-radius: 3px;
}

#Sliders #slider_Str::-webkit-slider-thumb,
#Sliders #slider_Agi::-webkit-slider-thumb,
#Sliders #slider_Int::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 3rem;
  height: 5px;
  cursor: pointer;
  z-index: 3;
  position: relative;
}

#Sliders #selector {
  height: 4rem;
  width: 3rem;
  position: absolute;
  bottom: -15px;
  left: 50%;
  -webkit-transform: translateX(-50%);
          transform: translateX(-50%);
  z-index: 2;
}

#Sliders #selector #SelectorBtn {
  position: absolute;
  bottom: 0;
  height: 3rem;
  width: 3rem;
  background-image: url(/img/azamat-zhanisov-wO4gtmdlW5Y-unsplash.jpg);
  background-size: cover;
  background-position: top center;
  border-radius: 50%;
}

#Sliders #selector #SelectorValue {
  position: relative;
  top: -100%;
  width: 3rem;
  height: 3rem;
  color: #333;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 10%;
  text-align: center;
  line-height: 3rem;
  font-size: 1.5rem;
  font-weight: 700;
}

#Sliders #selector #SelectorValue::after {
  content: "";
  border-left: 23px solid transparent;
  border-right: 23px solid transparent;
  position: absolute;
  bottom: -20px;
  left: 0;
}

#Sliders #selector .SelectorValue_Str::after {
  content: "";
  border-top: 15px solid red;
}

#Sliders #selector .SelectorValue_Agi::after {
  content: "";
  border-top: 15px solid green;
}

#Sliders #selector .SelectorValue_Int::after {
  content: "";
  border-top: 15px solid blue;
}

#Sliders #SelectorBar {
  position: absolute;
  top: 26px;
  left: 0;
  width: 50%;
  height: 5px;
  border-radius: 3px;
}

#Sliders .Str {
  background-color: red;
  -webkit-box-shadow: 0 0 15px 5px red;
          box-shadow: 0 0 15px 5px red;
}

#Sliders .Agi {
  background-color: green;
  -webkit-box-shadow: 0 0 15px 5px green;
          box-shadow: 0 0 15px 5px green;
}

#Sliders .Int {
  background-color: blue;
  -webkit-box-shadow: 0 0 15px 5px blue;
          box-shadow: 0 0 15px 5px blue;
}

JavaScript

const sliderStr = document.querySelector("#slider_Str");
const sliderAgi = document.querySelector("#slider_Agi");
const sliderInt = document.querySelector("#slider_Int");

const selector = document.querySelectorAll("#selector");

const SelectorValues = document.querySelectorAll("#SelectorValue");

const SelectorBar = document.querySelectorAll("#SelectorBar");

sliderStr.oninput = function() {
  SelectorValues[0].innerHTML = this.value;
  selector[0].style.left = this.value + "%";
  SelectorBar[0].style.width = this.value + "%";
};
sliderAgi.oninput = function() {
  SelectorValues[1].innerHTML = this.value;
  selector[1].style.left = this.value + "%";
  SelectorBar[1].style.width = this.value + "%";
};
sliderInt.oninput = function() {
  SelectorValues[2].innerHTML = this.value;
  selector[2].style.left = this.value + "%";
  SelectorBar[2].style.width = this.value + "%";
};

Also check out my Web Development Courses

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.