使DIV在悬停时显示在光标旁边

Make DIV show next to cursor on hover

本文关键字:光标 显示 DIV 悬停      更新时间:2023-09-26

我已经设置了我目前拥有的JSFiddle,基本功能工作,但div似乎只是显示它所在的位置,而不是通过光标。这在现场的网站结果在框显示没有地方附近的复选框,起初我认为这是一个问题的位置或z-索引,但现在我不这么认为。

好吧,我已经得到了这个工作在一个JS提琴,但由于某种原因,它不工作在网站上…它在菜单'Your Event'下

http://217.199.187.74/merlineventslondon.com/

任何帮助都会很棒- http://jsfiddle.net/ThisWebGuy/GYXE2/

JS

// Awards Dinner
$("input[type='checkbox'][value='awards-dinner']").mouseover(function(){         
$(".awardsbox").show();         
$(".awardsbox").css({             
top: (e.pageY + 50) + "px",             
left: (e.pageX + 15) + "px"         
});     
});
$("input[type='checkbox'][value='awards-dinner']").mouseout(function(){        
$(".awardsbox").hide();     
});
// Awards Dinner Dance
$("input[type='checkbox'][value='awards-dinner-dance']").mouseover(function(){         
$(".awardsdinnerdancebox").show();         
$(".awardsdinnerdancebox").css({             
top: (e.pageY + 50) + "px",             
left: (e.pageX + 15) + "px"         
});     
});
$("input[type='checkbox'][value='awards-dinner-dance']").mouseout(function(){        
$(".awardsdinnerdancebox").hide();     
});
// Cocktail Party
$("input[type='checkbox'][value='cocktail-party']").mouseover(function(){         
$(".cocktailbox").show();         
$(".cocktailbox").css({             
top: (e.pageY + 50) + "px",             
left: (e.pageX + 15) + "px"         
});     
});
$("input[type='checkbox'][value='cocktail-party']").mouseout(function(){        
$(".cocktailbox").hide();     
});
// Dinner
$("input[type='checkbox'][value='dinner']").mouseover(function(){         
$(".dinnerbox").show();         
$(".dinnerbox").css({             
top: (e.pageY + 50) + "px",             
left: (e.pageX + 15) + "px"         
});     
});
$("input[type='checkbox'][value='dinner']").mouseout(function(){        
$(".dinnerbox").hide();     
});
// Dinner and Dance
$("input[type='checkbox'][value='dinner-dance']").mouseover(function(){         
$(".dinnerdancebox").show();         
$(".dinnerdancebox").css({             
top: (e.pageY + 50) + "px",             
left: (e.pageX + 15) + "px"         
});     
});
$("input[type='checkbox'][value='dinner-dance']").mouseout(function(){        
$(".dinnerdancebox").hide();     
});
// Networking Event
$("input[type='checkbox'][value='networking-event']").mouseover(function(){         
$(".networkingbox").show();         
$(".networkingbox").css({             
top: (e.pageY + 50) + "px",             
left: (e.pageX + 15) + "px"         
});     
});
$("input[type='checkbox'][value='networking-event']").mouseout(function(){        
$(".networkingbox").hide();     
});
// Party
$("input[type='checkbox'][value='party']").mouseover(function(){         
$(".partybox").show();         
$(".partybox").css({             
top: (e.pageY + 50) + "px",             
left: (e.pageX + 15) + "px"         
});     
});
$("input[type='checkbox'][value='party']").mouseout(function(){        
$(".partybox").hide();     
});
HTML

<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="awards-dinner">Awards Dinner</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="awards-dinner-dance">Awards Dinner Dance</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="barbat-mitzvah">Bar/Bat Mitzvah</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="cocktail-party">Cocktail Party</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="dinner">Dinner</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="dinner-dance">Dinner Dance</label> <br>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="networking-event">Networking Event</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="party">Party</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="vip-experience">VIP Experience</label>
<div class="awardsbox">
    <p>Perfect for a formal dinner with an awards presentation. AV equipment and a team of technicians are included to ensure your event goes smoothly as well as a three course meal, unlimited wine, beer and soft drinks, staffing and furniture.</p>
