简体   繁体   English

在内容更改时反跳HTML文档的高度

[英]Debounce html document height on content change

Assume the viewport is 100×100 (width × height) , the document is empty, the scroll position is (0,0) (x,y) . 假设视口为100×100 (宽×高) ,文档为空,滚动位置为(0,0) (x,y)

  1. body.appendChild an element A of size 100×200 body.appendChild元素A的大小为100×200
  2. Now scroll to (0,100) manually 现在手动滚动到(0,100)
  3. Some javascript now removes A and body.appendChild s B (100×50) 现在,一些javascript删除了A和body.appendChild的B(100×50)
  4. The browser detects that the total document height is 50 and scrolls back to (0,0) 浏览器检测到文档总高度为50,然后滚动回到(0,0)
  5. Some time passes (~150ms) 一段时间过去(〜150ms)
  6. Some javascript removes B and body.appendChild s C (100×200 again) 一些javascript删除B和body.appendChild的C(再次为100×200)

Now the browser is still scrolled at 0,0. 现在浏览器仍在0,0滚动。 I'm looking for a solution/library/jQueryPlugin (nothing that i have to insert at 3 and 6) to prevent (4) from happening for a custom defined threshold (in this case ~150ms). 我正在寻找解决方案/库/ jQueryPlugin(无需在3和6插入任何内容),以防止自定义阈值(在这种情况下为150ms)发生(4)。

this fiddle blocks the scrollbar for 500 seconds when the height of the element changes 当元素的高度改变时,这个小提琴会阻塞滚动条500秒

$(document).on('click', function(e) {
    var elementClone = $('.element').clone();
    var currentHeight = $('.element').height();
    var currentWinHeight = $(document).height();

    $('body, html').css({
        height: currentHeight
    });

    $('.element').remove();
    var randomHeight = Math.random() * 1000;
    elementClone.css({
        height: randomHeight
    }).text('height: ' + randomHeight).appendTo('body');
    window.setTimeout( function() {
        $('body, html').css({
            height: 'auto' 
        });
    }, 500)
    e.preventDefault();
});

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

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