简体   繁体   English

使DIV显示在悬停光标旁边

[英]Make DIV show next to cursor on hover

I have setup a JSFiddle of what I currently have, the basic functionality works but the div just seems to show whereever it is rather than by the cursor. 我已经设置了一个当前可用的JSFiddle,基本功能可以运行,但是div似乎仅显示了它的位置,而不是通过光标显示。 This on the live site results in the box showing no where near the checkbox, at first I thought it was an issue with the position or z-index but now I don't think so. 实时网站上的结果导致该框未显示复选框附近的位置,起初我认为这是位置或z-index的问题,但现在我不这么认为。

Ok I have got this working in a JS Fiddle but for some reason it's not working on the site... It's in the menu under 'Your Event' 好的,我已经在JS小提琴中使用了此工具,但是由于某种原因,它在网站上无法使用...位于“您的活动”下的菜单中

http://217.199.187.74/merlineventslondon.com/

Any help would be awesome - http://jsfiddle.net/ThisWebGuy/GYXE2/ 任何帮助都会很棒-http: //jsfiddle.net/ThisWebGuy/GYXE2/

JS 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 的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 的CSS

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

You are missing 'e' in the function 您在函数中缺少“ e”

.mouseover(function(e) ... .mouseover(function(e) ...

.mouseout(function(e){ ... .mouseout(function(e){ ...

It will work if you add parameter 'e' for event 如果为事件添加参数“ e”,它将起作用

Get the current mouse position and use the xy coordinates as following 获取当前鼠标位置并使用xy坐标,如下所示

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

get the event as "e" in the argument of function(e) function(e)的参数中将事件获取为“ e”

check the first checkbox in the fiddle 选中小提琴中的第一个复选框

Edit: sorry i added code to get the coordinates again .. have updated the code and fiddle 编辑:对不起,我添加了代码以再次获取坐标..已更新了代码并拨弄了琴弦

http://jsfiddle.net/GYXE2/24/ check this fiddle http://jsfiddle.net/GYXE2/24/检查此小提琴

dude Too much jquery code ,just cleaned up some code ,this might reduce the file size.And thats what jquery is for right. dude太多的jQuery代码,只清理了一些代码,这可能会减小文件的大小。

$(".showPopUp").mouseover(function(e){         
  $("."+this.id).show();         
  $("."+this.id).css({             
    top:  e.pageY,             
    left: e.pageX         
  });     
}).mouseout(function(){        
  $("."+this.id).hide();     
});

There are a couple of things to notice here, first when passing .css as object, always wrap the keys in quotes. 这里有两点需要注意,首先,当将.css作为对象传递时,始终将键括在引号中。

But the basic thing you need to do here, in your code, is first of all to pass the event in the function aka ..function(e) so that it is available in whatever you do next. 但是,您需要在代码中执行的基本操作是,首先在函数aka ..function(e)中传递事件,以便随后执行的任何操作都可以使用该事件。 Otherwise, e.whatever is undefined. 否则,无论什么都不确定。

Then, you should take a look at what properties of e you have available, by console.log(e). 然后,您应该通过console.log(e)看一下e的可用属性。

By doing so, you will see (in FF at least) the coordinates are noted as clientX and clientY, therefore you should call e.clientX & e.clientY coordinates respectively. 这样,您将看到(至少在FF中)坐标被标记为clientX和clientY,因此您应该分别调用e.clientX和e.clientY坐标。

I have updated your jsfiddle with the first example working as you describe, take a look and ask for clarifications if needed. 我已经按照您描述的第一个示例更新了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"         
  });     
});

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

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