简体   繁体   English

jquery - 在我点击的地方出现div?

[英]jquery - make a div appear where I click?

Hey, I'm trying to make a little feature where I can click on an icon, and a box will appear that is similar to lightbox, but anchored on the icon. 嘿,我正在尝试制作一个小功能,我可以点击一个图标,然后会出现一个类似于灯箱的框,但是它固定在图标上。 Is there a way to 有办法吗?

  1. Tell where the icon is on the screen, and then 告诉屏幕上图标的位置,然后
  2. Have the top left corner of my box placed where the icon is, and then 将我的框的左上角放在图标所在的位置,然后
  3. Have my box appear in that location? 我的盒子出现在那个位置吗?

Thanks! 谢谢!

// 1.Tell where the icon is on the screen
var off = $("#iconId").offset();

// 2.Have the top left corner of my box placed where the icon is
var div = $("#divId");
div.css({ position: "absolute", top: off.top, left: off.left });

// 3.Have my box appear in that location?
div.show();
$(function() {
    $("#icon").click(function(){
        var offset = $(this).offset();
        $("#pop").css("position","absolute").css("top",offset.top+$(this).height()).css("left",offset.left);
        $("#pop").show();
    });
});

offset gets you the position of your icon and then you use those values to set the top and left offsets for your popup offset会使您获得图标的位置,然后使用这些值为弹出窗口设置顶部和左侧偏移

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

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