2012-12-19 102 views
25

我正在尝试创建div元素并将它们动态地打印到它们上。我创建了a demo以显示我所到达的位置。对齐列表项旁边的div

我的代码的问题是,它不会显示在我想要的列表旁边。相反,它显示在底部。是否有可能在我徘徊的元素旁边显示新的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> 

回答

23

您需要#bubble相对的位置设置的正在将鼠标停留在li 。试试这个:

$("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(); 
    } 
}); 

Example fiddle

请注意,我已经删除了使用live(),因为它已被弃用,并使用on()与委托来代替。我还使用了hover事件。

+0

+1使用'.on'并使小提琴更漂亮:-) –

+0

但如果div是动态创建的,只在第一次工作... – user1371896

+0

@ user1371896不正确,我使用'on()'与委托处理程序,所以它将表现为'live()',但具有更好的性能。 –

1

如果你给<ul><div>以下样式它应该工作。

display: inline; 
float: left; 
+0

没有这样的。泡沫div仍然在顶部。 –

+0

也许我没有正确理解你的问题,但下面的CSS规则适用于我:'ul,div {float:left; display:inline}' – micha

7

希望这有助于:

JS-Fiddle Demo

我的#bubble追加到李

$(".bubble").live({ 
      mouseenter : function() { 
       $("#bubble").html($(this).attr('id')); 
       $(this).append($("#bubble").show());    
      }, 
      mouseleave : function() { 
      $("#bubble").empty().hide(); 

      } 
    }); 
+1

+1虽然'display:inline-block'在IE7中有问题,没有一些肮脏的小黑客 –

2

做什么云母说,也拆除的位置是:绝对的;

所以CSS应该

#bubble{ 
width:100px; 
height:10px; 
background-color:#666666; 
display:hidden; 
display: inline; 
float: left; 
} 

ul{ 
display: inline; 
float: left; 
} 

http://jsfiddle.net/y44dR/21/

11

怎么样一个纯CSS的解决方案吗?

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:

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/

+0

在所有提供的解决方案中,我绝对更喜欢这个,它甚至可以在IE7中工作!太好了! – SaschaM78

+1

@ SaschaM78谢谢你,善良的先生。我通常更喜欢解决方案,打破IE7:P,但在这种情况下,我会例外。 – mkey

+1

OP的问题陈述:*我正在尝试创建div元素和**打印**项目给他们*。纯CSS解决方案的问题在于,您无法将项目打印到突出显示的“div”上。令人印象深刻的工作! –

3

尝试这种情况:

var bubble = $("#bubble"); 

$(".bubble").live({ 
    mouseenter : function(e) {  
     $("#bubble").html($(this).attr('id')); 
     e.target.appendChild(bubble); 
    }, 

    mouseleave : function() { 
     $("#bubble").empty();   
    } 
}); 

这样做是追加泡沫元件在其上鼠标在元件。通过将display: inline应用于div,您可以使其显示在其兄弟的旁边而不是其下方,因为如果直接使用此代码,它会执行此操作。

1
$(".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: ''}); 
    } 
});​ 
+2

不少于10个使用的jQuery对象,其中7个使用相同的选择器......好悲伤! –

+0

感谢您的评论。我从来没想过那个。它会改善我的风格! –

+0

没问题,jQuery的真正之处在于能够链接,所以'$('#bubble')。empty()。css({“top”:'','left':''});'会实现同样的事情,只有一个jQuery对象作为开销:) –

2

为什么当#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(); 
}); 

小提琴here

+0

如果我的列表对齐到右? – user1371896

+0

然后只需使用'left:offset.left - $(“#bubble”).width()'来设置左边的CSS属性。小提琴[** here **](http://jsfiddle.net/bruno/y44dR/25/) – Bruno