vendredi 19 février 2021

Proportional vertical shift of new blocks JS

I am making an active block diagram in JS. The idea is that when you hover over a block, a (+) button appears, which, when pressed, creates a new block next to the pressed block, similar to the original one, with the same buttons. The idea is that the boxes should be centered on the clicked box, creating a hierarchy. Below is an illustration (the red blocks are the ones that are clicked) and my code. There are one problems at the moment: the variable is responsible for the offset relative to the parent block, but I can't think of how to make the blocks move by a fixed height, and centered relative to the parent block? enter image description here

let distance = 350;

function change_color(elem, color) {
    elem.parentElement.style.background = color;
}

let count = 0;

document.body.addEventListener("click", function (e) {
    if (e.target.classList.contains("btn")) {
        let div = document.createElement("div"),
            father = e.path[5];
            count = count + 100;
        div.classList = father.classList;
        div.style.left = father.offsetLeft + distance + "px";
        div.style.top = father.offsetTop + count + "px";

        let arrow_width = distance - father.offsetWidth;
        div.innerHTML =
            father.innerHTML +
            `<div class='arrow' style='left: ${-arrow_width}px;width:${arrow_width}px;'><div></div></div>`;
        document.body.append(div);
    }
});
html {
  font-family: FuturaMediumC;
  line-height: 1.15;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

article,
aside,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section {
  display: block;
}

body {
  margin: 0;
  font-family: FuturaMediumC;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: #212529;
  text-align: left;
  background: #f5f7fa;
}

html,
body {
  scrollbar-color: red #1a1a1a;
  scrollbar-width: thin;
}

.center {
  margin: 0;
  position: absolute;
  top: 50%;
  transform: translate(0%, -50%);
}

body .card {
  position: relative;
  height: 12rem;
  width: 10%;
  min-width: 200px;
  box-shadow: 0 0 2rem -1rem rgba(0, 0, 0, 0.05);
  display: inline-block;
  z-index: 99999999 !important;
}

body .card .multi-button {
  z-index: 0;
  position: absolute;
  top: 1.25rem;
  left: 1.25rem;
  border-radius: 100%;
  width: 0rem;
  height: 0rem;
  transform: translate(-50%, -50%);
  transition: 0.25s cubic-bezier(0.25, 0, 0, 1);
}

body .card .multi-button button {
  display: grid;
  place-items: center;
  position: absolute;
  width: 2rem;
  height: 2rem;
  border: none;
  border-radius: 100%;
  background: #000;
  color: #fff;
  transform: translate(-50%, -50%);
  cursor: pointer;
  transition: 0.25s cubic-bezier(0.25, 0, 0, 1);
  box-shadow: 0 0 0rem -0.25rem var(--background);
}

body .card .multi-button button:hover {
  background: var(--text);
  color: var(--background);
  box-shadow: 0 0 1rem -0.25rem var(--background);
}

body .card .multi-button button:first-child:nth-last-child(1):nth-child(1),
body .card .multi-button button:first-child:nth-last-child(1) ~ *:nth-child(1) {
  left: 25%;
  top: 25%;
}

body .card .multi-button button:first-child:nth-last-child(2):nth-child(1),
body .card .multi-button button:first-child:nth-last-child(2) ~ *:nth-child(1) {
  left: 37.5%;
  top: 18.75%;
}

body .card .multi-button button:first-child:nth-last-child(2):nth-child(2),
body .card .multi-button button:first-child:nth-last-child(2) ~ *:nth-child(2) {
  left: 18.75%;
  top: 37.5%;
}

body .card .multi-button button:first-child:nth-last-child(3):nth-child(1),
body .card .multi-button button:first-child:nth-last-child(3) ~ *:nth-child(1) {
  left: 50%;
  top: 15.625%;
}

body .card .multi-button button:first-child:nth-last-child(3):nth-child(2),
body .card .multi-button button:first-child:nth-last-child(3) ~ *:nth-child(2) {
  left: 25%;
  top: 25%;
}

body .card .multi-button button:first-child:nth-last-child(3):nth-child(3),
body .card .multi-button button:first-child:nth-last-child(3) ~ *:nth-child(3) {
  left: 15.625%;
  top: 50%;
}

body .card .multi-button button:first-child:nth-last-child(4):nth-child(1),
body .card .multi-button button:first-child:nth-last-child(4) ~ *:nth-child(1) {
  left: 62.5%;
  top: 18.75%;
}

body .card .multi-button button:first-child:nth-last-child(4):nth-child(2),
body .card .multi-button button:first-child:nth-last-child(4) ~ *:nth-child(2) {
  left: 37.5%;
  top: 18.75%;
}

body .card .multi-button button:first-child:nth-last-child(4):nth-child(3),
body .card .multi-button button:first-child:nth-last-child(4) ~ *:nth-child(3) {
  left: 18.75%;
  top: 37.5%;
}

body .card .multi-button button:first-child:nth-last-child(4):nth-child(4),
body .card .multi-button button:first-child:nth-last-child(4) ~ *:nth-child(4) {
  left: 18.75%;
  top: 62.5%;
}

body .card .cardcontainer {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 1rem;
  background: var(--background);
  color: var(--text);
}

body .card:hover .multi-button,
body .card .multi-button:focus-within {
  width: 10rem;
  height: 10rem;
}

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
  background-color: #1a1a1a;
}

