pure javascript to check if something has hover (without setting on mouseover/out)

后端 未结 4 1012
死守一世寂寞
死守一世寂寞 2020-11-29 00:50

I have seen this jQuery syntax:

if($(element).is(\':hover\')) { do something}

Since I am not using jQuery, I am looking for the best way to

4条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 01:26

    it occurred to me that one way to check if an element is being hovered over is to set an unused property in css :hover and then check if that property exists in javascript. its not a proper solution to the problem since it is not making use of a dom-native hover property, but it is the closest and most minimal solution i can think of.

    
        
            
            
        
        
            
    hover here

    (function getStyle thanks to JavaScript get Styles)

    if anyone can think of a better css property to use as a flag than solid/dashed please let me know. preferably the property would be one which is rarely used and cannot be inherited.

提交回复
热议问题