Accurately detect mouseover event for a div with rounded corners

前端 未结 5 617
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 08:42

I am trying to detect a mouseover event on a circle. I define the circle div like this:

.circle {
  width: 80px;
  height: 80px;
  -moz-border-radius: 40px;
  -         


        
5条回答
  •  太阳男子
    2021-02-06 09:21

    Just ignore the mouseover event if the mouse's position is too far away. It's really simple. Take the center point of the div, and calculate the distance to the mouse pointer (distance formula = sqrt((x2 - x1)^2 + (y2 - y1)^2)) and if it's larger than the radius of the circle (half the width of the div), it's not in the circle yet.

提交回复
热议问题