我也遇到过类似情况:
我相信滚动条实际上并不是网页的一部分——它是一个操作系统级别的组件。但是,我能够通过监视鼠标在父元素上的位置来解决我的问题(通过 css 具有初始高度和宽度值 - 这可能是可选的,我不确定。我下面的代码稍微使用了不同的上下文,但我认为它仍然适用)。
在需要滚动条时重新调整子元素的宽度(在 Chrome 中宽度减少 18 像素)。但是,父元素/容器的宽度保持不变。因为它保持相同的宽度,我们可以添加一个 mousemove 事件并检查光标的位置是否落入滚动条出现在子元素中的 18px 间隙中。
此外,根据您所说的滚动条(整个条;轴、按钮、拇指和所有)或滚动条组件的确切含义,您可以在进行一些计算的帮助下实现该功能。
整个滚动条 - 鼠标悬停
$(".parent").bind("mousemove",function(e){
if($(".partent").width() <= e.offsetX){
//display tool-tip div
}else{
//If displayed, hide tool-tip div
}
});
滚动条拇指 - 鼠标悬停
$(".parent").bind("mousemove",function(e){
if($(".child").width() <= e.offsetX){
var scrollbarHeight = $(".parent").height() - 36; //36 = height of both up and down arrows
var scrollbarThumbHeight = scrollbarHeight/$(".child").height();
var scrollTopPosition = $(".parent").get(0).scrollTop;
var relativeThumbPosition = (childScrollTop/$(".child").height()) + 18;//18 = offset from the up button
if(e.offsetY >= relativeThumbPosition && e.offsetY <= relativeThumbPosition+scrollbarThumbHeight){
//display tooltip div
}else{
//If displayed, hide tool-tip div
}
}else{
//If displayed, hide tool-tip div
}
});
额外的鼠标移出
$(".parent").bind("mouseout",function(e){
if($(".child").width() <= e.offsetX){
//If displayed, hide tool-tip div
}
});
我只在 Windows 7 上的 Google-Chrome 中对此进行了测试,我认为魔术数字 (36,18) 需要针对不同的操作系统进行调整,但值会相对相似。