javascript - 悬停时不显示弹出框

标签 javascript jquery html css

当我将鼠标悬停在某些城市名​​称上时,我试图在所有内容之上显示一个弹出框。 它似乎没有与 z-index 设置为 999 和 position: absolute 重叠,它甚至没有显示。 这是 jQuery 脚本

$(function() {
var moveLeft = 0;
var moveDown = 0;
$('a.popper').hover(function(e) {

    var target = '#' + ($(this).attr('data-popbox'));

    $(target).show();
    moveLeft = $(this).outerWidth();
    moveDown = ($(target).outerHeight() / 2);
}, function() {
    var target = '#' + ($(this).attr('data-popbox'));
    $(target).hide();
});

$('a.popper').mousemove(function(e) {
    var target = '#' + ($(this).attr('data-popbox'));

    leftD = e.pageX + parseInt(moveLeft);
    maxRight = leftD + $(target).outerWidth();
    windowLeft = $(window).width() - 40;
    windowRight = 0;
    maxLeft = e.pageX - (parseInt(moveLeft) + $(target).outerWidth() + 20);

    if(maxRight > windowLeft && maxLeft > windowRight)
    {
        leftD = maxLeft;
    }

    topD = e.pageY - parseInt(moveDown);
    maxBottom = parseInt(e.pageY + parseInt(moveDown) + 20);
    windowBottom = parseInt(parseInt($(document).scrollTop()) + parseInt($(window).height()));
    maxTop = topD;
    windowTop = parseInt($(document).scrollTop());
    if(maxBottom > windowBottom)
    {
        topD = windowBottom - $(target).outerHeight() - 20;
    } else if(maxTop < windowTop){
        topD = windowTop + 20;
    }

    $(target).css('top', topD).css('left', leftD);


});

})(jQuery);

这是CSS

.popbox {
position: absolute;
display: none;
z-index: 999;
width: 400px;
padding: 10px;
background: #EEEFEB;
color: #000000;
border: 1px solid #4D4F53;
margin: 0px;
-webkit-box-shadow: 0px 0px 5px 0px rgba(164, 164, 164, 1);
box-shadow: 0px 0px 5px 0px rgba(164, 164, 164, 1);
}
.popbox h2
{
background-color: #4D4F53;
color:  #E3E5DD;
font-size: 14px;
display: block;
width: 100%;
margin: -10px 0px 8px -10px;
padding: 5px 10px;
}

这是HTML

<div id="pop1" class="popbox">
<h2>Youth Academy Bucuresti</h2>
<p>Membri:</p>
</div>
<div id="pop2" class="popbox">
<h2>Youth Academy Piatra Neamt</h2>
<p>Membri</p>
</div>
These are the cities: <a href="#" class="popper" data-popbox="pop1">Bucuresti</a>, <a href="#" class="popper" data-popbox="pop2">Piatra Neamt</a> etc.

好像没有这段代码

$('a.popper').mousemove(function(e) {
var target = '#' + ($(this).attr('data-popbox'));

leftD = e.pageX + parseInt(moveLeft);
maxRight = leftD + $(target).outerWidth();
windowLeft = $(window).width() - 40;
windowRight = 0;
maxLeft = e.pageX - (parseInt(moveLeft) + $(target).outerWidth() + 20);

if(maxRight > windowLeft && maxLeft > windowRight)
{
    leftD = maxLeft;
}

topD = e.pageY - parseInt(moveDown);
maxBottom = parseInt(e.pageY + parseInt(moveDown) + 20);
windowBottom = parseInt(parseInt($(document).scrollTop()) + parseInt($(window).height()));
maxTop = topD;
windowTop = parseInt($(document).scrollTop());
if(maxBottom > windowBottom)
{
    topD = windowBottom - $(target).outerHeight() - 20;
} else if(maxTop < windowTop){
    topD = windowTop + 20;
}

$(target).css('top', topD).css('left', leftD);


});

弹出框出现和消失都很好。 有什么想法吗?

最佳答案

检查 JS 控制台是否有错误。有一些。 但是,在一切准备就绪之前让 JavaScript 自行执行并不是一个好主意。

而是尝试将其全部挂接到文档就绪事件中。

$(document).ready(function () { 
  Your foo..
});

我已为您将所有内容放在一起:http://jsfiddle.net/wh2zxd2z/

这是预期的结果吗?

关于javascript - 悬停时不显示弹出框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377763/

相关文章:

javascript - JS jQuery 更改获取选择选项标签的innerHTML

javascript - 简单的 jquery .post 不返回数据

javascript - JQuery Mobile Listview 元素消失

html - 第 3 行 div 在一行上

javascript - 评论的滑动/滚动效果

javascript - 如何对动态 HTML5 数据描述符执行部分查找?

javascript - "[object Object]"不是有效的数字 jquery slider

html - 选中复选框时以及单击图像时突出显示图像

javascript - 如何同步每对动态创建的输入?

javascript - 初始化键值对 Javascript 对象