[英]Use JQuery next() and find() to find the next closest class-given element on a hovered div and show it
[英]How to show a div next to the hovered element with jQuery?
假設我有幾個像這樣的div:
編輯:
<div class="ProfilePic">
<a href="#">
<img src="lib/css/img/profile_pic1.png" alt="" class="ProfilePicImg"/>
</a>
<div class="PopupBox" style="display:none;"> ... </div>
</div>
我希望能夠將鼠標懸停在圖像.ProfilePicImg
並相對於它顯示另一個div。
懸停時要彈出的框設置為position:absolute
。 .ProfilePic
的位置是相對的。 就像應該的那樣。
我嘗試了不同的解決方案,但徒勞無功...而且我還在StackOverflow上搜索過...
有人為此有把戲嗎?
PS我不希望彈出框顯示在每個.ProfilePic
div上...
EDIT2:似乎jQuery的.find()
遍歷函數是獲取我想顯示的特定 .PopupBox而不是全部的關鍵。
我沒有測試代碼,但這是您想要的嗎?
$(".ProfilePicImg").hover(function(){
var divToShow = $(this).parent("a").siblings("div.PopupBox");
divToShow.css({
display: "block",
position: "absolute",
left: ($(this).offset().left + $(this).width()) + "px",
top: $(this).offset().top + "px"
});
},
function(){
$("div.PopupBox").hide();
});
為什么不簡單地使用CSS定位呢?
#PopupBox {
z-index: 1000;
margin-top: -15px;
margin-left: 90px;
position: absolute;
background-color: #abcdef;
padding: 1em;
border: 1px solid #456789;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.