views:

120

answers:

3

Dear experts,

I wanted have a dynamic division of content follow you with the cursor in the web browser space.

I am not a pro at JS so I spent 2 hours to finally debugged a very stupid way to accomplish this.

$(document).ready(function () {
    function func(evt) {
        var evt = (evt) ? evt : event;
        var div = document.createElement('div');

        div.style.position = "absolute";

        div.style.left = evt.clientX + "px";

        div.style.top = evt.clientY + "px";
        $(div).attr("id", "current")

        div.innerHTML = "CURSOR FOLLOW TEXT";

        $(this).append($(div));

        $(this).unbind()
        $(this).bind('mousemove', function () {
            $('div').remove("#current");

        });

        $(this).bind('mousemove', func);

    }

    $("body").bind('mousemove', func)
});

As you can see this is pretty much Brute force and it slows down the browser quite a bit. I often experience a lag on the browser as a drag my mouse from one place to another.

Is there a way to accomplish this easier and faster and more intuitive.

I know you can use the cursor image technique but thats something I'm looking to avoid.

Thanks in advance.

+2  A: 

The performance issue comes from the fact that you're creating/destroying the div every time the mousemove event fires (and also binding/unbinding the event). You can simplify it greatly by creating the div once, attaching it to the document and then just moving it on the mousemove event like follows:

$(document).ready(function () {
    //create the div
    var div = $("<div></div>").css("position", "absolute").text("CURSOR FOLLOW TEXT").appendTo("body");

    //attach the mousemove event
    $("body").bind('mousemove', function(evt) {
        div.css({
            "left": evt.pageX + "px",
            "top": evt.pageY + "px"
        });
    });
});
Alconja
Excellent IDEA!Thanks for your help
Dennis D
But just a one little thing,There is still a lag :(
Dennis D
@Dennis - Is your div content particularly complex or are you running on an old machine? I'm not seeing any lag here... What browser are you testing in, that could also have an effect (I tested in Firefox 3.6)?
Alconja
FF 3.6 and even IEIt is actually exactly the code i'm using.To be honest, the lag is actually more than the previous one i had.Sometimes when I drag it, it doesn't move along with it and just stays there.
Dennis D
+1  A: 

Based on answer by Alconja, this might work better:

$(document).ready(function () {
    //create the div
    var div = $("<div/>",{
        'css': {
            "position": "fixed"
        },
        'text': "CURSOR FOLLOW TEXT"
    }).appendTo("body");

    //attach the mousemove event
    $(window).bind('mousemove mouseover', function(evt) {
        div.offset({left:evt.pageX, top: evt.pageY});
    });
});
azatoth
it doesn't work. Maybe there's a bug I can't see?Thanks anyway.
Dennis D
@Dennis: strange, I've tested it here: http://jsfiddle.net/6GAa8/ and it works for me™
azatoth
lol NVM it works fine in FF3.6.I was using the default IE splitscreen in COFFEECUP.Cheers for your help!
Dennis D
and thanks for the link!
Dennis D
A: 

do you know how I can get an iframe to follow instead of text?

sam