jquery remove()跳回页面顶部

Joh*_*ohn 6 javascript jquery

我写了一个带有覆盖的弹出窗口(与通常的图片显示器不同),用户点击该窗口,并且覆盖层覆盖了用于显示的大图片的屏幕.

问题是,当用户点击"关闭"时,我的功能会淡化图片并覆盖,然后将其删除.当我调用该.remove()函数时,浏览器会关注body标签并滚动到页面顶部.

我尝试通过捕获offset.topco-ords并将它们存储在用户点击的元素的属性中,以及弹出窗口关闭时和之后的方法来进行解决.remove()我已经调用了scrollTo()函数,我使用该函数返回正确的滚动位置(由于某种原因,它会过冲并将元素滚动到顶部).

/*creating the popup layer and picture*/
function newsPopup(obj){

    /***PART ZERO - get the timestamp value of this ***/
    var itemID = $(obj).attr('itemID');
    var storyNumber = $(obj).attr('storyNumber');

    /*adding the identifier for later*/
    var offset = $(obj).offset();
    var roundedOff = Math.round(offset.top);
    $(obj).attr('scrollMeBack', roundedOff);

     /*** the script then continues to create an overlay and picture popup
}

/*function to remove popup*/
function overlayRemove(){
    //first fade out the cover and the container
    $("#NEWS_overlay").fadeOut();
    $("#NEWS_centeringContainer").fadeOut();

    //then remove it from the page completely
    setTimeout('$("#NEWS_overlay").remove();$("#NEWS_centeringContainer").remove();',400);


    /*value to scroll the element back to*/
    var scrollBack = $('[SpringCMSscrollMeBack]').attr('SpringCMSscrollMeBack');
    setTimeout('$(window).scrollTop('+scrollBack+')',401); /*notes the 1 millisecond delay here to mean the scroll ahppen after the item has been removed.*/
    $('[scrollMeBack]').removeAttr('scrollMeBack');

}
Run Code Online (Sandbox Code Playgroud)

为什么.remove()函数会导致跳转到页面顶部?即使scrollTo周围的工作看起来很邋of,因为东西逐渐淡出,但跳到屏幕的顶部,然后回到有问题的元素.

Poo*_*imi 7

请参阅如何在不导致页面滚动的情况下删除位置哈希? ;

或者,您可以return false;停止此行为......