Animate with Jquery through id

Advertisements

I am very new to Jquery. This is also my first time asking a question on here. I am trying to animate this id but I do not know how to apply it with an id. I want my name to move to the left, enlarge, and lessen the opacity right when the code runs, without a button.

$(document).ready(function(){
  $(name).animate({
    left: '250px',
    opacity: '0.5',
    height: '150px',
    width: '150px'
  });
}); 
#name{
    font-size:40px;
    font-weight: 700;
    font-family: Roboto;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="style.css">
        <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
        <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto'>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="resume-animations.js"></script>
    </head>
    <body>
        <div id="name">Christy Kim</div>
    </body>
</html>

>Solution :

Firstly, selectors in jQuery follow CSS rules. Therefore to select the <div id="name"> element you should use $('#name') instead of $(name).

To actually have the element move position it needs to be set to position: absolute in CSS.

With those updates made, your code works fine:

$(document).ready(function() {
  $('#name').animate({
    left: '250px',
    opacity: 0.5,
    height: '150px',
    width: '150px'
  });
});
#name {
  font-size: 40px;
  font-weight: 700;
  font-family: Roboto;
  position: absolute;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<div id="name">Christy Kim</div>

Leave a ReplyCancel reply