javascript - does offset Bottom exist in vanilla js? - Stack Overflow

I found this sticky div snippet and altered it so that it appears stuck to the bottom of the window whe

I found this sticky div snippet and altered it so that it appears stuck to the bottom of the window when the div hits the top of the page. I'm just curious is there a method of sorts for offset bottom. Here is the code :-

let Sticky = (function() {
    'use strict';

    let CSS_CLASS_ACTIVE = 'is-fixed';


    let Sticky = {
        element: null,
        position: 0,
        addEvents: function() {
            window.addEventListener('scroll', this.onScroll.bind(this));
        },
        init: function(element) {
            this.element = element;
            this.addEvents();
            this.position = element.offsetTop ;
            this.onScroll();
        },
        aboveScroll: function() {
            return this.position < window.scrollY;
        },
        onScroll: function(event) {
            if (this.aboveScroll()) {
                this.setFixed();
            } else {
                this.setStatic();
            }
        },
        setFixed: function() {
            this.element.classList.add(CSS_CLASS_ACTIVE);
        },
        setStatic: function() {
            this.element.classList.remove(CSS_CLASS_ACTIVE);
        }
    };

    return Sticky;

})();


//  Init Sticky
let sticky = document.querySelector('.sticky');
if (sticky) {
    Sticky.init(sticky);
}

I found this sticky div snippet and altered it so that it appears stuck to the bottom of the window when the div hits the top of the page. I'm just curious is there a method of sorts for offset bottom. Here is the code :-

let Sticky = (function() {
    'use strict';

    let CSS_CLASS_ACTIVE = 'is-fixed';


    let Sticky = {
        element: null,
        position: 0,
        addEvents: function() {
            window.addEventListener('scroll', this.onScroll.bind(this));
        },
        init: function(element) {
            this.element = element;
            this.addEvents();
            this.position = element.offsetTop ;
            this.onScroll();
        },
        aboveScroll: function() {
            return this.position < window.scrollY;
        },
        onScroll: function(event) {
            if (this.aboveScroll()) {
                this.setFixed();
            } else {
                this.setStatic();
            }
        },
        setFixed: function() {
            this.element.classList.add(CSS_CLASS_ACTIVE);
        },
        setStatic: function() {
            this.element.classList.remove(CSS_CLASS_ACTIVE);
        }
    };

    return Sticky;

})();


//  Init Sticky
let sticky = document.querySelector('.sticky');
if (sticky) {
    Sticky.init(sticky);
}
Share Improve this question edited Aug 3, 2017 at 16:32 Mahesh Babariya 4,5706 gold badges43 silver badges55 bronze badges asked Aug 3, 2017 at 16:12 UmarUmar 1132 silver badges8 bronze badges 1
  • document.body.offsetBottom evals to undefined – lilezek Commented Aug 3, 2017 at 16:14
Add a ment  | 

1 Answer 1

Reset to default 6

There's no offsetBottom on an element. Top and left are what are used to calculate the offset. If you want a lot more information you could use getBoundingClientRect. It returns information about the position of the element relative to the viewport. You get an object that looks like this:

{
  bottom: 775,
  height: 775,
  left: 0,
  right: 1322,
  top: 0,
  width: 1322
}

You call it like this:

var firstDiv = document.getElementsByTagName('div')[0]
var rect = firstDiv.getBoundingClientRect() 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744232712a4564330.html

相关推荐

  • javascript - does offset Bottom exist in vanilla js? - Stack Overflow

    I found this sticky div snippet and altered it so that it appears stuck to the bottom of the window whe

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信