Ges*_*set 2 html javascript jquery
我无法在鼠标单击的确切位置出现弹出窗口。我希望能够单击一个 DIV(位于表格单元格内),并在单击该 div 时显示一个弹出窗口。
现在我的弹出窗口正在工作,但是无论我尝试什么,弹出 DIV 都位于页面的中间位置。
超文本标记语言
<ul class="customdropdown" style="display:none;" id="xxx<?php echo $fetch['unit_no']; ?>" role="menu" aria-labelledby="menuitems">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"</i>Link 1</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"</i>Link 2</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
查询
$(document).ready(function () {
$('#myDiv<?php echo $fetch['unit_no']; ?>').click(function (e) {
var myDiv = document.getElementById('myDiv<?php echo $fetch['unit_no']; ?>');
var leftx = e.pageX-myDiv.offsetLeft;
var topy = e.pageY-myDiv.offsetTop;
$("#xxx<?php echo $fetch['unit_no']; ?>")
.css({
position: 'absolute',
left: leftx,
top: topy,
display: 'block'
})
});
});
Run Code Online (Sandbox Code Playgroud)
下面是发生的情况的屏幕截图:
这和DIV的定位有关吗?
Rok*_*jan 10
这取决于您的弹出窗口(工具提示)是否处于位置absolute或fixed父级是否有任何position其他static设置,以及弹出窗口是否位于包装器内部;或者是 的直接子代body。
前面三个例子:
\n<body>(最佳)案例:在父级position: absolute;内部弹出position: relative;。
overflow值不是visible)X = Event.pageX + Parent.scrollLeft - Parent.offsetLeft\nY = Event.pageY + Parent.scrollTop - Parent.offsetTop\nRun Code Online (Sandbox Code Playgroud)\n// !! Bad practice !!\nX = Event.pageX - Parent.offsetLeft \nY = Event.pageY - Parent.offsetTop \nRun Code Online (Sandbox Code Playgroud)\n例子:
\nconst el = (sel, par) => (par||document).querySelector(sel);\n\nconst elArea = el("#area");\nconst elPopup = el("#popup");\n\nconst showPopup = (evt) => {\n const elBtn = evt.currentTarget;\n \n Object.assign(elPopup.style, {\n left: `${evt.pageX + elBtn.scrollLeft - elBtn.offsetLeft}px`,\n top: `${evt.pageY + elBtn.scrollTop - elBtn.offsetTop}px`,\n display: `block`,\n });\n};\n\nelArea.addEventListener("click", showPopup);Run Code Online (Sandbox Code Playgroud)\r\nbody {\n height: 300vh; /* Just to force some scrollbars */\n}\n\n#area {\n position: relative; /* this is needed! */ \n height: 150px;\n background: #eee;\n margin: 40px;\n}\n\n#popup {\n position: absolute;\n height: 30px;\n background: gold;\n display: none;\n}Run Code Online (Sandbox Code Playgroud)\r\n<div id="area">\n Click here to shop popup, and scroll the window\n <div id="popup">Popup</div>\n</div>Run Code Online (Sandbox Code Playgroud)\r\n优点:
\n缺点:
\ntarget- 以允许与内部内容交互并且弹出窗口不移动。SomeClickedElement.append(EL_popup) .#area) 需要一个position集合(除了static),即:position: relative;案例:position: fixed;在父级外部(或内部)弹出,但通常作为子级<body>。
X = Event.clientX \nY = Event.clientY\nRun Code Online (Sandbox Code Playgroud)\n例子:
\nconst el = (sel, par) => (par||document).querySelector(sel);\n\nconst elArea = el("#area");\nconst elPopup = el("#popup");\n\nconst showPopup = (evt) => {\n Object.assign(elPopup.style, {\n left: `${evt.clientX}px`,\n top: `${evt.clientY}px`,\n display: `block`,\n });\n};\n\nelArea.addEventListener("click", showPopup);Run Code Online (Sandbox Code Playgroud)\r\nbody {\n height: 300vh; /* Just to force some scrollbars */\n}\n\n#area {\n /* position: relative; /* not necessary any more */\n height: 150px;\n background: #eee;\n margin: 40px;\n}\n\n#popup {\n position: fixed;\n height: 30px;\n background: gold;\n display: none;\n}Run Code Online (Sandbox Code Playgroud)\r\n<div id="area">\n Click here to shop popup, and scroll the window\n</div>\n\n<div id="popup">Popup fixed</div>Run Code Online (Sandbox Code Playgroud)\r\n优点:
\n#area- 如果单击在父级 ( ) 内部启动,则不需要额外的代码来阻止其移动。#area) 不需要position。Event.clientX这Event.clientY就是将其移动到新位置所需的一切。缺点:
\n案例:position: absolute;作为直接子项弹出body
X = Event.clientX + window.scrollX\nY = Event.clientY + window.scrollY\nRun Code Online (Sandbox Code Playgroud)\n例子:
\nconst el = (sel, par) => (par||document).querySelector(sel);\n\nconst elArea = el("#area");\nconst elPopup = el("#popup");\n\nconst showPopup = (evt) => {\n Object.assign(elPopup.style, {\n left: `${evt.clientX + window.scrollX}px`,\n top: `${evt.clientY + window.scrollY}px`,\n display: `block`,\n });\n};\n\nelArea.addEventListener("click", showPopup);Run Code Online (Sandbox Code Playgroud)\r\nbody {\n height: 300vh; /* Just to force some scrollbars */\n}\n\n#area {\n /* position: relative; /* not necessary any more */\n height: 150px;\n background: #eee;\n margin: 40px;\n}\n\n#popup {\n position: absolute;\n height: 30px;\n background: gold;\n display: none;\n}Run Code Online (Sandbox Code Playgroud)\r\n<div id="area">\n Click here to shop popup, and scroll the window\n</div>\n\n<div id="popup">Popup fixed</div>Run Code Online (Sandbox Code Playgroud)\r\n优点:
\nEvent.currentTarget是整个"body")如果单击其内容,弹出窗口将不会更改位置。Event.target.closest("#popup") !== EL_popuptrue最佳实践示例:
\n// DOM utility functions:\n\nconst el = (sel, par) => (par||document).querySelector(sel);\n\n\n// Popup:\n\nlet elPopup; // To remember the currently active popup\n\nconst handlePopup = (evt) => {\n // Get clicked target\n const elTarget = evt.target;\n\n // Clicked a popup, do nothing (Comment this line if not needed)\n if (elTarget.closest(".popup")) return;\n\n // Close currently open popup (if any):\n if (elPopup) elPopup.classList.remove("is-active");\n\n // Get initiator button\n const elBtn = elTarget.closest("[data-popup]");\n\n // Not a valid button\n if (!elBtn) return;\n\n // Get the popup\n elPopup = el(elBtn.dataset.popup); \n\n // No matching popup in this page, do nothing\n if (!elPopup) return; \n \n // Get its parent (BODY) so that we can calculate the min max\n // available space\n const elParent = elPopup.parentElement;\n \n // Position:\n const absX = evt.clientX + window.scrollX;\n const absY = evt.clientY + window.scrollY;\n \n const bcrParent = elParent.getBoundingClientRect();\n const bcrPopup = elPopup.getBoundingClientRect();\n \n const maxX = bcrParent.width - bcrPopup.width;\n const maxY = bcrParent.height - bcrPopup.height;\n \n const x = Math.max(0, Math.min(absX, maxX));\n const y = Math.max(0, Math.min(absY, maxY));\n \n // Show popup\n Object.assign(elPopup.style, {\n left: `${x}px`,\n top: `${y}px`,\n });\n elPopup.classList.add("is-active");\n\n};\n\nel("body").addEventListener("click", handlePopup);Run Code Online (Sandbox Code Playgroud)\r\n/*QuickReset*/ * { margin: 0; box-sizing: border-box; }\n\nbody {\n min-height: 300vh; /* just to force some demo scrollbars */\n}\n\n#area {\n background: #eee;\n padding: 10px;\n}\n\n.popup {\n position: absolute;\n background: gold;\n visibility: hidden;\n pointer-events: none;\n min-width: 100px;\n padding: 1rem;\n}\n\n.popup.is-active {\n visibility: visible;\n pointer-events: auto;\n}Run Code Online (Sandbox Code Playgroud)\r\n<div id="area" data-popup="#popup-one">Show "popup one"</div>\n\n<button data-popup="#popup-two" type="button">Show "popup two"</button>\n\n<br>Click anywhere to close an open popup \n<br>Click inside a popup. It will not close ()\n\n<div class="popup" id="popup-one">Popup one!</div>\n<div class="popup" id="popup-two">Popup TWO!</div>Run Code Online (Sandbox Code Playgroud)\r\n| 归档时间: |
|
| 查看次数: |
9058 次 |
| 最近记录: |