简体   繁体   English

jquery remove()跳回页面顶部

[英]jquery remove() jumps back to the top of the page

I have written a popup with an overlay (not dissimilar from the usual picture displayers) where the user clicks and an overlay covers the screen which fetches the big picture for display. 我写了一个带有覆盖的弹出窗口(与通常的图片显示器不同),用户点击该窗口,并且覆盖层覆盖了用于显示的大图片的屏幕。

The problem is, when the user clicks 'close' my function fades out the picture and overlay then removes them. 问题是,当用户点击“关闭”时,我的功能会淡化图片并覆盖,然后将其删除。 When I call the .remove() function, the browser then focuses on the body tag and scrolls to the top of the page. 当我调用.remove()函数时,浏览器会关注body标签并滚动到页面顶部。

I have attempted a work-around by capturing the offset.top co-ords and storing them in an attribute on the element the user clicks on, and when the popup is closed and after the . 我已经尝试通过捕获offset.top co-ords并将它们存储在用户单击的元素的属性中,以及弹出窗口关闭时和之后。 remove() function has been called, I use the scrollTo() function to return the correct scroll position (which for some reason overshoots and scrolls the element to the 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');

}

Why is the .remove() function causing the jump back to the top of the page? 为什么.remove()函数会导致跳转到页面顶部? Even with the scrollTo work around it looks sloppy as the stuff fades out, but jumps up to the top of the screen and then back down to the element in question. 即使使用scrollTo解决它看起来很邋in,因为它们会逐渐消失,但会跳到屏幕顶部,然后再回到相关元素。

See How can I remove the location hash without causing the page to scroll? 请参阅如何在不导致页面滚动的情况下删除位置哈希? ; ;

Alternatively, you can return false; 或者,您可以return false; to stop this behavior... 停止这种行为......

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

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