当鼠标悬停在单元格最左边的 10 个像素上时,我需要更改表格单元格的颜色。目前我有这个:
$("#myTable table thead tr th:nth-child(3)").mouseover(function () {
$(this).css("background-color", "red");
});
$("#myTable table thead tr th:nth-child(3)").mouseout(function () {
$(this).css("background-color", "white");
});
这适用于将鼠标悬停在整个元素上,但我需要它仅在将鼠标悬停在其最左侧 10px 上时发生。
你可以使用mousemove http://jsfiddle.net/antisanity/BLqrF/2/相反并检查偏移坐标:
$("div").on({
mousemove: function (e) {
var $this = $(this);
var x = e.offsetX;
// fix for firefox
if(typeof x === "undefined"){
x = e.pageX - $this.offset().left;
}
$this.css("background-color", x <= 10 ? "red" : "white");
},
mouseleave: function (e) {
$(this).css("background-color", "white");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>test test test test</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)