2012-02-29 195 views
0

我正在使用Adam Shaw的FullCalendar v1.5.3。我已经得到了几乎所有的东西,可以接受我正在尝试做一个“简单的”hovertip,并且不能完全得到我要找的东西。FullCalendar:在鼠标悬停时,我如何判断事件悬停的左上角?

我想让我的工具提示的左侧与我悬停的事件的左侧相同。我试图让工具提示悬停在事件之上。

我目前使用的代码似乎给我一个“随机”左和顶部鼠标进入FullCalendar事件的地方。

我当前的代码看起来像:

eventMouseover: function (event, jsEvent, view) { 
      var eventInfo = $("#EventInfoTip"); 
      var mousex = jsEvent.pageX + 20; //Get X coodrinates 
      var mousey = jsEvent.pageY + 20; //Get Y coordinates 
      var tipWidth = eventInfo.width(); //Find width of tooltip 
      var tipHeight = eventInfo.height(); //Find height of tooltip 

      //Distance of element from the right edge of viewport 
      var tipVisX = $(window).width() - (mousex + tipWidth); 
      //Distance of element from the bottom of viewport 
      var tipVisY = $(window).height() - (mousey + tipHeight); 

      if (tipVisX < 20) { //If tooltip exceeds the X coordinate of viewport 
       mousex = jsEvent.pageX - tipWidth - 20; 
      } if (tipVisY < 20) { //If tooltip exceeds the Y coordinate of viewport 
       mousey = jsEvent.pageY - tipHeight - 20; 
      } 
      //Absolute position the tooltip according to mouse position 
      eventInfo.css({ top: mousey, left: mousex }); 
      eventInfo.show(); //Show tool tip 
     } //end full calendar mouseover event 

EventInfoTip是页面上的简单跨度:

<span id = "EventInfoTip"> 
I'm over an event 
</span> 

是否有可能获得顶部和左侧,我徘徊在当前事件过度?

+0

不知道这是否会帮助,因此为注释:http://api.jquery.com/offset/ – mindandmedia 2012-02-29 22:36:34

回答

3

来自文档:“在回调函数中,this设置为事件的div”元素。

因此,您可以使用$(this).offset()来获取页面中的事件div位置。

var elOffset= $(this).offset(), offsetLeft=elOffset.left, offsetTop=elOffset.top 
+0

偏移就是我要找的。我上面的代码现在有点不合适,但这让我走上了正确的道路。 – 2012-03-01 16:19:56

+0

如果您使用的是'angularjs'或除jQuery之外的任何其他js框架,则this关键字将引用'window'而不是'div'元素,在这种情况下(如果您愿意编辑插件),只需将'this'关键字分配给'eventMouseover'触发器的另一个'param' – 2014-07-14 08:34:38

+0

@MuhiaNJoroge您的评论没有意义。 'this'的上下文在这里明确定义 – charlietfl 2014-07-14 11:47:21