admin管理员组文章数量:1400776
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 toundefined
– lilezek Commented Aug 3, 2017 at 16:14
1 Answer
Reset to default 6There'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()
本文标签: javascriptdoes offset Bottom exist in vanilla jsStack Overflow
版权声明:本文标题:javascript - does offset Bottom exist in vanilla js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744232711a2596414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论