[]分享:自定义鼠标(光标)样式——基于body修改style

使用社区实例无果后,在小明大佬的点拨下,实现了自定义功能。
定义:
cursor: url(' # ');# = 光标文件地址      (注意文件格式必须为:.cur 或 .ani)。
注意:定义完自定义的游标后在末尾加上一般性的游标,以防url所定义的游标不能使用。
//=================
一、自定义位置:
位置1:若游戏进入前修改光标样式,可直接在index.html中定义body的style实现。
<body style="cursor:url('mouse/mouseb.png'),url('mouse/aero_unavail_xl.cur'),pointer">
位置2:若进入游戏后修改光标样式,可通过获取body再定义style实现。
//鼠标样式
var body = laya.utils.Browser.window.document.getElementsByTagName("body")[0];
body.setAttribute('style', "cursor:url('mouse/mouseb.png'),url('mouse/aero_unavail_xl.cur'),pointer");
//=================
二、注意的地方:
路径:路径以index.html所在目录为根目录,此时光标样式资源的路径,即为我们需要的路径。
兼容:由于不同浏览器间不兼容的尿性,可对相同样式设置多种格式(.ani、.cur、.png),爱用谁就用谁,总会找到真爱的!
body.setAttribute('style', "cursor:url('mouse/mouseb.png'),url('mouse/mouseb.cur'),url('mouse/mouseb.ani'),pointer");
位置:当光标有误差时,可直接设值来调整光标的位置。
body.setAttribute('style', "cursor:url('mouse/mouseb.cur') 2 2,pointer");
已邀请:

Chuan

赞同来自:

修改:可直接获取body
var body = laya.utils.Browser.window.document.body;

要回复问题请先

商务合作
商务合作