0

I want to implement this menu: http://www.sundoginteractive.com/sunblog/posts/jquery-hover-box in my project.

I tested it in plain html and it works fine, but if i put into my project it doesn't. The problem is, that the HTML part where the text i hover over shall be is in a javascript functions return part, which looks like this:

function formatHits ( data ) {
    return '<a href="#" class="popper" data-popbox="pop1">test</a>'+
'<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;" width="100%">'+
    '<tr>'+ 
Some table stuff...
        '</tr>'+
    '</table>'
}

If i put

<a href="#" class="popper" data-popbox="pop1">test</a>

into the body part of my HTML it works, but it is in the wrong place (and not dynamically). Thanks already! :)

The function for hovering over this element is in the same but above:

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


});

});

Furthermore the element i want to show if hover over the text above is in the body part and:

    <div id="pop1" class="popbox"><h2>Success!</h2><p>This is an example popbox.</p>  </div>

The CSS is:

.popbox {
display: none;
position: absolute;
z-index: 99999;
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;
}

Btw. this mostly the code from the link above!

Linus
  • 695
  • 1
  • 10
  • 19
  • Do you call the plugin initialization code after the HTML has been returned and appended to the document? – GillesC Jul 27 '14 at 14:42
  • You are adding HTML dynamically and therefore need to delegate events http://learn.jquery.com/events/event-delegation/ or instantiate the jQuery-hover-box plugin after each time you add new elements to the page. – Rob Schmuecker Jul 27 '14 at 14:43
  • 2
    Please include a **complete** example in your question and describe what exactly is "not working". What does the code do when it "works"? – Felix Kling Jul 27 '14 at 14:43
  • @gillesc The plugin js code is in the same – Linus Jul 27 '14 at 14:48
  • @FelixKling i hope the question is now more clear – Linus Jul 27 '14 at 15:03
  • @FelixKling thanks a lot. I think this may be the problem – Linus Jul 27 '14 at 15:16

0 Answers0