Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Make an element falling down to the page using html ,css, js

I want to make the grid element to fall down to the page . I used setInterval to repeat the proces (the bottom will decrease so the grid will descend ) . I think I didn't create move() function correctly.I just want to know how can I set the function correctly .

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel= "stylesheet" href ="style.css"></link>
</head>
<body>
    <div class="grid"></div>
    <script src="javascript.js" ></script>
</body>                                                      
</html>
.grid {
    background-color:blue;
    height: 20px;
    width :100px;
    left:600px;
    top:150px;
    position : absolute;
}
var grid =document.querySelector('.grid');

function move () {
    grid.style.bottom-=4;
    grid.style.bottom=grid.bottom +'px';
}

move();
setInterval(move,30);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you would still like to implement your approach to realize this movement, here is some feedback.

Bottom value is String, not numerical (e.g. 300px vs 300)

If you want to manipulate the bottom value of an element, you have to parse the numerical value first, then change it, and then append a 'px' (or whatever unit you're using).

// grid.style.bottom-=4; // subtraction on strings is not allowed
// instead, use:
const currentBottom = parseInt(grid.style.bottom, 10)
grid.style.bottom = (currentBottom - 4) + 'px'

document.getElementById(...).style misses styles from <style> blocks and stylesheets

If you want to get all current styles of a DOM element, you should use window.getComputedStyle. As described in the docs:

getComputedStyle is read-only, and should be used to inspect the element's style — including those set by a element or an external stylesheet

In the snippet below, you can see and compare the values grid.style.bottom and window.getComputedStyle(grid). At first, the first version is empty, but the second has the expected value from the stylesheet.

Alternatively, you could directly apply the style in-line with the HTML element. Then you could use .style as well for accessing the correct value from the beginning.

<div class="grid" style="bottom: 100px"></div>

Check out the fixed version of the snippet below with a delay of 3 seconds for better understanding.

var grid = document.querySelector('.grid');

function move() {
  const style = grid.style.bottom
  const computedStyle = window.getComputedStyle(grid)

  console.log('bottom', style)
  console.log('bottom from computed style', computedStyle.bottom)

  // grid.style.bottom -= 4;
  // grid.style.bottom = grid.bottom + 'px';

  const newBottom = parseInt(computedStyle.bottom, 10) - 4; // parseInt only reads the numeric value from the bottom string
  grid.style.bottom = newBottom + 'px';
}

move();
setInterval(move, 3000);
.grid {
  background-color: blue;
  height: 20px;
  width: 100px;
  left: 100px;
  bottom: 200px;
  position: absolute;
}
<div class="grid"></div>

about 4 years ago · Juan Pablo Isaza Report

0

I would recommend you to use a CSS animation for that, you don't need JavaScript for that.

.grid {
  background-color: blue;
  height: 20px;
  width: 100px;
  left: 100px;
  position: absolute;
  animation: move 1.5s forwards;
}

@keyframes move {
  from {
    bottom: 200px;
  }
  to {
    bottom: 0;
  }
}
<body>
  <div class="grid"></div>
</body>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!