Home > Back-end >  Uncaught TypeError: $ is not a function $('#loader').hide()
Uncaught TypeError: $ is not a function $('#loader').hide()

Time:01-13

I want to add my page to a loading spinner. I want to hide spinner when page loaded. I want to show that when button clicked and i want to hide again when json process is finished. I can't continue because I got an error even in the first step. Here is the code i got in the first step :

Uncaught TypeError: $ is not a function

All HTML, CSS and Javascript codes are below:

window.addEventListener('load', function () {
      $('#loader').hide();

     })
.loader {
    position: absolute;
    top: calc(50% - 32px);
    left: calc(50% - 32px);
    width: 64px;
    height: 64px;
    border-radius: 50%;
    perspective: 800px;
  }
  
  .inner {
    position: absolute;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    border-radius: 50%;  
  }
  
  .inner.one {
    left: 0%;
    top: 0%;
    animation: rotate-one 1s linear infinite;
    border-bottom: 3px solid green;
  }
  
  .inner.two {
    right: 0%;
    top: 0%;
    animation: rotate-two 1s linear infinite;
    border-right: 3px solid green;
  }
  
  .inner.three {
    right: 0%;
    bottom: 0%;
    animation: rotate-three 1s linear infinite;
    border-top: 3px solid green;
  }
  
  @keyframes rotate-one {
    0% {
      transform: rotateX(35deg) rotateY(-45deg) rotateZ(0deg);
    }
    100% {
      transform: rotateX(35deg) rotateY(-45deg) rotateZ(360deg);
    }
  }
  
  @keyframes rotate-two {
    0% {
      transform: rotateX(50deg) rotateY(10deg) rotateZ(0deg);
    }
    100% {
      transform: rotateX(50deg) rotateY(10deg) rotateZ(360deg);
    }
  }
  
  @keyframes rotate-three {
    0% {
      transform: rotateX(35deg) rotateY(55deg) rotateZ(0deg);
    }
    100% {
      transform: rotateX(35deg) rotateY(55deg) rotateZ(360deg);
    }
  }
  #loading {
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: fixed;
    display: block;
    opacity: 0.7;
    background-color: #fff;
    z-index: 99;
    text-align: center;
    }
  #loading-image {
    position: absolute;
    top: 100px;
    left: 240px;
    z-index: 100;
    }
<div id="loading">
  <div >
    <div ></div>
    <div ></div>
    <div ></div>
  </div>
</div>

How can i solve this problem ?

CodePudding user response:

it seems like your using Vanilla JS, so you need to use querySelector, or otherwise if you're using jQuery, so you need to call the jQuery library at your header.

so incase your using the Vanila JS so you can call it like this

window.addEventListener('load', function () {
      setTimeout( () => {document.querySelector('.loader').style = 'display:none;'}, 2000);

     })
.loader {
    position: absolute;
    top: calc(50% - 32px);
    left: calc(50% - 32px);
    width: 64px;
    height: 64px;
    border-radius: 50%;
    perspective: 800px;
  }
  
  .inner {
    position: absolute;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    border-radius: 50%;  
  }
  
  .inner.one {
    left: 0%;
    top: 0%;
    animation: rotate-one 1s linear infinite;
    border-bottom: 3px solid green;
  }
  
  .inner.two {
    right: 0%;
    top: 0%;
    animation: rotate-two 1s linear infinite;
    border-right: 3px solid green;
  }
  
  .inner.three {
    right: 0%;
    bottom: 0%;
    animation: rotate-three 1s linear infinite;
    border-top: 3px solid green;
  }
  
  @keyframes rotate-one {
    0% {
      transform: rotateX(35deg) rotateY(-45deg) rotateZ(0deg);
    }
    100% {
      transform: rotateX(35deg) rotateY(-45deg) rotateZ(360deg);
    }
  }
  
  @keyframes rotate-two {
    0% {
      transform: rotateX(50deg) rotateY(10deg) rotateZ(0deg);
    }
    100% {
      transform: rotateX(50deg) rotateY(10deg) rotateZ(360deg);
    }
  }
  
  @keyframes rotate-three {
    0% {
      transform: rotateX(35deg) rotateY(55deg) rotateZ(0deg);
    }
    100% {
      transform: rotateX(35deg) rotateY(55deg) rotateZ(360deg);
    }
  }
  #loading {
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: fixed;
    display: block;
    opacity: 0.7;
    background-color: #fff;
    z-index: 99;
    text-align: center;
    }
  #loading-image {
    position: absolute;
    top: 100px;
    left: 240px;
    z-index: 100;
    }
<div id="loading">
  <div >
    <div ></div>
    <div ></div>
    <div ></div>
  </div>
</div>

PS. i'm using Timeout with 2seconds because no thing to load.

CodePudding user response:

It seems like you are using jquery, so make sure you have added <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> in the head tag.

Also change $('#loader') to $('.loader') because there isn't any div with id loader.

  • Related