2012-02-26 131 views
6

我已经写了一个带有覆盖层(与通常的图片显示器不相似)的弹出窗口,用户单击该覆盖层并覆盖了获取大图片以进行显示的屏幕。jquery remove()跳回到页面顶部

问题是,当用户点击'关闭'我的功能淡出图片和覆盖,然后删除它们。当我打电话给.remove()功能时,浏览器然后将焦点放在body标签上并滚动到页面的顶部。

我试图通过捕获offset.top co-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'); 

} 

为什么.remove()功能造成跳回到页面顶部?即使在scrollTo左右工作,它看起来马虎,因为东西淡出,但跳转到屏幕的顶部,然后回到有问题的元素。

回答