[英]Overflow combined with position absolute and floats
please take a look at JSFiddle example . 请看一下JSFiddle示例 。
I want to make menu with closing 'x' on it's right side. 我想在菜单的右边加上'x'。 Menu pops-up after click on green div. 单击绿色div后弹出菜单。
<div class="field">
<nav id="menu">
<ul>
<li>Menu item 1</li>
<li>Menu item 2</li>
<li>Item 3</li>
</ul>
<div class="close">X</div>
</nav>
.field {
background: green;
width: 350px;
height: 200px;
margin: 10px auto;
position: relative;
cursor: pointer;
}
#menu {
position: absolute;
display: none;
}
#menu ul {
width: 80%;
border: 1px solid #999;
float: left;
}
#menu li {
background: #fff;
padding: 5% 15%;
border-width: 0 0 1px 0;
border-style: solid;
border-color: #999;
white-space: nowrap;
}
#menu .close {
background: #ccc;
width: 20%;
padding: 5%;
float: right;
}
(JS is messy, I wrote it on quickly) (JS很乱,我很快就写了)
$('.field').on('click', function (e) {
var $pointer = $('#pointer'),
$menu = $('#menu'),
parentOffset = $(this).offset(),
relX = e.pageX - parentOffset.left,
relY = e.pageY - parentOffset.top,
circleX = relX - ($pointer.outerWidth() / 2) + 1,
circleY = relY - ($pointer.outerHeight() / 2) + 1;
$pointer.css('left', circleX);
$pointer.css('top', circleY);
$pointer.show();
$menu.css('left', circleX + $pointer.outerWidth());
$menu.css('top', circleY);
$menu.show();
$menu.one('click', '.close', function (e) {
$menu.hide();
$pointer.hide();
e.stopPropagation();
});
});
There are 2 issues: 有两个问题:
li items doesn't overflow properly text inside them; li项不会在其中正确溢出文本;
[x] element is under menu and not on its right side; [x]元素在菜单下而不是在右侧;
I tried different combinations suggested in other, similar questions but nothing works or I'm to tired and I missed something. 我尝试了其他类似问题中建议的不同组合,但没有任何效果,或者我很累,错过了一些东西。
Important thing is that there should not be any hard coded values. 重要的是,不应有任何硬编码值。 Properly values are only %. 正确的值仅是%。 That's because it should look good on different borwser sizes. 那是因为在不同的钻头尺寸下看起来应该不错。
Any ideas how to fix those issues? 有什么想法可以解决这些问题吗?
You should take a look at the CSS Box model . 您应该看看CSS Box模型 。 Padding, border and margin are always added to the width/height of the element and are not included in the width you define. 填充,边框和边距始终添加到元素的宽度/高度,并且不包括在您定义的宽度中。 Your floating X is jumping down, because there are no more 20% space left on the right side of the ul
. 您的浮动X向下跳跃,因为ul
的右侧不再剩余20%的空间。
Better place the X outside of your div. 最好将X放在div之外。 You can also leave out all the floats. 您也可以忽略所有花车。 Set position: absolute
on div.close and move it outside the boundary of the container using left: 100%
. 设置position: absolute
div.close上的position: absolute
,并使用left: 100%
将其移动到容器边界之外。
#menu {
position: absolute;
display: none;
}
#menu ul {
border: 1px solid #999;
}
#menu li {
background: #fff;
padding: 5% 15%;
border-width: 0 0 1px 0;
border-style: solid;
border-color: #999;
}
#menu .close {
background: #ccc;
width: 20%;
padding: 5%;
position: absolute;
top: 0;
left: 100%;
}
By the way, white-space: nowrap
leads to your menu elements' texts being out of the boundary of the li
, which is not very flexible. 顺便说一句, white-space: nowrap
导致菜单元素的文本超出li
的边界,这不是很灵活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.