[英]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 :通过X和Y定位滚动条区域:
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.