I'm working on this script since 9 days, I found the script online, and from there I tried to add some code. The point of the script is to rotate the Div dynamically based on the distance they have between each other. In a way it works, if you resize the page at some point the divs turn their Y axes. I have mainly 2 problems, the first one is that if I add new divs they just are shown in a new line. The second problem is that those divs position should change, they need to get closer and they should move to the left side of the div. I hope somebody can help because I spent already 10 days on this and I can't find a solution. Thank you so much
function myFunction(distance) {
  //add browser check currently it set for safari
  // Code for Safari
  var degree = 0;
  if (distance <= -1 && distance >= -5) {
    degree = 15;
  } else if (distance < -5 && distance >= -10) {
    degree = 25;
  } else if (distance < -10 && distance >= -15) {
    degree = 30;
  } else if (distance < -15 && distance >= -20) {
    degree = 35;
  } else if (distance < -20) {
    degree = 45;
  }
  document.getElementById("panel").style.WebkitTransform = "rotateY(" + degree + "deg)";
  document.getElementById("panel2").style.WebkitTransform = "rotateY(" + degree + "deg)";
  document.getElementById("panel3").style.WebkitTransform = "rotateY(" + degree + "deg)";
  document.getElementById("panel4").style.WebkitTransform = "rotateY(" + degree + "deg)";
  //    document.getElementById("panel").style.marginRight= "100px";
  document.getElementById("panel2").style.marginRight = "300px";
  document.getElementById("panel3").style.marginRight = "30px";
  document.getElementById("panel4").style.marginRight = "30px";
  //        document.getElementById("panel5").style.WebkitTransform = "rotateY(45deg)"; 
  //    document.getElementById("panel6").style.WebkitTransform = "rotateY(45deg)";
  // Code for IE9
  //    document.getElementById("asd").style.msTransform = "rotateY(20deg)"; 
  // Standard syntax
  //    document.getElementById("asd").style.transform = "rotateY(20deg)"; 
}
function myFunctionb() {
  document.getElementById("panel").style.WebkitTransform = "rotateY(0deg)";
  document.getElementById("panel2").style.WebkitTransform = "rotateY(0deg)";
  document.getElementById("panel3").style.WebkitTransform = "rotateY(0deg)";
  document.getElementById("panel4").style.WebkitTransform = "rotateY(0deg)";
  //    document.getElementById("panel5").style.WebkitTransform = "rotateY(0deg)"; 
  //    document.getElementById("panel6").style.WebkitTransform = "rotateY(0deg)";
}
//    need to find a better solution
var first = document.getElementById("panel");
var second = document.getElementById("panel2");
var lastpanel = document.getElementById("panel4");
var lastbox = document.getElementById("last");
var container = document.getElementById("wrapper");
var notcongainer = container.offsetLeft;
var distance = container.offsetWidth - (lastpanel.offsetWidth + lastbox.offsetLeft + 4) + notcongainer;
console.log(distance);
var myVar;
var minDistance = 10;
function check() {
  myVar = setInterval(testcheck, 100);
}
//    First I check that the boxes lenght are as much as the container
//    Then I check the distance between 2 boxes
function testcheck() {
  if (distance < minDistance) {
    myFunction(distance);
  } else {
    myFunctionb();
  }
  distance = container.offsetWidth - (lastpanel.offsetWidth + lastbox.offsetLeft + 4) + notcongainer;
  /*console.log(distance)*/
}
//ADD NEW DIV    
function addDiv() {
  var div = document.createElement('div');
  div.className = "col-box";
  div.id = "newId";
  div.innerHTML = '<div class="hover panel"><div  id= "panel3" class="front"><div class="box1"><p>New Div</p></div></div></div>';
  document.getElementById('wrapper').appendChild(div);
}body {
  background-color: #ecf0f1;
  margin: 20px;
  font-family: Arial, Tahoma;
  font-size: 20px;
  color: #666666;
  text-align: center;
}
p {
  color: #ffffff;
}
.col-box {
  width: 22%;
  position: relative;
  display: inline;
  display: inline-block;
  margin-bottom: 20px;
  z-index: 1;
}
.end {
  margin-right: 0 !important;
}
/*-=-=-=-=-=-=-=-=-=-=- */
/* Flip Panel */
/*-=-=-=-=-=-=-=-=-=-=- */
.wrapper {
  width: 80%;
  height: 200px;
  margin: 0 auto;
  background-color: #bdd3de;
  hoverflow: hidden;
  border: 1px;
}
.panel {
  margin: 0 auto;
  height: 130px;
  position: relative;
  -webkit-perspective: 600px;
  -moz-perspective: 600px;
}
.panel .front {
  text-align: center;
}
.panel .front {
  height: inherit;
  position: absolute;
  top: 0;
  z-index: 900;
  text-align: center;
  -webkit-transform: rotateX(0deg) rotateY(0deg);
  -moz-transform: rotateX(0deg) rotateY(0deg);
  -webkit-transform-style: preserve-3d;
  -moz-transform-style: preserve-3d;
  -webkit-backface-visibility: hidden;
  -moz-backface-visibility: hidden;
  -webkit-transition: all .4s ease-in-out;
  -moz-transition: all .4s ease-in-out;
  -ms-transition: all .4s ease-in-out;
  -o-transition: all .4s ease-in-out;
  transition: all .4s ease-in-out;
}
.panel.flip .front {
  -webkit-transform: rotateY(45deg);
  -moz-transform: rotateY(180deg);
}
.col-box:hover {
  z-index: 1000;
}
.box1 {
  background-color: #14bcc8;
  width: 160px;
  height: 60px;
  margin-left: 5px;
  padding: 20px;
  border-radius: 10px;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
}<body onload="check()">
  <div id="wrapper" class="wrapper">
    <div id="first" class="col-box">
      <div class="hover panel">
        <div id="panel" class="front">
          <div class="box1">
            <p>Div 1</p>
          </div>
        </div>
      </div>
    </div>
    <div id="second" class="col-box">
      <div class="hover panel">
        <div id="panel2" class="front">
          <div class="box1">
            <p>Div 2</p>
          </div>
        </div>
      </div>
    </div>
    <div id="third" class="col-box">
      <div class="hover panel">
        <div id="panel3" class="front">
          <div class="box1">
            <p>Div 3</p>
          </div>
        </div>
      </div>
    </div>
    <div id="last" class="col-box">
      <div class="hover panel">
        <div id="panel4" class="front">
          <div class="box1">
            <p>Last Div</p>
          </div>
        </div>
      </div>
    </div>
    <button onclick="addDiv()">Add New Div</button>
  </div> 
     
    
