我正在尝试检测滚动条的正确位置。有人可以解释一下是否有可能。滚动条缩略图没有固定的宽度。我正在使用nw.js,ES6和jQuery库。
以下是我的代码的简化摘录。
class C {
constructor() {
win.on('resize', this._resize.bind(this));
$('#divId').on('scroll', this._scroll.bind(this));
}
_getXScrollbarThumbPos() {
let lpos = $('#divId').scrollLeft();
let rpos = lpos + SCROLLBAR_THUMBNAIL_WIDTH; // FIXME how can I get the scrollbarThumbnailWidth
return rpos;
}
_resize() {
let x = this._getXScrollbarThumbPos();
//..
}
_scroll() {
let x = this._getXScrollbarThumbPos();
//..
}
}
调整大小和滚动侦听器可以正常工作,唯一的瓶颈是如何确定滚动条缩略图的宽度。win
是DOM窗口的nw.js包装器,请参见此处(此处未显示初始化,因为它与问题无关)。
这是我最终使用的解决方案。尽管由于动态宽度(我还没有检测到计算方法),我还没有找到直接获取右侧滚动条位置的任何尺寸的方法,但实际上我已经能够解决问题; 通过确定总内容的宽度(即可见内容+溢出内容),并减去可见内容宽度并滚动到左侧内容宽度,我们可以确定可滚动内容在视口中的最右端位置,(忽略任何可能的边框宽度)等于scollbar-thumb的正确位置。
class C {
constructor() {
win.on('resize', this._resize.bind(this));
$('#divId').on('scroll', this._scroll.bind(this));
}
_getXScrollbarThumbPos() {
// width of the content + overflow
let totalWidth = $('#divId').scrollWidth;
// width of the visible (non overflowing) content
let viewWidth = $('#divId').width();
// the amount of pixels that the content has been scrolled to the left
let lpx = document.getElementById('#divId').scrollLeft;
// the amount of pixels that are hidden to right area of the view
let rpx = totalWidth - viewWidth - lpx;
// represents the right position of the scrollbar-thumb
console.log(rpx);
return rpx;
}
_resize() {
let x = this._getXScrollbarThumbPos();
//..
}
_scroll() {
let x = this._getXScrollbarThumbPos();
//..
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句