Javascript 我悬停的位置上的工具提示
我在我的项目中使用了工具提示,它工作得很好,但问题是我需要工具提示出现在我悬停的地方,而不是固定位置。当前它出现在固定位置。我需要在我悬停的地方出现工具提示 如何修改此代码以达到此目的Javascript 我悬停的位置上的工具提示,javascript,jquery,tooltip,Javascript,Jquery,Tooltip,我在我的项目中使用了工具提示,它工作得很好,但问题是我需要工具提示出现在我悬停的地方,而不是固定位置。当前它出现在固定位置。我需要在我悬停的地方出现工具提示 如何修改此代码以达到此目的 /* Position the tooltip relative to the class associated with the tooltip */ setTip = function(top, left){ var topOf
/* Position the tooltip relative to the class
associated with the tooltip */
setTip = function(top, left){
var topOffset = tip.height();
var xTip = (left-30)+"px";
var yTip = (top-topOffset-60)+"px";
tip.css({'top' : yTip, 'left' : xTip});
}
完整的代码在这里
$.fn.betterTooltip = function(options){
/* Setup the options for the tooltip that can be
accessed from outside the plugin */
var defaults = {
speed: 200,
delay: 300
};
var options = $.extend(defaults, options);
/* Create a function that builds the tooltip
markup. Then, prepend the tooltip to the body */
getTip = function() {
var tTip =
"<div class='tip'>" +
"<div class='tipMid'>" +
"</div>" +
"<div class='tipBtm'></div>" +
"</div>";
return tTip;
}
$("body").prepend(getTip());
/* Give each item with the class associated with
the plugin the ability to call the tooltip */
$(this).each(function(){
var $this = $(this);
var tip = $('.tip');
var tipInner = $('.tip .tipMid');
var tTitle = (this.title);
this.title = "";
var offset = $(this).offset();
var tLeft = offset.left;
var tTop = offset.top;
var tWidth = $this.width();
var tHeight = $this.height();
/* Mouse over and out functions*/
$this.hover(
function() {
tipInner.html(tTitle);
setTip(tTop, tLeft);
setTimer();
},
function() {
stopTimer();
tip.hide();
}
);
/* Delay the fade-in animation of the tooltip */
setTimer = function() {
$this.showTipTimer = setInterval("showTip()", defaults.delay);
}
stopTimer = function() {
clearInterval($this.showTipTimer);
}
/* Position the tooltip relative to the class
associated with the tooltip */
setTip = function(top, left){
var topOffset = tip.height();
var xTip = (left-30)+"px";
var yTip = (top-topOffset-60)+"px";
tip.css({'top' : yTip, 'left' : xTip});
}
/* This function stops the timer and creates the
fade-in animation */
showTip = function(){
stopTimer();
tip.animate({"top": "+=20px", "opacity": "toggle"}, defaults.speed);
}
});
};
$.fn.betterTooltip=函数(选项){
/*设置可编辑的工具提示选项
从插件外部访问*/
var默认值={
速度:200,,
延误:300
};
var options=$.extend(默认值,选项);
/*创建用于生成工具提示的函数
然后,将工具提示添加到主体*/
getTip=function(){
变量tTip=
"" +
"" +
"" +
"" +
"";
返回tTip;
}
$(“body”).prepend(getTip());
/*为每个项目指定与之关联的类
插件可以调用工具提示*/
$(this).each(function(){
var$this=$(this);
变量tip=$('.tip');
var tipInner=$('.tip.tipMid');
var tTitle=(this.title);
this.title=“”;
var offset=$(this.offset();
var tLeft=偏移量。左;
var tTop=offset.top;
var tWidth=$this.width();
var tHeight=$this.height();
/*鼠标悬停功能*/
$this.hover(
函数(){
html(tTitle);
设置提示(tTop、tLeft);
setTimer();
},
函数(){
停止计时器();
tip.hide();
}
);
/*延迟工具提示的淡入动画*/
setTimer=函数(){
$this.showTipTimer=setInterval(“showTip()”,默认值为.delay);
}
stopTimer=函数(){
clearInterval($this.showTipTimer);
}
/*相对于类定位工具提示
与工具提示关联*/
设置提示=功能(顶部,左侧){
var topOffset=tip.height();
var xTip=(左-30)+“px”;
变量yTip=(top-topOffset-60)+“px”;
css({'top':yTip,'left':xTip});
}
/*此函数用于停止计时器并创建
淡入动画*/
showTip=函数(){
停止计时器();
动画({“top”:“+=20px”,“不透明”:“toggle”},默认值为.speed);
}
});
};
为其指定鼠标坐标,而不是静态div坐标:
$(document).mousemove (function(e) {
mouseX = e.pageX;
mouseY = e.pageY;
});
jQuery文档:.hover(handlerIn,handlerOut)是.mouseenter(handlerIn.mouseleave(handlerOut)的缩写。这意味着您可以访问mousenter()的鼠标坐标
更改.hover绑定以使用鼠标悬停入口点的坐标:
/* Mouse over and out functions*/
$this.hover(
function(e) {
tipInner.html(tTitle);
setTip(e.clientY, e.clientX); //<--- using mouseenter coords here
setTimer();
},
function() {
stopTimer();
tip.hide();
}
).mousemove (function(e) {
setTip(e.pageY, e.pageX);
});
/*鼠标悬停和移出功能*/
$this.hover(
职能(e){
html(tTitle);
setTip(e.clientY,e.clientX);//我相信@Hidde下面的答案解决了您的问题,但我建议使用经过良好测试的插件来处理大量的场景,而不是重新发明轮子!我建议的一个是,但还有更多。我不鼓励将上述内容用作学习体验,但除此之外,我会坚持使用经过良好测试的代码。我会替换我的用你的代码编码它改变了位置,但是工具提示会出现在离我悬停的地方很远的地方。嗯…多远?工具提示应该出现在你鼠标进入div的地方,+/-你在setTip函数中对其进行的位置调整。我会更新代码片段,让你的工具提示跟踪上面的鼠标移动。你不需要将你可以在任何地方使用它。想想它能做什么,以及你如何使用它来提供你需要的功能。我们不是来编写你的代码的,我们是来帮助人们的。但是你必须解决你自己的问题。