简体   繁体   English

JavaScript工具提示定位问题

[英]Javascript tooltip positioning issue

I would like to create a javascript popup using following script. 我想使用以下脚本创建一个JavaScript弹出窗口。

var xOffset = 30;
var yOffset = -5;

function showPopup (targetObjectId, eventObj) {
    var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
    var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
    moveObject(targetObjectId, newXCoordinate, newYCoordinate);

    -----etc
}



function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {

//      newXCoordinate = newXCoordinate-8;
//      newYCoordinate = newYCoordinate-300;

        styleObject.left = newXCoordinate;
        styleObject.top = newYCoordinate;
        return true;
    } else {
        // we couldn't find the object, so we can't very well move it
        return false;
    }
} // moveObject

Demo here: 演示在这里:

http://jsfiddle.net/jkdYr/ http://jsfiddle.net/jkdYr/

Popup will show successfully but, its positioning becomes an issue. 弹出窗口将成功显示,但是其位置成为问题。 Popup not getting dynamic position according to cursor move. 弹出窗口没有根据光标移动获得动态位置。 Anybody can please help to change the above script to show popup position according to cursor position. 任何人都可以帮助更改以上脚本以根据光标位置显示弹出位置。 ie, normally popup attach to bottom of element; 即,通常弹出窗口附加到元素的底部; if there is not enough space at bottom popup should attach top 如果底部没有足够的空间,请在顶部弹出

Thanks 谢谢

I think this is what you need: 我认为这是您需要的:

<li id="list-item" onclick="return !showPopup('product_deatils_2', event);">
    <a href="javascript:;">Product2</a>
    <div onclick="event.cancelBubble = true;" class="popup" id="product_deatils_2"><a href="javascript:;" onclick="hideCurrentPopup(); return false;" title="close" class="popupLink">[x]</a> Popup Text  
    </div>
</li>

and in your JS use something like this: 并在您的JS中使用如下代码:

$("#list-item").click(function (e) {
    $("#product_deatils_2").css("margin-left",e.offsetX);
    $("#product_deatils_2").css("margin-top",e.offsetY); 
});

Let me know if it helps :) 让我知道是否有帮助:)

Please check this. 请检查一下。 Is this tht ur looking fr? 这看起来像吗?

http://jsfiddle.net/jkdYr/5/ http://jsfiddle.net/jkdYr/5/

I just modified the functions like 我刚刚修改了像

function showPopup(elementN, event){
    $("#"+elementN).css({
        "display":"block",
        "top":event.pageY+"px" ,
        "left":event.pageX+"px"    
    })
}
function hideCurrentPopup(ele){
    $(ele).parent().hide();
}

if you dont use jquery then also it can be simply implemented in pure js. 如果您不使用jquery,那么它也可以简单地在纯js中实现。

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

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