我创建了 2 个解决方案:
- 用所有相关内容动态填充 DIV 卡。
- 在 CSS 中使用
display:none 消除不需要的内容、段落或元素
- 只有一张“大”卡片会改变它在页面上的位置,克隆并在悬停时显示完整内容 - 为该元素准备 CSS 以
display:block; 所有内容。
悬停时:
- 获取悬停卡片的位置
- 获取所有“小”卡片内容并填充更大的卡片
- 添加悬停意图以在显示之前等待约 180 毫秒
- 将大卡放在小卡上,并使用
.show(600) 显示。
- 如果鼠标离开大卡使用
.hide()
jQuery:
$('body').append('<div id="userCard"><div id="userCardContent"></div>');
var $hoveredImg;
$('.userCardMini').on('mouseenter','img',function(e){
$hoveredImg = $(this);
var thisSrc = $hoveredImg.attr('src').split('s=')[0];
var posX = $hoveredImg.offset().left-10;
var posY = $hoveredImg.offset().top-10;
$('#userCardContent').html( $hoveredImg.parent().html() );
$('#userCardContent').find('img').attr('src', thisSrc+'s=64&d=identicon&r=PG');
var t = setTimeout(function() {
$('#userCard').css({left:posX, top:posY}).show(600);
}, 200);
$hoveredImg.data('timeout', t);
}).on('mouseleave',function(){
clearTimeout($hoveredImg.data('timeout'));
});
$('#userCard').mouseleave(function(){
$(this).stop(1,1).hide();
});
HTML:
<div class="userCardMini" data-user="383904"></div>
<div class="userCardMini" data-user="1063093"></div>
CSS:
.badge{
width:6px;
height:6px;
border-radius:10px;
font-size:11px;
display:inline-block;
margin:2px;
}
.gold{background:gold;}
.silver{background:silver;}
.bronze{background:#cc9966;}
.userCardMini{
width:200px;
height:32px;
/*background:#eee;*/
margin:4px;
clear:both;
}
a{color:#27f;}
img.userImg{
cursor:pointer;
float:left;
margin-right:10px;
box-shadow:1px 1px 3px -1px #999;
}
.userDetails, .userLocation{display:none;}
/* user card - BIG ONE */
#userCard{
position:absolute;
top:0;
left:0;
width:280px;
box-shadow:1px 1px 3px -1px #999;
border-radius:3px;
background:#666;
color:#eee;
display:none;
}
#userCardContent{
width:260px;
margin:10px;
}
#userCardContent a{color:#fff;}
#userCard .userDetails,
#userCard .userLocation{
display:block;
margin-bottom:4px;
}
另一种解决方案(我最喜欢)
是使用 DIV 元素的 CSS 和 z-index,其方式是 - 将父级悬停在我们从下方为内容(子级)元素设置动画
这点 jQuery:
var $desc;
$('.userCard').hover(function(){
$desc = $(this).find('.description');
$(this).css({zIndex:'3'});
var t = setTimeout(function() {
$desc.show('slow');
}, 150);
$(this).data('timeout', t);
},function(){
$(this).css({zIndex:0});
clearTimeout($(this).data('timeout'));
$desc.hide();
});
HTML:
<div class="userCard">
<div class="initial">
<img src="http://placehold.it/26x26/f7b" /><h2>User name 1</h2>
</div>
<div class="description">
<div class="description_content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
In et neque quam, vel dapibus neque. Praesent rutrum ultricies sodales.
</div>
</div>
</div>
CSS:
.userCard{
position:relative;
padding:10px;
width:200px;
margin:10px;
}
.initial{
position:relative;
cursor:pointer;
z-index:2;
}
.userCard img{
float:left;
margin-right:10px;
box-shadow: 1px 1px 3px -1px #999;
}
.description{
background:#eee;
position:absolute;
top:0px;
left:0px;
padding:10px;
clear:both;
display:none;
}
.description_content{
padding-top:37px;
position:relative;
width:200px;
}