简体   繁体   English

为整个页面更改鼠标 Cursor?

[英]Change Mouse Cursor for whole page?

I have a drag UI program, where the mouse cursor on the draggable element changes to a grabbing hand on click.我有一个拖动 UI 程序,其中可拖动元素上的鼠标 cursor 在单击时变为抓手。

The problem is, I allow the drag to happen anywhere on screen, and the cursor changes over anchor tags, etc...问题是,我允许拖动发生在屏幕上的任何地方,并且 cursor 改变了锚标签等......

I've tried $('*').addClass('grabbing') , but it's REALLY expensive.我试过$('*').addClass('grabbing') ,但它真的很贵。

Is there a simple easy code efficient way to handle this?有没有一种简单易用的代码高效方法来处理这个问题?

Do it at the CSS level:在 CSS 级别执行此操作:

* {
   cursor: pointer;
}

doing it in jquery forces it to iterate every single node in the dom and rewrite its css classes.在 jquery 中执行它会强制它迭代 dom 中的每个节点并重写其 css 类。 On a long document, that's a huge waste of time.在一个很长的文档上,这是一种巨大的时间浪费。

try this尝试这个

$('body').addClass('grabbing');

OR或者

$(document).addClass('grabbing');

//EDITED ANSWER //编辑答案

$('body').mousedown(function(e){
    $(this).css({'cursor':'pointer'});
}).mouseup(function(e){
    $(this).css({'cursor':'auto'});
});

If you firebug is on, you can see the changes in body style tag.如果您打开了萤火虫,您可以看到 body 样式标签的变化。 But some how it's not working.但有些方法是行不通的。 You can take it as a reference and try similar approach the get solution.您可以将其作为参考并尝试使用类似的方法获取解决方案。

Though this is not a direct answer to this question but I want to draw attentions to that we can solve the problem differently.虽然这不是对这个问题的直接回答,但我想提请注意,我们可以以不同的方式解决问题。

By putting an empty fullscreen div on top of everything except drop zones, activate it after mousedown event.通过在除拖放区之外的所有内容之上放置一个空的全屏 div,在mousedown事件后激活它。 So we can simply change cursor for entire screen through :hover class of that div .所以我们可以通过:hover类的 div 简单地更改整个屏幕的光标。

I tried most of these, but not all elements will display the cursor.我尝试了其中的大部分,但并非所有元素都会显示光标。 The quick and dirty method is to iterate the entire dom and set the cursor for each element.快速而肮脏的方法是迭代整个dom并为每个元素设置光标。

document.querySelectorAll('*').forEach(function(node) {
    node.style.cursor = 'progress';
});

Execute ajax or whatever here....Followed by:在此处执行 ajax 或其他任何内容....其次是:

document.querySelectorAll('*').forEach(function(node) {
    node.style.cursor = 'default';
});

When you need it, you can invoke a single line of javascript:当你需要它时,你可以调用 javascript 的单行:

document.body.style.setProperty('cursor', 'pointer');

When you want to undo it again, you can execute another single line:当你想再次撤消它时,你可以执行另一行:

document.body.removeAttribute('style'); 

You can add a new stylesheet to the page/site.您可以向页面/站点添加新的样式表。

https://codepen.io/Swell_boy/pen/oNMBbKq https://codepen.io/Swell_boy/pen/oNMBbKq

const css = "* { cursor: grabbing !important; }",
  styletag = document.createElement("style");

styletag.type = "text/css";
if (styletag.styleSheet) {
  styletag.styleSheet.cssText = css;
} else {
  styletag.appendChild(document.createTextNode(css));
}

document.addEventListener("mouseup", function myFunction_Default() {
  styletag.remove();
});

document.addEventListener("mousedown", function myFunction_Grab() {
  document.body.appendChild(styletag);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM