简体   繁体   English

悬停时不显示弹出框

[英]Pop-up box doesn't show on hover

I'm trying to make a pop-up box show on top of everything when I hover over some city names. 当我将鼠标悬停在某些城市名​​称上时,我试图使弹出框显示在所有内容的顶部。 It doesn't seem to overlap with z-index set to 999 and position: absolute, it doesn't even show. z-index设置为999且位置似乎没有重叠:绝对,甚至不显示。 This is jQuery script 这是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);

This is the CSS 这是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;
}

This is the HTML 这是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.

Seems that without this block of code 似乎没有此代码块

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


});

the pop-up box appear and dissapears just fine. 弹出框出现并消失。 Any ideas? 有任何想法吗?

Check the JS Console for errors. 检查JS控制台中是否有错误。 There are some. 有一些。 However, it's not a good idea to let the JavaScript self execute before anything is ready. 但是,让JavaScript在一切准备就绪之前自行执行不是一个好主意。

Instead try to hook it all up in the document ready Event. 而是尝试将其全部挂接到文档就绪事件中。

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

I have putted it all together in the fiddle for you: http://jsfiddle.net/wh2zxd2z/ 我已将所有内容整理在小提琴中: http//jsfiddle.net/wh2zxd2z/

Is this the expected result? 这是预期的结果吗?

It should work... See 它应该工作... 请参阅

I think you have missed the Jquery should load on top. 我认为您已经错过了应该在顶部加载Jquery的问题。

And your Width of the popup should be Auto. 并且您的弹出窗口的宽度应为自动。 Unless it will expand the window. 除非它将扩大窗口。

.popbox {
  width: auto;
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM