简体   繁体   English

悬停在10px距离内

[英]Hover within 10px distance

I have aa bunch of text with links. 我有一堆带链接的文字。 I would like to be able to fire an event (some sort of "hover" event) when the cursor is at a distance less than 10px from a given link. 当光标与给定链接的距离小于10px时,我希望能够触发一个事件(某种“悬停”事件)。 I know I can use padding but then that would create extra space between the links and the rest of the text. 我知道我可以使用padding但是这会在链接和文本的其余部分之间创建额外的空间。

Is there a nice way to do this? 有一个很好的方法来做到这一点?

if you put 10px of padding on them, then put -10px of margin on them. 如果你把10px的填充物放在它们上面,那么就把-10px的边距放在它们上面。 That should get you close to the same view before padding. 这应该让你在填充之前接近相同的视图。

you can use this code to see that the other answer has flaws, and a new version: http://jsfiddle.net/25gSY/1/ 您可以使用此代码来查看其他答案是否存在缺陷,以及新版本: http//jsfiddle.net/25gSY/1/

html: HTML:

works here
<a href="#" style="position:relative">
  <span style="position:absolute;top:-20px;left:-20px"></span>example
</a>
and works here

javascript: JavaScript的:

$('span').width($('span').parent().width()+40);
$('span').height($('span').parent().height()+40);
$('span').hover(function(){alert('123')});

This function will work on any element without having to add markup to your HTML. 此函数适用于任何元素,无需在HTML中添加标记。

Demo: http://jsfiddle.net/ThinkingStiff/Lpg8x/ 演示: http//jsfiddle.net/ThinkingStiff/Lpg8x/

Script: 脚本:

$( 'body' ).mousemove( function( event ) {

    if( isNear( $( '#your-element' ), 20, event ) ) {
        //near
    } else {
        //not near
    };

} );           

function isNear( $element, distance, event ) {

    var left = $element.offset().left - distance,
        top = $element.offset().top - distance,
        right = left + $element.width() + ( 2 * distance ),
        bottom = top + $element.height() + ( 2 * distance ),
        x = event.pageX,
        y = event.pageY;

    return ( x > left && x < right && y > top && y < bottom );

};

Well this is a javascript version, though i think i'd personally use Jared's (depends on context..) 这是一个javascript版本,虽然我认为我个人使用Jared的(取决于上下文..)

$("p").bind("mousemove", function(e) {


    var links = $("a", this);

    for(var i = 0 ; i < links.length; i++) {

        var link = $(links[i]);
        var linkPosition = link.position();
        var linkSize = {width: link.width(), height: link.height()};
        var mousePosition = {left: e.pageX, top: e.pageY};

        //match top
        var matchTop = false;
        if(mousePosition.top >= linkPosition.top - 10 && (mousePosition.top <= linkPosition.top + linkSize.height + 10)) {
            matchTop = true;
        }

        //match left
        var matchLeft = false;
        if(mousePosition.left >= linkPosition.left - 10 && (mousePosition.left <= linkPosition.left + linkSize.width + 10)) {
            matchLeft = true;
        }

        if(matchLeft&&matchTop) {
            link.addClass("HOVER");
        } else {
            link.removeClass("HOVER");
        }


    }


});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM