4

セルの左端の 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");
});

これは、要素全体にカーソルを合わせた場合に機能しますが、一番左の 10 ピクセルにカーソルを合わせた場合にのみ発生する必要があります。

4

2 に答える 2

4

mousemove代わりに使用して、オフセット座標を確認できます。

$("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>

于 2013-10-12T19:10:26.130 に答える