::-webkit-scrollbar-thumb {
  background-color: #000;
  border-radius: 1em;
  box-shadow: inset 1px 1px 10px #f3faf7;
}

::-webkit-scrollbar-thumb:hover {
  background-color: #000;
}

::-webkit-scrollbar-button:vertical:start:decrement {
  background-color: #000;
}

::-webkit-scrollbar-button:vertical:end:increment {
  background-color: #000;
}

::-webkit-scrollbar-button:horizontal:start:decrement {
  background-color: #000;
}

::-webkit-scrollbar-button:horizontal:end:increment {
  background-color: #000;
}

.invisible {
  display: none !important;
}

.schema {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);
}

.optionsmenu {
  bottom: 0;
  width: 100%;
  height: 25%;
  background: #000;
  position: absolute;
}

.optionsmenu textarea {
  color: #000;
}

.col-1,
.col-2,
.col-3,
.col-4,
.col-5,
.col-6,
.col-7,
.col-8,
.col-9,
.col-10 {
  position: absolute;
}

.col-1-2,
.col-2-3,
.col-3-4,
.col-4-5,
.col-5-6,
.col-6-7,
.col-7-8,
.col-8-9,
.col-9-10 {
  position: absolute;
}

.schema2 {
  display: none;
}

.blue {
  --background: #3f72c3;
  --text: white;
}

.purple {
  --background: #743da0;
  --text: white;
}

.white {
  --background: #e3e8e6;
  --text: black;
}

.arguments {
  z-index: 9999999999;
  position: absolute;
  width: 100%;
  height: 80%;
  bottom: 0;
  background: transparent;
  color: white;
  font-weight: bolder;
  letter-spacing: 2;
}

#in-kod {
  width: 0px;
  height: 0px;
}

.add {
  z-index: 99999999 !important;
}

.btn {
  z-index: 99999999 !important;
  position: absolute;
  width: 25%;
  bottom: 0;
}
.btn2 {
  z-index: 99999999 !important;
  position: absolute;
  width: 25%;
  bottom: 0;
  left: 50%;
}
.btn3 {
  z-index: 99999999 !important;
  position: absolute;
  width: 25%;
  bottom: 0;
  right: 0;
}

.in-kod {
  display: block;
}

.arrow {
  position: absolute;
  top: 50%;
  height: 2px;
  background: #000;
}
  <div class='col-4' style="top:200px;left:50px;">
    <span class="in-kod">
        <div class="card blue">
            <div class="multi-button">
                <span class="add">
                    <button class="btn">+</button>
                </span>
            </div>
            <div class="cardcontainer">
                <textarea class="arguments"></textarea>
                <button class="" onclick="change_color(this,'#3f72c3')">P</button>
                <button class="" onclick="change_color(this,'#743da0')">B</button>
                <button class="" onclick="change_color(this,'#e3e8e6')">W</button>
            </div>
        </div>
    </span>
</div>
<div class='col-6' style="top:200px;left:800px;">
    <span class="in-kod">
        <div class="card purple">
            <div class="multi-button">
                <span class="add">
                    <button class="btn">+</button>
                </span>
            </div>
            <div class="cardcontainer">
                <textarea class="arguments"></textarea>
                <button class="" onclick="change_color(this,'#3f72c3')">P</button>
                <button class="" onclick="change_color(this,'#743da0')">B</button>
                <button class="" onclick="change_color(this,'#e3e8e6')">W</button>
            </div>
        </div>
    </span>
</div>
</html>



Aucun commentaire:

Enregistrer un commentaire