计算 div 何时触底的最佳方法?
我有一个动态生成 div 框的应用程序。这些框彼此重叠,并向右下方移动 15px。
但是,当一个框的底部到达页面底部时,我希望下一个框出现在页面顶部。当框的右侧碰到页面的右侧时也是如此(即框不应该能够超出页面)。
下面是我的代码,但它不能按我想要的方式工作。盒子在右侧出现“出界”,然后在左侧出现,并且关于底部,它也不太正确。我想有一个更好的方法来实现这一点,我只是不知道它是什么。
var win = $('<div/>', {
'class': 'window',
'width': this.width,
'height': this.height
}).appendTo('#container');
var containerHeight = $('#container').height();
var maxBottom = (desktopHeight / 2);
var containerWidth = $('#container').width();
var maxRight = desktopWidth;
var prev = win.prev('.window');
if (prev.length > 0) {
win.offset({
left: prev.offset().left + 15,
top: prev.offset().top + 15 });
if (prev.offset().top >= maxBottom){
win.offset({
top: 10
});
}
if (prev.offset().left >= maxRight){
win.offset({
left: 0
})
}
}
I have an application that generates div boxes dynamically. The boxes are overlapping each other, and moved 15px down and to the right.
However, when the bottom of a box hits the bottom of the page I want the next box to turn up on the top on the page. The same goes for when the right side of a box hits the right side of the page (ie. the boxes shouldn't be able to go outside the page).
Below is my code, but it doesn't work as I want. The boxes gets "out of bounds" on the right side before turning up on the left side, and regarding the bottom it's not really right either. I guess there is a better way to accomplish this, I just don't know what it is.
var win = $('<div/>', {
'class': 'window',
'width': this.width,
'height': this.height
}).appendTo('#container');
var containerHeight = $('#container').height();
var maxBottom = (desktopHeight / 2);
var containerWidth = $('#container').width();
var maxRight = desktopWidth;
var prev = win.prev('.window');
if (prev.length > 0) {
win.offset({
left: prev.offset().left + 15,
top: prev.offset().top + 15 });
if (prev.offset().top >= maxBottom){
win.offset({
top: 10
});
}
if (prev.offset().left >= maxRight){
win.offset({
left: 0
})
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查元素的
.offset().top
属性加上元素的.height()
是否等于或大于.height()
> 如果文档:You can check if the element's
.offset().top
property plus the element's.height()
are equal or greater than the.height()
if the document: