简体   繁体   English

关闭 div 会向上移动页面

[英]Closing a div moves the page up

I'm making a page where you can open and close descriptions.我正在制作一个页面,您可以在其中打开和关闭描述。 The page works perfectly on firefox, but on other browser like Chrome, the page seems to go up as you open and close the other divs.该页面在 firefox 上完美运行,但在 Chrome 等其他浏览器上,当您打开和关闭其他 div 时,该页面似乎为 go。

EDIT: the page goes up when I close a menu under another one.编辑:当我关闭另一个菜单下的菜单时,页面会上升。 But not the other way.但不是相反。 here is a link so you can see what is happening with chrome: https://imgur.com/a/4zgrzc0这是一个链接,因此您可以查看 chrome 发生了什么: https://imgur.com/a/4zgrzc0

I suppose the problem is $(this).parent('.sub-menu').siblings().find('ul').slideUp('fast');我想问题是$(this).parent('.sub-menu').siblings().find('ul').slideUp('fast'); How can I avoid that?我怎样才能避免这种情况? Thanks a lot.非常感谢。

 $(document).ready(function(){ $(".exposition").on('click',function(){ var hello = $(this).attr('data-id'); $('.photos-evenements').hide(); $('[id='+ hello + ']').show(); }); }); $( document ).ready(function(open) { $('.sub-menu ul').hide(); $('.sub-menu a').click(function () { $(this).parent('.sub-menu').siblings().find('ul').slideUp('fast'); // to hide all ul expect this one $(this).parent('.sub-menu').children('ul').slideToggle(200); }); $('.sub-menu a').click(function(open) { open.preventDefault(); }); });
 .photos-evenements{ display:none; max-width: 100%; max-height: 90vh; }.exemple { height:100vh; background-color:lavender; }
 <div class="exemple">hi</div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="menu"> <li class='sub-menu'> <a href='#' class="exposition" data-id="divId1">1</a> <ul> <li> When opened, i'm a description, I'm not supposed to move the page when opened or closed. </li> </ul> </li> <li class='sub-menu'> <a href='#' class="exposition" data-id="divId2">2</a> <ul> <li> I'm supposed to close 1 and don't move the page up </li> </ul> </li> </ul> <div class="exposition"> <div class="photos-ind"><img id="divId1" class="photos-evenements" src="http://follebeton.com/img/Performances%20OK.jpg" data-id="divId1"/></div> <div class="photos-ind"><img id="divId1" class="photos-evenements" src="http://follebeton.com/img/333_3.png" data-id="divId1"/></div> <div class="photos-ind"><img id="divId1" class="photos-evenements" src="http://follebeton.com/img/333_1.png" data-id="divId1"/></div> </div> <div class="exposition"> <div class="photos-ind"><img id="divId2" class="photos-evenements" src="http://follebeton.com/img/Performances%20OK.jpg" data-id="divId2"/></div> <div class="photos-ind"><img id="divId2" class="photos-evenements" src="http://follebeton.com/img/333_3.png" data-id="divId1"/></div> <div class="photos-ind"><img id="divId2" class="photos-evenements" src="http://follebeton.com/img/333_1.png" data-id="divId1"/></div> </div> </div>

I believe that what you are witnessing is aggressive scroll anchoring from Chrome.我相信您所看到的是来自 Chrome 的激进滚动锚定 For some reason Chrome is anchoring the scroll on the link you clicked, while Firefox is anchoring it on some other element, possibly the container or preceding div.由于某种原因,Chrome 将滚动锚定在您单击的链接上,而 Firefox 将其锚定在其他一些元素上,可能是容器或前面的 div。

It's not clear, to me at least, why there's a difference in behaviour or which is 'correct'.至少对我来说,尚不清楚为什么行为会有所不同,或者哪个是“正确的”。 In any case you should be able to resolve your issue by simply disabling scroll anchoring within the menu container.在任何情况下,您都应该能够通过简单地禁用菜单容器内的滚动锚定来解决您的问题。 To do this we can use the overflow-anchor property on the element where we want to disable scroll anchoring.为此,我们可以在要禁用滚动锚定的元素上使用overflow-anchor属性。

In the example you have given we would simply add the following code to the CSS在您给出的示例中,我们只需将以下代码添加到 CSS

.sub-menu{
   overflow-anchor:none
}

This should fix the issue.这应该可以解决问题。

I have edited your example in the snippet below to include this (I also tidied the code up slightly to make it clearer).我在下面的代码段中编辑了您的示例以包含此内容(我还稍微整理了代码以使其更清晰)。 I have tested this in both Firefox and Chrome and the jumping of the page seems to be gone.我已经在 Firefox 和 Chrome 中对此进行了测试,页面的跳转似乎消失了。

Obviously you will have to change what you set the overflow-anchor:none property on for different scripts with different class names.显然,您必须更改为具有不同 class 名称的不同脚本设置的overflow-anchor:none属性。 One approach would be to just disable scroll anchoring for the entire document by setting it on the body.一种方法是通过将其设置在正文上来禁用整个文档的滚动锚定。

body{
   overflow-anchor:none
}

Be warned however, that scroll anchoring was introduced to counteract the very disruptive experience of what the user is currently looking at being moved unexpectedly by changes elsewhere on the document.但是请注意,引入滚动锚定是为了抵消用户当前正在查看的内容被文档其他地方的更改意外移动的非常破坏性的体验。 It would be best to only disable it in select areas if possible.如果可能,最好只在 select 区域禁用它。

 $(document).ready(function(){ $(".exposition").on('click',function(){ var hello = $(this).attr('data-id'); $('.photos-evenements').hide(); $('[id='+ hello + ']').show(); }); }); $( document ).ready(function(open) { $('.sub-menu ul').hide(); $('.sub-menu a').click(function () { $(this).parent('.sub-menu').siblings().find('ul').slideUp('fast'); // to hide all ul expect this one $(this).parent('.sub-menu').children('ul').slideToggle(200); }); $('.sub-menu a').click(function(open) { open.preventDefault(); }); });
 .example { height:100vh; background-color:lavender; }.sub-menu { overflow-anchor:none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="example">Space above</div> <ul class="menu"> <li class='sub-menu'> <a href='#' class="exposition" data-id="divId1">1</a> <ul> <li> When opened, i'm a description, I'm not supposed to move the page when opened or closed. </li> </ul> </li> <li class='sub-menu'> <a href='#' class="exposition" data-id="divId2">2</a> <ul> <li> I'm supposed to close 1 and don't move the page up </li> </ul> </li> </ul> <div class="example">Space below</div>

Revised answer修改后的答案

I think this might be a simple minimum height issue for that page.我认为这可能是该页面的一个简单的最小高度问题。 By reserving the space for the hidden content, you can avoid the jump通过为隐藏内容预留空间,可以避免跳转

I made a codesandbox to demonstrate the issue and the height on the list can be tweaked - remove the border when you are happy.我制作了一个代码框来演示这个问题,并且可以调整列表上的高度 - 当你高兴时删除边框。

https://codesandbox.io/s/sleepy-currying-6vtsy?file=/index.html:467-489 https://codesandbox.io/s/sleepy-currying-6vtsy?file=/index.html:467-489

Not exactly sure what you're trying to do, but you might try the following:不完全确定您要做什么,但您可以尝试以下操作:

$(document).on('click', '.sub-menu a', function (e) {
     e.preventDefault()
     e.stopPropagation()

     $(this).parents('.sub-menu').siblings().find('ul').slideUp('fast');
     $(this).parents('.sub-menu').children('ul').slideToggle(200);
  });

It's good practice to bind events to the document instead of a specific element in case you might end up loading data through AJAX in the future.最好将事件绑定到文档而不是特定元素,以防您将来可能最终通过 AJAX 加载数据。

Anyhow , I usually achieve the thing you're looking for by defining the CSS for the element, in your case.sub-menu ul, to:无论如何,我通常通过在你的 case.sub-menu ul 中为元素定义 CSS 来实现你正在寻找的东西:

.sub-menu ul{
   max-height:0vh; // To hide sub-menu <ul> contents
   overflow:hidden;
   transition:500ms ease; // For fancy smooth opening and closing  [1/2]
   transition:max-height 500ms ease; // Alternative, more specific [2/2]
}

.sub-menu.active ul{
   max-height:100vh;  // or any height you expect it to never exceed.
}

Then - with jQuery - you can do the following:然后 - 使用 jQuery - 您可以执行以下操作:

$(document).on('click', '.sub-menu a', function(e){
  e.preventDefault();
  $(this).parents('.sub-menu').addClass('active')
  $(this).parents('.sub-menu').siblings().removeClass('active')
})

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

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