简体   繁体   English

隐藏内容时如何避免位置跳跃?

[英]How to avoid position jumping when hiding content?

I have been working on the site https://hotel4meetings.firebaseapp.com/ , where clicking on small map expands a larger map below.我一直在网站https://hotel4meetings.firebaseapp.com/上工作,点击小地图会展开下面的大地图。

However, when you scroll below that large map and click on the button at the bottom to close the map, the content jumps to another position.但是,当您滚动到大地图下方并单击底部的按钮关闭地图时,内容会跳转到另一个位置。

I wonder if it possible to minimise jumping of the content below the closing button?我想知道是否可以最大限度地减少关闭按钮下方内容的跳转?

My reasoning: The user is looking at the content below the closing button, so it is preferable not to move that content.我的推理:用户正在查看关闭按钮下方的内容,因此最好不要移动该内容。

The site is Angular-based but the problem is not specific to Angular.该站点基于 Angular,但问题并非特定于 Angular。 The same functionality can be achieved eg with jQuery.例如,可以使用 jQuery 实现相同的功能。

assuming that .map is your big map container and .close is the button that closes the map:假设.map是您的大地图容器,而.close是关闭地图的按钮:

$('.close').click(function(){
     var sctop = $(window).scrollTop();
     var maptop = $('.map').offset().top;
     dif = maptop - sctop;
     if(dif <= 0) 
          $(window).scrollTop($(window).scrollTop()+dif-100);
    $('.map').hide();
});

With animation:带动画:

$('.close').click(function(){
    $('.map').slideUp(300);
    var sctop = $(window).scrollTop() - $('.map').height();
 $('html, body').animate({scrollTop : sctop}, 300);

});

Here is the Demo: https://jsfiddle.net/ym0ek6oq/1/这是演示: https : //jsfiddle.net/ym0ek6oq/1/

Another Demo: https://jsfiddle.net/ym0ek6oq/2/另一个演示: https : //jsfiddle.net/ym0ek6oq/2/

It isn't that the content is jumping it's that when you hide the element the space it takes up closes.并不是内容在跳跃,而是当您隐藏元素时,它占用的空间会关闭。

You can try using您可以尝试使用

$("#someID").css("visibility", "hidden");

with

#someID {
    display: block; 
}

That should do what you are asking but it will leave a blank space within the page.这应该可以满足您的要求,但它会在页面内留下一个空白区域。

Maybe you could scroll the page back down when you close the map the same distance it takes up when visible to cancel the apparent jump of content.也许您可以在关闭地图时将页面向下滚动到与可见时相同的距离以取消内容的明显跳转。 Not sure it'll give the desired effect though.不确定它会产生预期的效果。

Hope this helps, Tim希望这会有所帮助,蒂姆

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

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