當鼠標懸停在某個類的元素A上時,應該會出現另一個包含鏈接的浮動div B. B只要鼠標離開A和B就應該消失。如何使用jQuery創建浮動div div
如何使用jQuery來做到這一點? jsFiddle
var container = $('#container');
var area = $('.area'); // this is A
var position = area.offset();
var floating = $("<div />", {
css : { "position" : "absolute",
"top" : position.top - 30,
"left" : position.left + 20,
"border" : "1px solid #000",
"padding" : "5px",
"display" : "none",
"z-index" : "100",
"background-color" : "#F00"
}
})
.text('Hello World'); // this is B
container.css('position', 'relative');
container.append(floating);
function show() {
floating.show();
}
function hide() {
floating.hide();
}
area.on('mouseenter', show);
area.on('mouseleave', hide);
floating.on('mouseenter', function(event) {
console.log("floating: mouseenter");
area.off('mouseenter', show);
area.off('mouseleave', hide);
});
floating.on('mouseleave', function(event) {
console.log("floating: mouseleave");
area.on('mouseenter', show);
area.on('mouseleave', hide);
});
我的問題是evertime鼠標進入B,B已經消失。我需要用jQuery來做到這一點,不僅僅是CSS。
'的z index'不以像素爲單位。 – Diego