Can';当使用javascript在div标记中移动鼠标时,是否运行onmousemove?
我有一个示例代码: javascript:Can';当使用javascript在div标记中移动鼠标时,是否运行onmousemove?,javascript,Javascript,我有一个示例代码: javascript: function bootstrap(id) { this.id = id; this.init = function() { document.getElementById(this.id).onmousemove = positionButton; } function positionButton(e) { e = e || window.event; var cursor = {x:0
function bootstrap(id) {
this.id = id;
this.init = function() {
document.getElementById(this.id).onmousemove = positionButton;
}
function positionButton(e) {
e = e || window.event;
var cursor = {x:0, y:0};
if (e.pageX || e.pageY) {
cursor.x = e.pageX;
cursor.y = e.pageY;
} else {
cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
}
var elem = document.getElementById(this.id);
elem.style.position = 'absolute';
elem.style.top = cursor.y - 10 + 'px';
elem.style.left = cursor.x - 30 + 'px';
}
}
在html中:
<div id="button" style="position: absolute; opacity: 1; z-index: 100; width:27px; height:20px; overflow:hidden">
test test
</div>
<script type="text/javascript" src="script.js"></script>
<script>
var bootstrap = new bootstrap('button');
bootstrap.init();
</script>
测试
var bootstrap=新引导(“按钮”);
bootstrap.init();
当我运行代码时,result not run event mousemove,如何修复它?按钮的样式。
您的代码:
width:27px;height:20px;
我换成
width:60px;height:50px;
我在IE9上试用了它。它很有效。运行我创建的,只需稍微修改一下您的代码:
function bootstrap(id) {
this.id = id;
this.init = function() {
document.getElementById(this.id).onmousemove = positionButton;
};
function positionButton(e) {
e = e || window.event;
var cursor = {
x: 0,
y: 0
};
if (e.pageX || e.pageY) {
cursor.x = e.pageX;
cursor.y = e.pageY;
} else {
cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
}
var elem = document.getElementById(this.id);
elem.style.position = 'absolute';
elem.style.top = cursor.y - 10 + 'px';
elem.style.left = cursor.x - 30 + 'px';
}
}
var bootstrap = new bootstrap('button');
bootstrap.init();
它可以与Chrome、Safari和FFX一起正常工作
您的浏览器控制台有任何错误吗
script.js下载是否正确
你用的是什么浏览器