【发布时间】:2019-07-15 12:41:20
【问题描述】:
在运行下面的演示应用程序时,我遇到的问题是情节底部的悬停消息最终会从屏幕上消失。
有谁知道是否有办法调整位置,使整个消息始终落在屏幕边界(l,r,t,b)内?
require('shiny')
require('ggplot2')
library(DT)
ui <- pageWithSidebar(
headerPanel("Hover off the page"),
sidebarPanel(width = 2
),
mainPanel(
tags$head(
tags$style('
#my_tooltip {
position: absolute;
pointer-events:none;
z-index: 1;
padding: 0;
}'),
tags$script('
$(document).ready(function() {
setTimeout(function(){
$("[id^=FP1Plot]").mousemove(function(e) {
$("#my_tooltip").show();
$("#my_tooltip").css({
top: (e.offsetY) + "px",
left: (e.pageX -300) + "px"
});
});
},1000)});')
),
plotOutput('FP1Plot1' ,
width = 1000,
height = 800,
hover = hoverOpts(id = 'FP1Plot1_hover', delay = 0)
),
uiOutput("my_tooltip"),
style = 'width:1250px'
)
)
server <- function(input, output, session) {
ranges <- reactiveValues()
output$FP1Plot1 <- renderPlot({
ggplot(mtcars, aes(wt, mpg, color = as.factor(cyl))) + geom_point() +
coord_cartesian(xlim = ranges[[paste('FP1Plot1', 'x', sep = '')]],
ylim = ranges[[paste('FP1Plot1', 'y', sep = '')]]
)
})
tooltipTable <- reactive({
y <- nearPoints(mtcars, input$FP1Plot1_hover,
threshold = 15)
if(nrow(y)){
datatable(t(y), colnames = rep("", nrow(y)),
options = list(dom = 't'))
}
})
output$my_tooltip <- renderUI({
req(tooltipTable())
wellPanel(DTOutput("vals"),
style = 'background-color:#fff; padding:10px; width:400px;border-color:#339fff')
})
output$vals <- renderDT({
tooltipTable()
})
}
shinyApp(ui, server)
【问题讨论】:
标签: css r shiny hover mousemove