简体   繁体   English

在d3.js中,如何根据屏幕位置调整工具提示(上下)?

[英]In d3.js How to adjust tooltip (up and down) based on the screen position?

I was created a geo map using d3.js .In that i created a tooptip over each country. 我使用d3.js创建了一个geo map ,因此在每个国家/地区创建了一个tooptip。

Basically am creating tooltip while mousemove event. 基本上是在mousemove事件时创建工具提示。

Code for Tooltip: 工具提示代码:

.on("mousemove", function(d) {  

    return tooltip
    .style("left",d3.event.pageX+"px")
    .style("top",d3.event.pageY+10+"px")
    .style("visibility", "visible")             

    .html(function() {

    return '<div style="border:1px solid #ccc;">'
            +'<p style="font-weight:bold;">'+ d.properties.name +'</p>'
            +'<div>'+d.properties.tooltip+'</div></div>';                                                                               
       })   

})

My tooltip was working fine.I used d3.event.pageX and d3.event.pageY for getting current location.Basically am updating tooltip style left with d3.event.pageX and top with d3.event.pageY . 我的提示是工作fine.I使用d3.event.pageXd3.event.pageY用于获取当前location.Basically正在更新提示风格leftd3.event.pageXtopd3.event.pageY

So far tooltip is coming below for each country.But i need display tooltip based the window position. 到目前为止,每个国家的tooltip都在下面。但是我需要根据窗口位置显示工具提示。

For example: 例如:

If i mousemove the top placed countries like United states i need to display tooltip below the country.Suppose if i mousemove over the Antarctica i need to display tooltip above the country(Because Antarctica is the bottom country so that tooltip cant visible properly in the page) 如果我mousemove美国等排名靠前的国家/ United states需要该国家/地区下方显示tooltip 。如果我mousemoveAntarctica需要该国家/地区上方显示tooltip (因为Antarctica是最bottom country因此该tooltip在页面中无法正确显示)

So my problem is i need to change the tooltip postion based on the windows postion.Please help me solve this.I update my fiddle link below.Kindly take a look. 所以我的问题是我需要根据Windows位置更改工具提示位置。请帮助我解决此问题。我在下面更新了我的小提琴链接。请看一下。

Fiddle Link - http://jsfiddle.net/sam0kqvx/36/ 小提琴链接-http: //jsfiddle.net/sam0kqvx/36/

Try to calculate, If the mouse is reaching the bottom, If mouse position is at bottom then set top value as needed. 尝试计算,如果鼠标到达底部,如果鼠标位置在底部,则根据需要设置顶部值。 Below is the code 下面是代码

.style("top",function(){
                    if(current_position[1]+70 > 500){
                        return current_position[1]-50+"px";
                    }else{
                        return current_position[1]+100+"px"
                    }
                })

Hope you got it, If not ask me for more... 希望你明白了,如果不要求我更多...

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

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