[英]Aligning div right next to the list items
I am trying to create div
elements and print items on to them dynamically. 我正在尝试创建
div
元素并动态地打印它们。 I've created a demo to show where I've reached. 我已经创建了一个演示来展示我到达的地方。
The issue with my code is that it doesn't show up right next to the list where I want it. 我的代码的问题是它不会出现在我想要的列表旁边。 Instead it is displayed at the bottom.
相反,它显示在底部。 Is it possible to show the new
div
right next to the element that I'm hovering over? 是否有可能在我正在盘旋的元素旁边显示新的
div
?
$(".bubble").live({
mouseenter : function() {
$("#bubble").show();
$("#bubble").html($(this).attr('id'));
},
mouseleave : function() {
$("#bubble").empty();
}
});
#bubble{
width:100px;
height:20px;
background-color:#666666;
position:absolute;
display:hidden;
}
<ul>
<li><span class="bubble" id="test1">test1</span></li>
<li><span class="bubble" id="test2">test2</span></li>
<li><span class="bubble" id="test3">test3</span></li>
<li><span class="bubble" id="test4">test4</span></li>
<li><span class="bubble" id="test5">test5</span></li>
</ul>
<div id="bubble"></div>
You need to set the position of #bubble
relative to the li
which is being moused over. 您需要设置
#bubble
相对于被鼠标悬停的li
的位置。 Try this: 试试这个:
$("ul").on('hover', '.bubble', function(e) {
if (e.type == 'mouseenter') {
var $el = $(this);
$("#bubble")
.html($el.attr('id'))
.css({
top: $el.offset().top,
left: $el.offset().left + $el.width()
})
.show();
}
else {
$("#bubble").empty();
}
});
Note that I have removed the use of live()
as it has been deprecated, and used on()
with a delegate instead. 请注意,我已经删除了
live()
的使用,因为它已被弃用,而是on()
使用了委托。 Also I used the hover
event. 我还使用了
hover
事件。
What about a pure CSS solution? 纯CSS解决方案怎么样?
HTML: HTML:
<ul>
<li>
<span class="bubble" id="test1">test1</span>
<div>test1</div>
</li>
<li>
<span class="bubble" id="test2">test2</span>
<div>test2</div>
</li>
<li>
<span class="bubble" id="test3">test3</span>
<div>test3</div>
</li>
<li>
<span class="bubble" id="test4">test4</span>
<div>test4</div>
</li>
<li>
<span class="bubble" id="test5">test5</span>
<div>test5</div>
</li>
</ul>
CSS: CSS:
ul li div {
width: 100px;
height: 10px;
background-color: #666666;
position: absolute;
display: none;
}
ul li:hover div {
display: inline;
}
JSFiddle: http://jsfiddle.net/H2ZMc/ JSFiddle: http : //jsfiddle.net/H2ZMc/
Hope this helps: 希望这可以帮助:
I append the #bubble to the li 我将#bubble附加到li上
$(".bubble").live({
mouseenter : function() {
$("#bubble").html($(this).attr('id'));
$(this).append($("#bubble").show());
},
mouseleave : function() {
$("#bubble").empty().hide();
}
});
Try this: 试试这个:
var bubble = $("#bubble");
$(".bubble").live({
mouseenter : function(e) {
$("#bubble").html($(this).attr('id'));
e.target.appendChild(bubble);
},
mouseleave : function() {
$("#bubble").empty();
}
});
What this does is appends the bubble element to the element on which the mouse is over. 这样做是将bubble元素追加到鼠标所在的元素上。 By applying
display: inline
to the div
you can make it appear beside its sibling instead of below it as it will do if you use this code directly. 通过对
div
应用display: inline
,你可以让它显示在它的兄弟旁边而不是它下面,就像你直接使用这个代码一样。
Do what Mica said and also remove the position:absolute; 做Mica所说的并删除位置:绝对;
So css should be 所以css应该是
#bubble{
width:100px;
height:10px;
background-color:#666666;
display:hidden;
display: inline;
float: left;
}
ul{
display: inline;
float: left;
}
Why use .on()
when the #bubble
element already exists. 当
#bubble
元素已经存在时,为什么要使用.on()
。
$(".bubble").hover( function( ) {
var offset = $(this).offset();
$("#bubble").html($(this).attr("id"))
.css({ top: offset.top, left: offset.left + $(this).width() })
.show();
}, function( ) {
$("#bubble").html("").hide();
});
If you give the <ul>
and the <div>
the following styles it should work. 如果你给
<ul>
和<div>
以下样式,它应该工作。
display: inline;
float: left;
$(".bubble").live({
mouseenter: function() {
$("#bubble").show();
$("#bubble").html($(this).attr('id'));
var p = $(this).position();
$("#bubble").css({left: p.left + $(this).outerWidth(), top: p.top });
},
mouseleave: function() {
$("#bubble").empty().css({top: '', left: ''});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.