简体   繁体   中英

How to align an element to the bottom of the window using JS

I have created a html-5 banner using Adobe Animate and have an element, which should be aligned to the bottom of the window depending on the height of this window.

My banner should be 100% height and on the first picture you can see the right position of the bottom elements. And on the second picture there is a space between the element and the background. And my element should remain aligned to the bottom when changing the height of the window. How can I do it using js?

Here is my code which helps me move the element but it doesn't align it to the bottom:

this.frame_0 = function() {

    var _second = this.second;

    this.addEventListener("tick", res.bind(this));
    function res() {
    _second.y =  window.innerHeight/2 + 120;

    }
}

This is my file

If use CreateJS then you are using a canvas. CSS solutions don't work, only JS.

Use the Window's resize event.

this.frame_0 = function() {
  var _second = this.second;

  function setSecondToTheBottom() {
    _second.y = window.innerHeight - (_second.nominalBounds.height / 2);
  }

  setSecondToTheBottom();

  window.addEventListener("resize", setSecondToTheBottom);
};

use CSS to set the positions of element.

position:fixed;
left:ValueFromLeft;
bottom:0;

It's easy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM