全屏
<div class="not-supported"> Your browser doesn't support this demo. It is only supported in Chrome 125+</div>

<main>
  <div class="anchor">anchor</div>
  <div class="target">target</div>
</main>

<form class="stylesForm">
  <header>
    <h2>Edit Styles</h2>
  </header>
  <div class="targetStyle">
    <span class="className">.target {</span>

    <div class="property">
      <span class="propertyName">inset-area: </span>
      <input class="styleInput" type="text" id="insetAreaInput" value="bottom right" placeholder="styles here..." />
    </div>

    <div class="property">
      <span class="propertyName">position-visibility: </span>
      <input class="styleInput" type="text" id="positionVisibility" value="no-overflow" placeholder="styles here..." />
    </div>

    <span>}</span>
  </div>
  <button type="submit">Apply Styles</button>
</form>
.anchor {
  anchor-name: --my-anchor;
}

.target {
  position: absolute;
  position-anchor: --my-anchor;

  position-area: bottom right;
  position-visibility: no-overflow;
}

/* Aesthetic Changes */

:root {
  --anchor-size: clamp(50px, 8vw, 80px);
}

* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}

body {
  display: flex;
  flex-flow: row-reverse wrap;
  justify-content: center;
  align-items: center;

  gap: 40px;

  padding: 20px;

  min-height: 100vh;

  font-family: monospace;
}

main {
  display: flex;
  align-items: start;
  justify-content: start;
  padding: calc(var(--anchor-size) * 2) 0px 0px calc(var(--anchor-size) * 2);

  border: 2px solid black;
  border-radius: 10px;

  height: calc(var(--anchor-size) * 5);
  aspect-ratio: 1;

  position: relative;
  resize: both;
  overflow: hidden;
}

.anchor {
  display: flex;
  align-items: center;
  justify-content: center;

  border-radius: 10px;

  width: var(--anchor-size);
  aspect-ratio: 1;

  background-color: #ffbd59;
}

.target {
  display: flex;
  align-items: center;
  justify-content: center;

  width: var(--anchor-size);
  aspect-ratio: 1;

  border-radius: 10px;

  background-color: #cb6ce6;
}

header {
  display: flex;
  justify-content: space-between;
  aling-items: center;
}

.stylesForm {
  display: flex;
  flex-flow: column;
  gap: 10px;

  width: clamp(200px, 40vw, 400px);
}

.stylesForm input[type="text"] {
  all: unset;

  flex: 1 1 0;

  border: 1px solid grey;
  border-radius: 5px;
  padding: 5px;
}

.stylesForm button {
  all: unset;

  display: inline-block;

  border-radius: 4px;
  padding: 0 16px;

  height: 36px;
  min-width: 64px;

  cursor: pointer;
  font-weight: 500;

  line-height: 1.15;
  word-spacing: 0px;
  font-size: 14px;
  text-transform: uppercase;
  text-align: center;

  color: #000;
  background: #ffbd59;
  box-shadow: 0px 3px 1px -2px rgb(0 0 0 / 20%),
    0px 2px 2px 0px rgb(0 0 0 / 14%), 0px 1px 5px 0px rgb(0 0 0 / 12%);
  transition: box-shadow 280ms cubic-bezier(0.4, 0, 0.2, 1);

  &:hover {
    box-shadow: 0px 2px 4px -1px rgb(0 0 0 / 20%),
      0px 4px 5px 0px rgb(0 0 0 / 14%), 0px 1px 10px 0px rgb(0 0 0 / 12%);
  }
}

.targetStyle {
  display: flex;
  flex-flow: column;
  gap: 10px;

  width: 100%;

  padding: 15px;
  border-radius: 10px;

  font-weight: 600;

  background-color: #1d1e23;
  color: #fff;
}

.className {
  color: #d3c47a;
}

.property {
  display: flex;
  flex-flow: row wrap;
  align-items: center;
  gap: 10px;
}

.propertyName {
  color: #887573;
}

.not-supported {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2;

  display: flex;
  align-items: center;
  justify-content: center;

  width: 100vw;
  height: 40px;

  font-weight: 600;

  background-color: #ffbd59aa;
}

@supports (inset-area: bottom right) {
  .target {
    inset-area: bottom right;
  }
}

@supports (position-area: bottom right) or (inset-area: bottom right) {
  .not-supported {
    display: none;
  }
}
// Apply styles from form

function applyStyles(event) {
  event.preventDefault();

  const insetAreaInput = document.querySelector("#insetAreaInput").value;
  const positionVisibility = document.querySelector("#positionVisibility")
    .value;

  const target = document.querySelector(".target");

  // Create a new style element
  const styleElement = document.createElement("style");
  styleElement.textContent = `
  .target { 
      inset-area: ${insetAreaInput};  
      position-visibility: ${positionVisibility};
    }`;

  // Remove any previous style elements for the .target class
  document.querySelectorAll("style[data-target]").forEach((el) => el.remove());

  // Append the new style element
  styleElement.setAttribute("data-target", "true");
  document.head.appendChild(styleElement);
}

const stylesForm = document.querySelector(".stylesForm");
stylesForm.addEventListener("submit", (event) => applyStyles(event));
返回