将光标锁定到目标



在颜色框中有一个直径10px的小圆圈。拖动圆圈选择一种颜色。但我不希望用户在拖动光标之前必须把光标放到10px的圆圈内。如果它们靠近圆圈,比如在圆圈周围30px的正方形内,我想让光标捕捉到圆圈的中心。

我知道你可以用jQuery UI捕捉可拖动对象,但是捕捉光标。似乎有所不同。

谢谢你的建议

恶作剧:http://jsfiddle.net/coma/KNnXd/

不要在家里这样做

<div id="area">
    <div>
        <div></div>
    </div>
</div>
<div id="cursor"></div>
CSS

html {
    cursor: none;
}
#cursor {
    background: transparent url(http://telcontar.net/Misc/screeniecursors/Cursor%20arrow%20Aero.png) no-repeat 0 0;
    width: 17px;
    height: 23px;
    position: absolute;
    pointer-events: none;
}
#area {
    height: 300px;
    background-color: #eee;
}
#area > div {
    position: absolute;
    background-color: rgba(0, 0, 0, 0.2);
    padding: 20px;
}
#area > div > div {
    background-color: #333;
    width: 1em;
    height: 1em;
    border-radius: .5em;
    font-size: 10px;
}

JS

$(function() {
    var area = $('#area');
    var circle = area.children('div');
    var cursor = $('#cursor');
    var html = $('html');
    circle.draggable();
    var xo = 0;
    var yo = 0;
    var refresh = function(event) {
        cursor.css({
            left: event.clientX - xo,
            top: event.clientY - yo
        });
    };
    html.mousemove(function(event) {
        refresh(event);
    });
    circle.mousedown(function(event) {
        xo = event.clientX - (circle.offset().left + circle.outerWidth() / 2);
        yo = event.clientY - (circle.offset().top + circle.outerHeight() / 2);
        refresh(event);
    });
    circle.mouseup(function(event) {
        xo = 0;
        yo = 0;
        refresh(event);
    });
});

不能将光标固定在目标上,但可以将目标的中心固定在光标上。

html:

<div id="holder">
    <div id="block"></div>
</div>
css:

#holder{
    height: 40px;
    width: 40px;
    padding: 30px;
    cursor: pointer;
}
#block{
    height: 40px;
    width: 40px;
    background-color: black;
    cursor: move;
}
jquery:

var drag = $('#holder');
drag.draggable({
    start: function() {
        $(document).mousemove(function (e) {
            drag.offset({ top: e.pageY-50, left: e.pageX-50 });
        }).click(function () {
            $(this).unbind("mousemove");
        });
    }
});

下面是一个工作示例:http://jsfiddle.net/Angvs/

从这些回复来看,我似乎不可能做到我想要的,也许你最好不要这样做,否则网站所有者可以为他们的访问者做出选择。

一个类似的效果,不过,可能只是让小圆圈改变可见性,当你滚动它。这样你就不用小心地把鼠标移到它上面了。你可以直接冲向它,直到你得到可见的反馈,然后停下来。

无论如何,谢谢你的回复。

最新更新