</div>
<div class="awardsdinnerdancebox">
<p>Perfect for awards dinner celebrations or charity events. AV equipment and a team of technicians are included to ensure your event goes smoothly as well as  a three course meal, unlimited wine, beer and soft drinks, staffing and furniture. Not to forget a DJ and Disco to ensure your guests dance the night away.</p>
</div>
<div class="dinnerdancebox">
<p>This style of event is more formal and ideal for events where you may need to entertain guests or clients with a formal drinks reception, dinner and a great party afterwards.  A three course meal and unlimited wine, beer and soft drinks are included as well as AV, staffing and furniture.</p>
</div>
<div class="dinnerbox">
<p>This style of event is more formal and ideal for events where you may need to entertain clients without the full party or disco atmosphere. A three course meal and unlimited wine, beer and soft drinks are included as well as AV, staffing and furniture.</p>
</div>
<div class="partybox">
<p>Perfect for both corporate or private celebrations, parties include a DJ & Disco as well as a bowl food menu and unlimited wine, beer and soft drinks. AV, staffing and furniture are also included.</p>
</div>
<div class="networkingbox">
<p>A classic three hour networking event ideal for client parties where talking business is key. Canapés, unlimited wine, beer and soft drinks as well as staffing, AV and furniture are all included in our quotes.</p>
</div>
<div class="cocktailbox">
<p>A cocktail party is a classic three hour networking event ideal for client parties where talking business is key. Canapés, unlimited wine, beer and soft drinks as well as staffing, AV and furniture are all included in our quotes.</p>
</div>
CSS

.awardsbox, .awardsdinnerdancebox, .dinnerdancebox, .dinnerbox, .partybox, .networkingbox, .cocktailbox{
    display: none;
    padding: 7px;
    background-color: red;
    width: 300px;
    position:absolute;
    z-index: 999;
}

函数中缺少'e'

.mouseover(function(e)

.mouseout(function(e){

如果你为事件添加参数'e'就可以了

获取当前鼠标位置并使用如下的x-y坐标

$("input[type='checkbox'][value='awards-dinner']").mouseover(function(e){      
$(".awardsbox").show();         
$(".awardsbox").css({             
top: (e.pageY + 50) + "px",             
left: (e.pageX + 50) + "px"         
});     
});

获取function(e)参数中的事件为"e"

选中第一个复选框

编辑:对不起,我添加了代码来再次获得坐标。已经更新了代码并修改

http://jsfiddle.net/GYXE2/24/查看此提琴

太多的jquery代码,只是清理一些代码,这可能会减少文件大小。这就是jquery的作用。
$(".showPopUp").mouseover(function(e){         
  $("."+this.id).show();         
  $("."+this.id).css({             
    top:  e.pageY,             
    left: e.pageX         
  });     
}).mouseout(function(){        
  $("."+this.id).hide();     
});

这里有几件事需要注意,首先,当传递.css作为对象时,总是用引号括住键。

但是你在这里需要做的基本事情,在你的代码中,首先是在函数中传递事件,也就是…function(e),这样无论你接下来做什么,它都是可用的。

然后,您应该通过console.log(e)查看e的哪些属性可用。

通过这样做,您将看到(至少在FF中)坐标被标记为clientX和cliententy,因此您应该调用e.f clientX &e.clientY坐标。

我已经用你描述的第一个例子更新了你的jsfiddle,如果需要的话,看看并要求澄清。

$("input[type='checkbox'][value='awards-dinner']").mouseover(function(e){
  console.log(e.clientX);
  $(".awardsbox").show();         
  $(".awardsbox").css({             
    'top': e.clientY + "px",             
    'left': e.clientX + "px"         
  });     
});