【发布时间】:2012-02-26 20:45:45
【问题描述】:
我编写了一个带有叠加层的弹出窗口(与通常的图片显示器没有什么不同),用户在其中单击,并且叠加层覆盖了屏幕,该屏幕会获取大图片进行显示。
问题是,当用户单击“关闭”时,我的功能会淡出图片并覆盖,然后将其删除。当我调用.remove() 函数时,浏览器会关注body 标签并滚动到页面顶部。
我试图通过捕获offset.top 坐标并将它们存储在用户单击的元素上的属性中,以及当弹出窗口关闭并且在调用 .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 工作,它看起来很草率,因为内容淡出,但会跳到屏幕顶部,然后返回到有问题的元素。
【问题讨论】:
标签: javascript jquery