0

Делаю активную блок схему на JS. Идея в том, при наведении на блок появляется кнопка (+), которая при нажатии создает новый блок рядом с нажатым блоком, аналогичный изначальному, с такими же кнопками. Идея в том, что блоки должны центрироваться относительно нажатого блока, создавая иерархию. Ниже иллюстрация (красные блоки те, которые нажимаются) и мой код. На данный момент две проблемы:

  1. Как сделать так, чтобы блоки центрировались относительно родительского, как на изображении?
  2. Какие есть варианты реализации изображения связи между блоками (стрелками или линиями, как на изображении)?Схема работы

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

var addAudit = function() {
    var col3 = document.querySelector('.col-3');
    var elemKod2 = document.querySelector('.in-kod2').innerHTML;

    col3.innerHTML += '<div>' + elemKod2 + '</div>';
};
var addAudit2 = function() {
    var col2 = document.querySelector('.col-2');
    var elemKod3 = document.querySelector('.in-kod3').innerHTML;

    col2.innerHTML += '<div>' + elemKod3 + '</div>';
};

var addAudit3 = function() {
    var col1 = document.querySelector('.col-1');
    var elemKod2 = document.querySelector('.in-kod2').innerHTML;

    col1.innerHTML += '<div>' + elemKod2 + '</div>';
};



var addAudit5 = function() {
    var col7 = document.querySelector('.col-7');
    var elemKod5 = document.querySelector('.in-kod5').innerHTML;

    col7.innerHTML += '<div>' + elemKod5 + '</div>';
};

var addAudit6 = function() {
    var col8 = document.querySelector('.col-8');
    var elemKod6 = document.querySelector('.in-kod6').innerHTML;

    col8.innerHTML += '<div>' + elemKod6 + '</div>';
};

var addAudit7 = function() {
    var col9 = document.querySelector('.col-9');
    var elemKod7 = document.querySelector('.in-kod7').innerHTML;

    col9.innerHTML += '<div>' + elemKod7 + '</div>';
};

document.onclick = function() {
    var elem = window.event.srcElement;
    if (elem.className === "btn") {
        addAudit();
    }
    else if (elem.className === "btn2") {
        addAudit2();
    }       
    else if (elem.className === "btn3") {
        addAudit3();
    }
    else if (elem.className === "btn4") {
        addAudit4();
    }       
    else if (elem.className === "btn5") {
        addAudit5();
    }
    else if (elem.className === "btn6") {
        addAudit6();
    }   
    else if (elem.className === "btn7") {
        addAudit7();
    }           
};
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;
  margin: 10%;
}

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;
  height: 100%;
  width: 10%;
}

.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;
  height: 100%;
  width: 5%;
}

.col-1 {
  left: 0;
}

.col-1-2 {
  left: 10%;
}

.col-2 {
  left: 15%;
}

.col-2-3 {
  left: 25%;
}

.col-3 {
  left: 30%;
}

.col-3-4 {
  left: 40%;
}

.col-4 {
  left: 45%;
}

.col-4-5 {
  left: 40%;
}

.col-5 {
  left: 45%;
}

.col-5-6 {
  left: 55%;
}

.col-6 {
  left: 60%;
}

.col-6-7 {
  left: 70%;
}

.col-7 {
  left: 75%;
}

.col-7-8 {
  left: 85%;
}

.col-8 {
  left: 90%;
}

.col-8-9 {
  left: 100%;
}

.col-9 {
  left: 105%;
}

.col-9-10 {
  left: 100%;
}

.col-10 {
  left: 100%;
}

.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;
}
<body>
  <div class="schema2">
    <span class="in-kod2">
      <div class="card in-kod white">
        <div class="multi-button">
          <span class="add">
            <button class="btn2">+</button>
          </span>
        </div>
       
        <div class="cardcontainer">
          <textarea class="arguments"></textarea> 

        <button class="" onclick="change_color(this,'#3f72c3')">Л</button>
        <button class="" onclick="change_color(this,'#743da0')">П</button>
        <button class="" onclick="change_color(this,'white')">Б</button>
        </div>
      </div>
    </span>
  </div>

<div class="schema2">
  <span class="in-kod3">
    <div class="card in-kod white">
      <div class="multi-button">
        <span class="add"><button class="btn3">+</button></span>
      </div>

      <div class="cardcontainer">  
        <textarea class="arguments"></textarea>

        <button class="buttons" onclick="change_color(this,'#3f72c3')">Л</button>
        <button class="buttons" onclick="change_color(this,'#743da0')">П</button>
        <button class="buttons" onclick="change_color(this,'#e3e8e6')">Б</button>
      </div>
    </div>
  </span>
</div>

<div class="schema2">
  <span class="in-kod5">
    <div class="card in-kod white">
      <div class="multi-button">
        <span class="add">
          <button class="btn6">+</button>
        </span>
      </div>

      <div class="cardcontainer">  
        <textarea class="arguments"></textarea> 

        <button class="" onclick="change_color(this,'#3f72c3')">Л</button>
        <button class="" onclick="change_color(this,'#743da0')">П</button>
        <button class="" onclick="change_color(this,'#e3e8e6')">Б</button>
      </div>
    </div>
  </span>
</div>

<div class="schema2">
  <span class="in-kod6">
    <div class="card in-kod white">
      <div class="multi-button">
        <span class="add">
          <button class="btn7">+</button>
        </span>
      </div>

      <div class="cardcontainer">  
        <textarea class="arguments"></textarea>

        <button class="" onclick="change_color(this,'#3f72c3')">Л</button>
        <button class="" onclick="change_color(this,'#743da0')">П</button>
        <button class="" onclick="change_color(this,'#e3e8e6')">Б</button>
      </div>
    </div>
  </span>
</div>
<div class="schema2">
  <span class="in-kod7">
    <div class="card in-kod white">
      <div class="multi-button">
        <span class="add">
          <button class="btn8">+</button>
        </span>
      </div>

      <div class="cardcontainer">  
        <textarea class="arguments"></textarea>      
        <button class="" onclick="change_color(this,'#3f72c3')">Л</button>
        <button class="" onclick="change_color(this,'#743da0')">П</button>
        <button class="" onclick="change_color(this,'#e3e8e6')">Б</button>
      </div>
    </div>
  </span>
</div>

<div class='col-1'></div>
<div class='col-1-2'></div>
<div class='col-2'></div>
<div class='col-2-3'></div>
<div class='col-3'></div>
<div class='col-3-4'></div>

<div class='col-4'>
  <span class="in-kod">
    <div class="card blue center">
      <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')">Л</button>
        <button class="" onclick="change_color(this,'#743da0')">П</button>
        <button class="" onclick="change_color(this,'#e3e8e6')">Б</button>
      </div>
    </div>
  </span>
</div>
<div class='col-5'></div>
<div class='col-6'>
  <span class="in-kod">
    <div class="card purple center">
      <div class="multi-button">
        <span class="add">
          <button class="btn5">+</button>
        </span>
      </div>
      
      <div class="cardcontainer">  
        <textarea class="arguments"></textarea> 
        <button class="" onclick="change_color(this,'#3f72c3')">Л</button>
        <button class="" onclick="change_color(this,'#743da0')">П</button>
        <button class="" onclick="change_color(this,'#e3e8e6')">Б</button>
      </div>
    </div>
  </span>
</div>
<div class='col-5'></div>
<div class='col-5-6'></div>
<div class='col-6-7'></div>
<div class='col-7'></div>
<div class='col-7-8'></div>
<div class='col-8'></div>
<div class='col-8-9'></div>
<div class='col-9'></div>
<div class='col-9-10'></div>
<div class='col-10'></div>
<div class='col-11'></div>

1 ответ 1

1

let distance = 250;

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

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

        let arrow_width = distance - father.offsetWidth;
        div.innerHTML =
            father.innerHTML +
            `<div class='arrow' style='left: ${-arrow_width}px;width:${arrow_width}px;'></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')">Л</button>
                <button class="" onclick="change_color(this,'#743da0')">П</button>
                <button class="" onclick="change_color(this,'#e3e8e6')">Б</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')">Л</button>
                <button class="" onclick="change_color(this,'#743da0')">П</button>
                <button class="" onclick="change_color(this,'#e3e8e6')">Б</button>
            </div>
        </div>
    </span>
</div>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.