简体   繁体   English

如何删除顶部div而不会导致底部div跳起来?

[英]How to delete top divs without causing bottom divs to jump up?

I have a situation where I need to delete the top-most divs in a document without causing any changes from the user's perspective. 我有一种情况,我需要删除文档中最顶层的div,而不会从用户的角度进行任何更改。 Basically, I need to go from: 基本上,我需要从:

---------top of document ---------文件顶部
div1 DIV1
div2 DIV2
div3 DIV3
---------top of window ---------窗口顶部
div4 DIV4
div5 DIV5
---------bottom of window ---------窗口底部
div6 DIV6
div7 DIV7
---------bottom of document ---------文件底部

to: 至:

---------top of document ---------文件顶部
div3 DIV3
---------top of window ---------窗口顶部
div4 DIV4
div5 DIV5
---------bottom of window ---------窗口底部
div6 DIV6
div7 DIV7
---------bottom of document ---------文件底部

What would be the best way to accomplish this transition in a manner that doesn't cause the user to see div4 and div5 make any movement? 以不会导致用户看到div4和div5进行任何移动的方式完成此转换的最佳方法是什么?

You should be able to set the document's scrollTop to the current one minus the height of the div s you are removing. 您应该能够将文档的scrollTop设置为当前值减去要删除的div的高度。 Something like: 就像是:

var height = $('#div-1').height() + $('#div-2').height();
$(document).scrollTop($(document).scrollTop() - height);
$('#div-1, #div-2').remove();

The change in scroll position should be fast enough that the user won't notice it. 滚动位置的变化应足够快,以至于用户不会注意到它。

Heavily edited! 重编辑!

$( document ).ready(function() {
    var heightof = $('#one').height() + $('#two').height();
    $(window).scroll(function(){
        var y = $(this).scrollTop();

        if(y >= heightof)
        {
            $('#one, #two').remove();
            $(this).scrollTop('0px');
        }
    });  
});

Here, this should work! 在这里,这应该工作!

This code is basically checking for scroll event, and when it reaches past the first two divs (so we add their heights), we remove them. 这段代码基本上是检查滚动事件,当它到达前两个div时(所以我们加上它们的高度),我们删除它们。 I didn't even noticed anything changes when I tested them. 当我测试它时,我甚至没有注意到任何变化。

check it out live on jsfiddle jsfiddle查看

If you don't need to actually remove the element from the DOM, you can set its style.visibility property to "hidden". 如果您不需要从DOM中实际删除该元素,则可以将其style.visibility属性设置为“hidden”。 It will disappear from the page, but there will still be an empty space where it used to be. 它将从页面中消失,但仍然会有一个空白的空间。

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

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