Nessuna descrizione

virtual.html 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html> <title>Virtual Element Visual Test</title>
  2. <style>
  3. @import '/reset.css';
  4. #reference {
  5. position: absolute;
  6. top: 150px;
  7. left: 150px;
  8. width: 200px;
  9. height: 200px;
  10. background-color: red;
  11. box-shadow: inset 0 0 0 1px black;
  12. }
  13. #popper {
  14. width: 100px;
  15. height: 100px;
  16. background-color: rebeccapurple;
  17. box-shadow: inset 0 0 0 1px black;
  18. }
  19. #offset {
  20. position: relative;
  21. top: 100px;
  22. left: 100px;
  23. }
  24. </style>
  25. <div id="offset">
  26. <div id="reference">Reference Box</div>
  27. </div>
  28. <div id="popper">Popper Box</div>
  29. <script type="module">
  30. import { createPopper } from './dist/popper.js';
  31. const reference = document.querySelector('#reference');
  32. const popper = document.querySelector('#popper');
  33. function generateGetBoundingClientRect(x = 0, y = 0) {
  34. return () => ({
  35. width: 0,
  36. height: 0,
  37. top: y,
  38. right: x,
  39. bottom: y,
  40. left: x,
  41. });
  42. }
  43. const virtualElement = {
  44. getBoundingClientRect: generateGetBoundingClientRect(),
  45. };
  46. const instance = createPopper(virtualElement, popper);
  47. document.addEventListener('mousemove', ({ clientX: x, clientY: y }) => {
  48. virtualElement.getBoundingClientRect = generateGetBoundingClientRect(x, y);
  49. instance.update();
  50. });
  51. </script>