JavaScripting

The definitive source of the best
JavaScript libraries, frameworks, and plugins.


  • ×

    Calculates if the mouse position is near to a given element.
    Filed under  › 

    • 🔾7%Overall
    • 134
    • 108.7 days
    • 🕩12
    • 👥2

    is-near

    Calculates if the mouse position is near to a given element.

    It's cross-browser compatible:

    • Chrome
    • Firefox
    • Opera
    • Safari
    • IE9
    • IE8
    • IE7

    Installation

    $ component install pazguille/is-near
    

    See: https://github.com/component/component

    Standalone

    Also, you can use the standalone version:

    <script src="is-near.js"></script>
    

    How-to

    var isNear = require('is-near'),
        box = document.getElementById('box');
    
    document.addEventListener('mousemove', function () {
        var near = isNear(box, 50);
    
        if (near) {
    
            if (near === 'inside') {
                box.style.backgroundColor = '#8e44ad';
                box.innerHTML = 'Inside';
            } else {
                box.style.backgroundColor = '#2ecc71';
                box.innerHTML = 'Yes';
            }
    
        } else {
            box.style.backgroundColor = '#c0392b';
            box.innerHTML = 'No';
        }
    });
    

    API

    isNear(element, [distance])

    Calculates if the mouse position is near to a given element. Returns a boolean value (true or false) or string (inside).

    • element - A given DOMElement.
    • distance [optional] - Minimum distance (in pixels) between the element and mouse position.
    isNear(someElement, 20);
    

    Contact

    License

    Copyright (c) 2013 @pazguille Licensed under the MIT license.

    Show All