简体   繁体   English

淡出后删除元素

[英]Removing element after fading out

I have some code that does a 90's style mouse trail by appending an image at the cursor coordinates and then fading out. 我有一些代码通过在光标坐标处添加图像然后淡出来完成90年代风格的鼠标跟踪。 At the moment it keeps the appended image on the page but I want to remove it after it has faded out any clues on what to populate in the callback after fadeOut 目前,它会将附加的图像保留在页面上,但是在淡出淡出后,有关其填充回调的所有线索之后,我想将其删除

Of the few options I have tried it seemed to not show the trail as it was removing too early or by removing all of the 在我尝试过的几种选择中,它似乎没有显示踪迹,因为它删除得太早或通过删除所有

(document).mousemove(function(e) {

    pointer = $('<img>').attr({
        'src':'images/sparkle.png'
    });

    $('body').append(pointer);

    pointer.attr('class','pointer').css({
        position:'absolute',
        top: e.pageY +2 ,
        left: e.pageX +2

    }).fadeOut(2000,'easeInOutBounce',function(){
         //something here to remover
    });


});

In the fadeOut complete callback function you can just put this: fadeOut完整的回调函数中,您可以这样:

$(this).remove();

or more optimally, avoiding the jQuery call: 或更优化,避免jQuery调用:

this.parentNode.removeChild(this);

Also, don't forget the var specifier on the declaration of pointer , that being the original cause of your problem, because you only had one global pointer variable. 另外,不要忘记pointer的声明上的var说明符,这是导致问题的原始原因,因为您只有一个全局pointer变量。

That said, it's perfectly possible to write this function without any temporary variables: 也就是说,完全可以在没有任何临时变量的情况下编写此函数:

$(document).mousemove(function(e) {
    $('<img>', {
       'src': 'images/sparkle.png',
       'class': 'pointer'
    })
    .css({
        top: e.pageY + 2,
        left: e.pageX + 2
    })
    .appendTo(document.body)
    .fadeoOut(2000, 'easeInOutBounce', function() {
         this.parentNode.removeChild(this);
    });
});

I've removed the absolute and position CSS attributes on the element, they'd be better off attached to the .pointer class in your CSS style sheet. 我已删除元素上的CSS absolute属性和position属性,最好将它们附加到CSS样式表中的.pointer类。

It should just be pointer.remove() , but you need to locally scope each sparkle. 它应该只是pointer.remove() ,但是您需要在本地范围内处理每个闪闪发光。 At the moment they share a single global var called pointer as you have not declared it local (ie with var ). 目前,它们共享一个称为pointer全局变量,因为您尚未将其声明为局部变量(即,使用var )。

eg 例如

$(document).mousemove(function(e) {
    var pointer = $('<img>').attr({
        'src':'images/sparkle.png'
    });

    $('body').append(pointer);

    pointer.attr('class','pointer').css({
        position:'absolute',
        top: e.pageY +2 ,
        left: e.pageX +2

    }).fadeOut(2000,'easeInOutBounce',function(){
         //something here to remover
         pointer.remove();
    });
});

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

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