简体   繁体   中英

why there are square gathering at top left corner in this…div painting

I can't figure out why...what am i doing wrong.

For some reasons elements with 0,0 are being created at top.

http://jsfiddle.net/6UamQ/

var m = $('.main'), div = $(document.createElement('div')), d;


m.on('mousemove',function(e){
    d = div.clone();
    d.addClass('paint');
    d.css({top:e.offsetY,left:e.offsetX});
    m.append(d);
});

The problem is that once you've added the div 's to main , you may get mousemove events from those child elements as well. Try filtering out those child events like this:

m.on('mousemove',function(e){
    if( e.target === this ) {
        d = div.clone();
        d.addClass('paint');
        d.css({top:e.offsetY,left:e.offsetX});
        m.append(d);
    }
});

Demonstration

It will work if you replace offsetY and offsetX with pageY and pageX. http://jsfiddle.net/6UamQ/4/

var m = $('.main'), div = $(document.createElement('div')), d;


m.on('mousemove',function(e){
d = div.clone();
d.addClass('paint');
d.css({top:e.pageY,left:e.pageX});
m.append(d);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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