我在试着找出滚动条拇指的正确位置。有谁能解释一下这是否可能。滚动条缩略图没有固定的宽度。我正在使用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包装器,参见这里 (这里没有显示初始化,因为它与问题无关)。
发布于 2017-02-04 19:20:41
这就是我最终使用的解决方案。虽然由于动态宽度(也没有检测到计算方法),我还没有找到直接获得右滚动条-拇指位置的任何维度的方法,但实际上我已经解决了这个问题;通过确定总内容的宽度(即可视+溢出内容)的宽度,并减去可视内容的宽度和滚动到左侧的内容宽度,我们可以确定可滚动内容的最右边位置,在视图端口内(忽略任何可能的边框宽度)等于scollbar的正确位置。
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();
//..
}
}https://stackoverflow.com/questions/42027354
复制相似问题