简体   繁体   English

奇怪的CSS位置权:0问题

[英]Strange CSS position right:0 issue

I have the following code that works for the most part. 我有以下大部分代码都可以使用。 However not always. 但是并非总是如此。 I have fiddled with it quite a lot to try get it working with no avail. 我花了很多时间尝试使它无济于事。 the issue seems to be intermittent however it definitely reproducible. 这个问题似乎是断断续续的,但是绝对可以重现。

What this code tries to do is reposition the .menu div to the right or left of the item depending on where in the viewport it is. 此代码尝试执行的操作是根据项目在视口中的位置,将.menu div重新定位在.menu的右侧或左侧。 If the item's right edge is past the halfway mark it changes the side of the .menu div. 如果项目的右边缘超过中途标记,它将更改.menu div的一侧。

However, after re-positioning the window a few times and selecting an item that is on the right side it seems that sometimes the CSS doesn't work immediately and it positions the element on the left. 但是,在重新定位窗口几次并选择了右侧的项目之后,似乎CSS有时无法立即工作,并且将元素放置在左侧。

Is my code doing something wrong or has someone else seen this issue before? 我的代码做错什么了吗?或者其他人以前见过这个问题? Does anyone know how to fix it? 有谁知道如何修理它? You can see, by the console values, that right is greater than the width, however, the element is still positioned wrong. 您可以通过控制台值看到,正确的宽度大于宽度,但是,元素的位置仍然错误。

I also checked with an alert() and it's definitely going into the if at the right point... 我也有一个检查alert()和它肯定会到if在合适的点...

在此处输入图片说明

 $(document).ready(function() { $(document).on("click", ".item", function(event) { var widthCenter = window.innerWidth / 2; var rightPos = $(this)[0].getBoundingClientRect().right; console.log("width center:", widthCenter); console.log("right", rightPos); if (rightPos > widthCenter) $(this).children(".menu").css("right", "0", "!important"); else $(this).children(".menu").css("left", "0", "!important"); var showingMenu = false; if ($(this).children(".menu").is(':visible')) showingMenu = true; $(".menu").hide(); if (showingMenu) $(this).children(".menu").hide(); else $(this).children(".menu").show(); }); }); 
 .container1 { display: flex; flex-direction: row; flex-wrap: wrap; border-width: 1px; border-style: solid; } .item { position: relative; width: 60px; height: 40px; border-width: 1px; border-style: solid; margin: 20px; } .menu { display: none; position: absolute; width: 100px; height: 100px; background-color: blue; top: 100%; margin: 0; padding: 0; z-index: 100; border-width: 1px; border-style: solid; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container1"> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> </div> 

You're not removing the css, so after a couple of show/close/resize/show iterations, it has both left:0!important and right:0!important so (I guess) it picks one as the most important, probably the first that was applied. 您没有删除css,因此在几次show / close / resize / show迭代之后,它同时具有left:0!importantright:0!important因此(我想)它选择了一个最重要的位置,可能首先应用。

You can get around this by adding/removing classes rather than manipulating the css directly (it's reasier to remove a class): 您可以通过添加/删除类来解决此问题,而不是直接操纵css(删除类更容易):

add some css: 添加一些CSS:

.left { left:0!important }
.right { right:0!important }

and toggle the classes: 并切换类:

    if (rightPos > widthCenter)
      $(this).children(".menu").addClass("right").removeClass("left");
    else
      $(this).children(".menu").addClass("left").removeClass("right");

 $(document).ready(function() { $(document).on("click", ".item", function(event) { var widthCenter = window.innerWidth / 2; var rightPos = $(this)[0].getBoundingClientRect().right; console.log("width center:", widthCenter); console.log("right", rightPos); if (rightPos > widthCenter) $(this).children(".menu").addClass("right").removeClass("left"); else $(this).children(".menu").addClass("left").removeClass("right"); var showingMenu = false; if ($(this).children(".menu").is(':visible')) showingMenu = true; $(".menu").hide(); if (showingMenu) $(this).children(".menu").hide(); else $(this).children(".menu").show(); }); }); 
 .container1 { display: flex; flex-direction: row; flex-wrap: wrap; border-width: 1px; border-style: solid; } .item { position: relative; width: 60px; height: 40px; border-width: 1px; border-style: solid; margin: 20px; } .menu { display: none; position: absolute; width: 100px; height: 100px; background-color: blue; top: 100%; margin: 0; padding: 0; z-index: 100; border-width: 1px; border-style: solid; } .left { left:0!important } .right { right:0!important } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container1"> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> <span class="item">item<div class="menu"></div></span> </div> 

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

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