简体   繁体   English

有什么方法可以使用 javascript 激活浏览器滚动条的右键单击

[英]Is there any way to activate right click on scroll bar of browser using javascript

how to use right click on scroll bar scroller如何使用右键单击滚动条滚动条

generally when we left click on scroll bar path then we move to position where mouse clicked but when we right click on scroll bar it do nothing.通常,当我们左键单击滚动条路径时,我们会移动到 position 鼠标单击的位置,但是当我们右键单击滚动条时,它什么也不做。

You can simulate the events of clicking the right mouse button using the event contextmenu .您可以使用事件contextmenu模拟单击鼠标右键的事件。

Next, we disable the default behavior of event contextmenu - we disable the appearance of the context menu:接下来,我们禁用事件contextmenu菜单的默认行为——我们禁用上下文菜单的外观:

event.preventDefault();

And with the help of calculations within the if {... } condition, we get the result we need.if {... }条件下的计算帮助下,我们得到了我们需要的结果。 Target the scrollbar area, both by X and by Y :通过XY定位滚动条区域:

event.offsetX > event.target.clientWidth || event.offsetY > event.target.clientHeight

Accordingly, instead of console.log("Right click on the scrollbar;");因此,而不是console.log("Right click on the scrollbar;"); , you can use any logic that needs to be called. ,您可以使用任何需要调用的逻辑。

 window.addEventListener("contextmenu", function (event) { if (event.offsetX > event.target.clientWidth || event.offsetY > event.target.clientHeight) { event.preventDefault(); console.log("Right click on the scrollbar;"); } });
 body { height: 5000px; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以使用JavaScript控制滚动条吗? - Is there any way to control scroll bar using JavaScript? 有没有办法'模拟'右键单击save-as命令或强制在浏览器中使用JavaScript下载文件? - Is there any way to 'simulate' right-click save-as command or force download of file in the browser with JavaScript? 如何使用航点激活滚动条上的进度条? - How to activate progress bar on scroll using waypoints? 单击浏览器滚动条关闭弹出窗口 - click on browser scroll bar close popup 在浏览器中使用Javascript右键单击时禁用“导出到MS Excel”选项 - Disabling Export to MS Excel option on right click in browser using Javascript 有没有办法在不使用 jquery 的情况下检查 Javascript 中何时出现滚动条? - Is there a way to check when a scroll bar appears in Javascript without using jquery? 在内部使用javascript代码时无法解释的浏览器滚动条 <div> 设置为overflow-y:scroll; - Unexplained browser scroll bar when using javascript code inside <div> set to overflow-y:scroll; 要在页面滚动中激活的javascript - javascript to be activate on page scroll 有什么方法可以检测浏览器是否停止滚动重画? - Any way to detect if a browser halts repainting on scroll? 使用右键单击激活ap:ajax事件 - activate a p:ajax event where using right click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM