[英]Position div relatively to icon position
When a user hovers over a chevron icon next to a name on my website a div appears with options. 当用户将鼠标悬停在我网站上名称旁边的人字形图标上时,将显示带有选项的div。
The Problem : The names can have different lengths and I would like to display the div below the chevron no matter how long the name is. 问题 : 名称可以有不同的长度 ,无论名称有多长,我都想在V形下方显示div。
Here is my code: 这是我的代码:
HTML 的HTML
<div class='settings'><i class='icon ion-chevron-down'></i></div>
<div class='settings-wrapper'>
<ul class='settings-bubble'>
<li>Bearbeiten</li>
<li>Löschen</li>
</ul>
</div>
SCSS SCSS
// The chevron icon
.settings {
display: inline;
position: relative;
padding: .1em 0 0 .5em;
opacity: 0; // I display the chevron on hover using jquery
}
// The options bubble
.settings-wrapper {
position: relative;
}
.settings-bubble {
position: absolute;
top: 0;
left: 0;
width: auto;
height: auto;
margin: 0;
padding: 0 .6em;
opacity: 0;
z-index: 9999;
li {
position: relative;
display: block;
a { float: left; }
}
}
I would be very thankful for any kind of help!! 我将非常感谢您提供的任何帮助!
If I change left: 0
to right: 0
it looks like this: 如果我将
left: 0
更改为right: 0
它看起来像这样:
In .settings-bubble
change left: 0;
在
.settings-bubble
left: 0;
更改left: 0;
into right: 0;
right: 0;
and it will stick to the inner right side of the parent instead of the inner left. 它将粘在父对象的内部右侧而不是内部左侧。
EDIT: the trick is to add the div containing the bubble into the div containing the string of arbitrary length, and attach that bubble inside that div to the inner right-hand side, as illustrated by this fiddle . 编辑:诀窍是将包含气泡的div添加到包含任意长度字符串的div中,并将该div内的气泡附加到内侧的右手侧,如该小提琴所示 。
When the icon is hovered, there is an event handler that displays the div. 当图标悬停时,有一个事件处理程序显示div。 In that handler, you can inspect the x and y coordinates of the icon.
在该处理程序中,您可以检查图标的x和y坐标。 When you display the div, you can modify its style to be positioned relative to the icon.
显示div时,可以修改其样式以相对于图标定位。 For example:
例如:
var chevron = document.getElementById('chevron'); var popup = document.getElementById('popup'); chevron.addEventListener('mouseover', function(e) { popup.classList.remove('hidden'); popup.style.left = e.target.offsetLeft + 'px'; }); chevron.addEventListener('mouseout', function(e) { popup.classList.add('hidden'); });
.hidden { display: none; } #popup { position: absolute; border: 1px solid #000; }
<h1 contenteditable="true">Some long title <span id="chevron">></span></h1> <div id="popup" class="hidden">popup</div>
I left the title editable so you can make it longer and see the popup change position. 我将标题保留为可编辑状态,因此可以使其更长,并查看弹出窗口的更改位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.