I met some basic problem in jquery, but I can’t figure it out.
Here is the code: http://jsfiddle.net/pdQsY/
How do I get the position of half of a div.(mouseover left 200px of the div, output font color #ff0. mouseover right 200px of the div, output font color #336)
I use e.pageX - div.offsetLeft, but this echo NaN, thanks.
Use
.offset().leftinstead of the non-existent jQuery propertyoffsetLeft.For efficiency considerations, you should cache the
$("#main")and$('p')variable. You’re calling this twice in yourmousemovehandler. Themousemovehandler is fired very often, so you end up calling$("#main")over thousand times, easily.Demo: http://jsfiddle.net/pdQsY/2/