[英]JQuery: Show a hidden div at mouse position when a link is clicked
Suppose I have 2 links in my page and I want a hidden div to appear at the position the mouse is located (mouse x,y) at on the page when either of links is clicked.假设我的页面中有 2 个链接,并且我希望在单击任一链接时在页面上鼠标所在的位置(鼠标 x,y)显示一个隐藏的 div。 Also, I'd like to pass in a value to set the title for the div (see divTitle id).
另外,我想传入一个值来设置 div 的标题(请参阅 divTitle id)。
How can I accomplish this using jQuery?如何使用 jQuery 完成此操作?
Hidden Div:隐藏分区:
<div class="boxFAQ" id="questionMarkId">
<span id="divTitle"></span>
SHOW ME!
</div>
The below code will give you idea about showing the div.下面的代码将让您了解显示 div。 Proceed with your requirements and then hide it accordingly or display message as you need
继续您的要求,然后相应地隐藏它或根据需要显示消息
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
$('.alink').click(function(e){
$('#questionMarkId').hide();
$('#questionMarkId').css({'top':e.pageY-50,'left':e.pageX, 'position':'absolute', 'border':'1px solid black', 'padding':'5px'});
$('#questionMarkId').show();
});
});
</script>
</head>
<body>
<div class="boxFAQ" id="questionMarkId" style="display: none;">
<span id="divTitle"></span>
SHOW ME!
</div>
<br /><br /><br /><br />
<a href="#" class="alink">Link 1</a>
<a href="#" class="alink">Link 2</a>
</body>
</html>
UPDATED更新
create a function which will be invoked on clicking of the link.创建一个将在单击链接时调用的函数。 To this function pass your message (any number of parameter)
向此函数传递您的消息(任意数量的参数)
So your link will look like this所以你的链接看起来像这样
<a href="#" class="alink" onclick="showTitle('This is the title')">Link 1</a>
<a href="#" class="alink" onclick="showTitle('This is another title')">Link 2</a>
Function will look like this函数看起来像这样
function showTitle(message)
{
$('#questionMarkId').hide();
$('#questionMarkId').css({'top':e.pageY-50,'left':e.pageX, 'position':'absolute', 'border':'1px solid black', 'padding':'5px'});
$('#questionMarkId').show();
$('#divTitle').html(message);
}
UPDATED更新
Functions with event
parameter带
event
参数的函数
function showTitle(message, evt)
{
var e = e || evt || window.event;
// ... rest of the code
}
With the context you provided, here's a help:根据您提供的上下文,这是一个帮助:
$('#questionMarkId').hide();
$('a').on('click', function(e) {
e.preventDefault();
$('#questionMarkId').css( 'position', 'absolute' );
$('#questionMarkId').css( 'top', e.pageY );
$('#questionMarkId').css( 'left', e.pageX );
$('#questionMarkId').show();
});
And the fiddle link: http://jsfiddle.net/m6XPP/和小提琴链接: http : //jsfiddle.net/m6XPP/
To get ie to find the mouse coordinates, I had to add the following to the function.为了让 ie 找到鼠标坐标,我必须将以下内容添加到函数中。 I'm surprised there isn't a more "Jquery" way to solve this.
我很惊讶没有更“Jquery”的方法来解决这个问题。 Thanks again for all your help!
再次感谢你的帮助!
var posx;
var posy;
if (typeof e.pageY === "undefined")
{ //will e work here?
posx = window.event.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = window.event.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
alert("was undefined!");
}
else{
posx = e.pageX;
posy = e.pageY;
alert("was NOT undefined!");
}
alert("posy:" + posy);
alert("posX:" + posx);
Maybe help someone in the future as i landed on this page just now - but here is my solution当我刚刚登陆此页面时,可能会在将来帮助某人 - 但这是我的解决方案
$(document).ready(function() {
$('.hider').hide();
$(document).mousemove(function(event) {
var x = event.pageX;
var y = event.pageY;
//output mouse movements
//console.log(x + y);
$('h3').html("X="+x+"Y="+y+"");
$(document).on('click',function(event) {
$('.hider').show();
$('.hider').css({'background-color' : 'red','width' : '250px','height' : '250px','position' : 'absolute','top': y,'left' : x});
});
});
});
Working codepen https://codepen.io/desjardins2019/pen/XWNpmXL工作代码笔https://codepen.io/desjardins2019/pen/XWNpmXL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.