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

Eth*_*han 2 javascript jquery

嘿,我正在尝试制作一个小功能,我可以点击一个图标,然后会出现一个类似于灯箱的框,但是它固定在图标上.有办法吗?

  1. 告诉屏幕上图标的位置,然后
  2. 将我的框的左上角放在图标所在的位置,然后
  3. 我的盒子出现在那个位置吗?

谢谢!

Jos*_*ola 8

// 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();
Run Code Online (Sandbox Code Playgroud)

  • +1.应提醒OP应将其放在$(document).ready(function(){}块中. (2认同)