我可以通过javascript在鼠标光标旁边动态添加文本吗?
Can I dynamically add text next to the mouse cursor via javascript?
现在,如果有任何ajax正在使用此javascript代码的过程中,我更改了光标图像
$(function(){
$("html").bind("ajaxStart", function(){
$(this).addClass('busy');
}).bind("ajaxStop", function(){
$(this).removeClass('busy');
});
});
及以下 CSS
html.busy, html.busy * {
cursor: wait !important;
}
现在我也想在光标旁边添加一些文本。并在 ajax 完成后将其删除。如果不使用任何jQuery插件,这怎么可能?
试试这个:
带有启动/停止功能和更改文本的演示
http://jsfiddle.net/SY4mv/18/
请参阅 http://jsfiddle.net/PbAjt/show/:
.CSS:
#cursorText{
position:absolute;
border:1px solid blue; /* You can remove it*/
}
JavaScript:
document.body.onmousemove=moveCursor;
var curTxt=document.createElement('div');
curTxt.id="cursorText";
curTxt.innerHTML="Hello!"; /* Or whatever you want */
document.body.appendChild(curTxt);
var curTxtLen=[curTxt.offsetWidth,curTxt.offsetHeight];
function moveCursor(e){
if(!e){e=window.event;}
curTxt.style.left=e.clientX-curTxtLen[0]+'px';
curTxt.style.top=e.clientY-curTxtLen[1]+'px';
}
根据你想要什么,你可以改变
curTxt.style.left=e.clientX-curTxtLen[0]+'px';
到
curTxt.style.left=e.clientX+'px';
和
curTxt.style.top=e.clientY-curTxtLen[1]+'px';
自
curTxt.style.top=e.clientY+'px';
CSS:
#tooltip {
position: fixed;
background-color: black;
color: white;
padding: 2px;
opacity: 0;
-webkit-border-radius: 3px;
border-radius: 3px;
-webkit-transition: opacity 0.3s ease-in-out;
-moz-transition: opacity 0.3s ease-in-out;
-ms-transition: opacity 0.3s ease-in-out;
-o-transition: opacity 0.3s ease-in-out;
transition: opacity 0.3s ease-in-out;
}
html.busy #tooltip { opacity: 1 }
html.busy, html.busy * {
cursor: wait !important;
}
.HTML:
<div id="tooltip">Message</div>
.JS:
$(function() {
$("html").bind("ajaxStart", function() {
$(this).addClass('busy');
$(this).bind('mousemove', function(event) {
$('#tooltip').css({
top: event.pageY - $('#tooltip').height() - 5,
left: event.pageX
});
});
}).bind("ajaxStop", function() {
$(this).removeClass('busy');
$(this).unbind('mousemove');
});
});
事件文档:http://api.jquery.com/mousemove/
演示:http://jsfiddle.net/RGNCq/1/
相关文章:
- 正在添加动态第二类Jquery
- 如何在javascript而不是jquery中添加动态列表
- 为图例添加动态色彩标签
- 如何在谷歌表单中添加动态字段
- 如何添加动态Id's并在运行时提取它们
- 在 JavaScript 中添加动态文本
- 如何添加动态表单元素但保留其值(JS)
- 使用AngularJs在自定义TinyMCE编辑器中添加动态数据
- 如何使用JavaScript添加动态HTML内容(DIV)
- javascript初学者:在javascript中添加动态样式
- 如何使用Javascript在html源中添加动态数据
- 向Uploadify添加动态字段
- 如何在 Vue.js 中添加动态组件/部件
- 锚导航与坚持到顶部菜单..需要向锚点添加动态偏移量
- Javascript:添加动态方法
- 向角度对象添加动态功能
- 添加动态文本框时更新动态高度
- JavaScript 运行时错误:无法在 Windows 8 应用中添加动态内容
- 添加动态点击事件
- 添加动态字段以创建特定的 JSON 格式