JavaScript 光标更改(并再次更改回来)
我有这个页面,它执行一些时髦的数据库操作,需要几秒钟的时间来处理,同时我想设置一个 "wait"
光标,这样用户就不会崩溃并保持单击按钮。我查看了
document.body.style.cursor = "wait"
的东西,问题是它仅在鼠标悬停在页面主体上时才起作用(即仍然显示正常指针(如果它位于按钮上方)。如何设置,使得无论鼠标在页面的哪个位置,都显示等待图标?
这个问题的第二部分是,一旦完成,我该如何将其恢复?如果我将其设置回“default”,这似乎会覆盖我在CSS中设置的任何“hover”光标更改(因此当超过时它不再变成手)指定对象等)。
编辑:第一个答案效果很好,但在 IE 中它不会刷新光标(因此您会注意到光标类型的变化),直到您实际移动光标。有修复吗?
I have this page that does some funky database stuff that takes a couple seconds to process, and in the meantime I'd like to set a "wait"
cursor so the user doesn't flip out and keep clicking the button. I've looked at the
document.body.style.cursor = "wait"
thing, the problem with this is that it only works when the mouse is over the body of the page (i.e. still shows normal pointer if it's over a button). How can I set it so that no matter where the mouse is on the page, it shows a wait icon?
A second part to this question is, once it's done it's thing, how do I set it back? If I set it back to "default"
, this seems to override any "hover"
cursor changes I had set in my CSS (so it no longer becomes a hand when over a specified object, etc.).
EDIT: the first answer works nicely, except in IE it doesn't refresh the cursor (so you notice the change of cursor type) until you actually move the cursor. Any fixes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我的建议有两点:
a) 最好编写像这样的 CSS
b) 使用 JS 来处理主体类
您可能想要添加类而不是替换整个类属性,我建议使用 jQuery 之类的东西。
编辑2019:不要仅使用 jQuery,使用 classList
What I suggest is two things:
a) Better write a CSS like
b) Use the JS to handle the body class
You might want to add the class instead of replace the whole class attribute, what I suggest is to use something like jQuery for that.
EDIT 2019: don't use jQuery for just this, use classList
对于您的第一个问题,请尝试使用
cursor: wait !important;
。对于第二个问题,元素的默认光标是
cursor: auto;
,而不是cursor: default;
或cursor:inherit;
。For your first problem, try using
cursor: wait !important;
.For your second problem, the default cursor for elements is
cursor: auto;
, notcursor: default;
orcursor: inherit;
.样式应通过 CSS 处理,如 W3C.com 中所述:
按照 Tom Rogerro 的建议,在 CSS 文件中添加一行:
但是,您的脚本不应覆盖整个类名列表。 Tom 建议通过 jQuery 设置类名,但在这种情况下不需要 jQuery。简单的 JavaScript 就可以做到这一点。
要将类名“waiting”添加到文档正文:
要从文档正文中删除类名“waiting”:
The styling should be handled via CSS, as stated by W3C.com:
As suggested by Tom Rogerro, add a line to your CSS file:
However, your script should not overwrite the entire list of class names. Tom suggested setting the class names via jQuery, but jQuery is unnecessary in this case. Simple Javascript can do this.
To add a class name 'waiting' to the document body:
To remove a class name 'waiting' from the document body:
如果你很高兴使用 JQuery 那么解决这个问题的一个快速方法是使用:
我不知道这有多优雅,但它一直为我工作,
If you are happy using JQuery then a quick way to solve this would be to use:
I don't know how elegant this is but it has been working for me,
不是问题的答案,而是实现目标的一种方式。
使 div(参见下面的类)在加载时可见。
您可以添加动画 gif 来代替光标来指示正在发生的事情。
<代码>.loading{
位置:固定;
高度:100%;
宽度:100%;
左:0;
顶部:0;
光标:等待;
背景:#000;
不透明度:.5;
z-index:999}
Not an answer to the question, but a way of achieving what is wanted.
Make a div (see class below) visible when you are loading.
you can add an animated gif to indicate something is going on instead of the cursor.
.loading{
position:fixed;
height:100%;
width:100%;
left:0;
top:0;
cursor:wait;
background:#000;
opacity:.5;
z-index:999}
默认情况下不继承光标的任何元素(例如按钮)都需要将光标设置为继承:
要返回元素的默认值(并且不会破坏诸如
:hover
之类的内容强制光标),将其设置为空字符串:Any elements that don't inherit the cursor by default (such as buttons) will need to set the cursor to inherit:
To go back to the default for an element (and not break things like
:hover
with a forced cursor), set it to an empty string:我尝试了一切,但最后这个 jquery 起作用了,特别是如果你想等待光标悬停在所有元素上,包括按钮和链接。
在 Angular .ts 文件顶部定义
declare var $: any;
然后在您想要等待光标的位置:
$('*').css('cursor','wait' );
并删除等待:
$('*').css('cursor','auto');
I tried everything but finally this jquery worked, especially if you want wait cursor over all elements including buttons and links.
define at the top of angular .ts file
declare var $: any;
and then where ever you want wait cursor:
$('*').css('cursor','wait');
and remove wait:
$('*').css('cursor','auto');
要完全替换 CSS 切换行为,我们可以简单地使用
this
内联:To fully replace the CSS toggling behaviour, we can simply use
this
inline: