var allowedWidthOfTheWindow = 992; // , var elemsToCompare = [], // DOM [[a,b,c],[a,b,c]] dataForCompareHeight = {}; // ( localArrayForStaticElems) var localArrayForStaticElems = []; // () var correctingHeights = false; // allowedWidthOfTheWindow, / var userScreenWidth, userScreenHeight; //
var elemsWithOurDataset = document.querySelectorAll("div[data-compareheight]");
// var i = 0; while(true){ if (elemsWithOurDataset.length <= 1) break; // 2 , if(!dataForCompareHeight[elemsWithOurDataset[i].dataset.compareheight]) { dataForCompareHeight[elemsWithOurDataset[i].dataset.compareheight] = ""; localArrayForStaticElems.push(elemsWithOurDataset[i].dataset.compareheight);}; dataForCompareHeight[elemsWithOurDataset[i].dataset.compareheight] += i + " "; i++; if (i == elemsWithOurDataset.length) break; // DOM }
// DOM for (var i = 0; i < localArrayForStaticElems.length; i++){ var numberOfElems = dataForCompareHeight[localArrayForStaticElems[i]].split(" "); numberOfElems.pop(); if (numberOfElems.length < 2) continue; var localArr = []; for (var j = 0; j < numberOfElems.length; j++){ localArr.push(elemsWithOurDataset[numberOfElems[j]]); } elemsToCompare.push(localArr); }
// () function setBreakePointWidthMinusScrollWidth(){ allowedWidthOfTheWindow = allowedWidthOfTheWindow - window.innerWidth + document.body.clientWidth; }
function checkUserScreenSize(){ userScreenWidth = document.documentElement.clientWidth; userScreenHeight = document.documentElement.clientHeight; }
// , - // function checkTheLargestElement(){ for (var i = 0; i < elemsToCompare.length; i++){ elemsToCompare[i].sort(compareNumericArray); } } // numberic function compareNumericArray(a, b) { return b.clientHeight + getComputedStyle(b).marginTop + getComputedStyle(b).marginBottom - a.clientHeight - getComputedStyle(a).marginTop - getComputedStyle(a).marginBottom; }
// - function checkAllowedMovingOrNot(){ if (userScreenWidth < allowedWidthOfTheWindow){ if (!correctingHeights) return; correctingHeights = false; } else{ correctingHeights = true; } }
function checkParams(elem){ if (elem.getAttribute("data-compareheightdo")){ var a = elem.getAttribute("data-compareheightdo").toLowerCase(); if (a == "margin"){ return 1; } if (a == "height"){ return 2; } if ((a == "margin height") || (a == "height margin")){ return 3; } } return 0; }
// margin-top function getTotalHeightOfChildren(parent){ var heightNow = 0; for (var i = 0; i < parent.children.length; i++){ if(i == 0){ heightNow += parent.children[i].offsetHeight + parseInt(getComputedStyle(parent.children[i]).marginBottom); continue; } if(i == (parent.children.length - 1)){ heightNow += parent.children[i].offsetHeight + parseInt(getComputedStyle(parent.children[i]).marginTop); continue; } heightNow += parent.children[i].offsetHeight + parseInt(getComputedStyle(parent.children[i]).marginTop + getComputedStyle(parent.children[i]).marginBottom); } return heightNow; }
// margin-top (, ) // height function makeElementsEqualHeight(){ if(correctingHeights){ // for(var i = 0; i < elemsToCompare.length; i++){ for (var j = 1; j < elemsToCompare[i].length; j++){ if (checkParams(elemsToCompare[i][j]) == 0) continue; if(checkParams(elemsToCompare[i][j]) == 1){ elemsToCompare[i][j].children[0].style.marginTop = parseInt((elemsToCompare[i][0].offsetHeight - getTotalHeightOfChildren(elemsToCompare[i][j]))/2) + "px"; } if(checkParams(elemsToCompare[i][j]) == 2){ if(!elemsToCompare[i][j].querySelector("div[class='notchange']")){ // notchange var div = document.createElement("div"); // , div.classList.add("notchange"); // DOM elemsToCompare[i][j].appendChild(div); // } elemsToCompare[i][j].lastChild.style.marginBottom = parseInt((elemsToCompare[i][0].offsetHeight - getTotalHeightOfChildren(elemsToCompare[i][j])) - 1) + "px"; } if(checkParams(elemsToCompare[i][j]) == 3){ if(!elemsToCompare[i][j].querySelector("div[class='notchange']")){ var div = document.createElement("div"); div.classList.add("notchange"); elemsToCompare[i][j].appendChild(div); } elemsToCompare[i][j].children[0].style.marginTop = parseInt((elemsToCompare[i][0].offsetHeight - getTotalHeightOfChildren(elemsToCompare[i][j]))/2) + "px"; elemsToCompare[i][j].lastChild.style.marginBottom = parseInt((elemsToCompare[i][0].offsetHeight - getTotalHeightOfChildren(elemsToCompare[i][j]))/2 - 1) + "px"; } } } } else{ // for(var i = 0; i < elemsToCompare.length; i++){ for (var j = 1; j < elemsToCompare[i].length; j++){ if (checkParams == 0) continue; elemsToCompare[i][j].children[0].style.marginTop = ""; elemsToCompare[i][j].lastChild.style.marginBottom = ""; } } } }
// function recountElems(){ checkUserScreenSize(); // checkTheLargestElement(); // ( data-compareheight="NAME") checkAllowedMovingOrNot(); // , makeElementsEqualHeight(); // margin-top margin-bottom };
setBreakePointWidthMinusScrollWidth(); // recountElems(); // ,
// window.onresize = function(e){ recountElems(); }
// var allowedWidthOfTheWindow = 992; // , var elemsToCompare = [], // DOM [[a,b,c],[a,b,c]] dataForCompareHeight = {}; // ( localArrayForStaticElems) var localArrayForStaticElems = []; // () var correctingHeights = false; // allowedWidthOfTheWindow, false var userScreenWidth, userScreenHeight; // var elemsWithOurDataset = document.querySelectorAll("div[data-compareheight]"); // var i = 0; while(true){ if (elemsWithOurDataset.length <= 1) break; if(!dataForCompareHeight[elemsWithOurDataset[i].dataset.compareheight]) { dataForCompareHeight[elemsWithOurDataset[i].dataset.compareheight] = ""; localArrayForStaticElems.push(elemsWithOurDataset[i].dataset.compareheight);}; dataForCompareHeight[elemsWithOurDataset[i].dataset.compareheight] += i + " "; i++; if (i == elemsWithOurDataset.length) break; } // DOM for (var i = 0; i < localArrayForStaticElems.length; i++){ var numberOfElems = dataForCompareHeight[localArrayForStaticElems[i]].split(" "); numberOfElems.pop(); if (numberOfElems.length < 2) continue; var localArr = []; for (var j = 0; j < numberOfElems.length; j++){ localArr.push(elemsWithOurDataset[numberOfElems[j]]); } elemsToCompare.push(localArr); } // // () function setBreakePointWidthMinusScrollWidth(){ allowedWidthOfTheWindow = allowedWidthOfTheWindow - window.innerWidth + document.body.clientWidth; } function checkUserScreenSize(){ userScreenWidth = document.documentElement.clientWidth; userScreenHeight = document.documentElement.clientHeight; } // , - // function checkTheLargestElement(){ for (var i = 0; i < elemsToCompare.length; i++){ elemsToCompare[i].sort(compareNumericArray); } } // numberic function compareNumericArray(a, b) { return b.clientHeight + getComputedStyle(b).marginTop + getComputedStyle(b).marginBottom - a.clientHeight - getComputedStyle(a).marginTop - getComputedStyle(a).marginBottom; } // margin-top function getTotalHeightOfChildren(parent){ var heightNow = 0; for (var i = 0; i < parent.children.length; i++){ if(i == 0){ heightNow += parent.children[i].offsetHeight + parseInt(getComputedStyle(parent.children[i]).marginBottom); continue; } if(i == (parent.children.length - 1)){ heightNow += parent.children[i].offsetHeight + parseInt(getComputedStyle(parent.children[i]).marginTop); continue; } heightNow += parent.children[i].offsetHeight + parseInt(getComputedStyle(parent.children[i]).marginTop + getComputedStyle(parent.children[i]).marginBottom); } return heightNow; } // margin-top (, ) // height function makeElementsEqualHeight(){ if(correctingHeights){ // for(var i = 0; i < elemsToCompare.length; i++){ for (var j = 1; j < elemsToCompare[i].length; j++){ if (checkParams(elemsToCompare[i][j]) == 0) continue; if(checkParams(elemsToCompare[i][j]) == 1){ elemsToCompare[i][j].children[0].style.marginTop = parseInt((elemsToCompare[i][0].offsetHeight - getTotalHeightOfChildren(elemsToCompare[i][j]))/2) + "px"; } if(checkParams(elemsToCompare[i][j]) == 2){ if(!elemsToCompare[i][j].querySelector("div[class='notchange']")){ // notchange var div = document.createElement("div"); // , div.classList.add("notchange"); // DOM elemsToCompare[i][j].appendChild(div); // } elemsToCompare[i][j].lastChild.style.marginBottom = parseInt((elemsToCompare[i][0].offsetHeight - getTotalHeightOfChildren(elemsToCompare[i][j])) - 1) + "px"; } if(checkParams(elemsToCompare[i][j]) == 3){ if(!elemsToCompare[i][j].querySelector("div[class='notchange']")){ var div = document.createElement("div"); div.classList.add("notchange"); elemsToCompare[i][j].appendChild(div); } elemsToCompare[i][j].children[0].style.marginTop = parseInt((elemsToCompare[i][0].offsetHeight - getTotalHeightOfChildren(elemsToCompare[i][j]))/2) + "px"; elemsToCompare[i][j].lastChild.style.marginBottom = parseInt((elemsToCompare[i][0].offsetHeight - getTotalHeightOfChildren(elemsToCompare[i][j]))/2 - 1) + "px"; } } } } else{ // for(var i = 0; i < elemsToCompare.length; i++){ for (var j = 1; j < elemsToCompare[i].length; j++){ if (checkParams == 0) continue; elemsToCompare[i][j].children[0].style.marginTop = ""; elemsToCompare[i][j].lastChild.style.marginBottom = ""; } } } } // - function checkAllowedMovingOrNot(){ if (userScreenWidth < allowedWidthOfTheWindow){ if (!correctingHeights) return; correctingHeights = false; } else{ correctingHeights = true; } } // , // , : // height // margin function checkParams(elem){ if (elem.getAttribute("data-compareheightdo")){ var a = elem.getAttribute("data-compareheightdo").toLowerCase(); if (a == "margin"){ return 1; } if (a == "height"){ return 2; } if ((a == "margin height") || (a == "height margin")){ return 3; } } return 0; } // function recountElems(){ checkUserScreenSize(); // checkTheLargestElement(); // ( data-compareheight="NAME") checkAllowedMovingOrNot(); // , makeElementsEqualHeight(); // margin-top }; setBreakePointWidthMinusScrollWidth(); // recountElems(); // , // window.onresize = function(e){ recountElems(); }
<head> <meta charset="UTF-8"> <style> .elem1{ width: 35%; border: 1px dashed #ccc; float: left; margin-left: 20px; } p{ padding: 0px; margin: 0px; } body{ padding: 0px; margin: 0px; } </style> </head> <body> <div class="parent"> <div class="elem1" data-compareheight="hello" data-compareheightdo="margin"><p>What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p></div> <div class="elem1" data-compareheight="hello" data-compareheightdo="margin"><p>What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's</p><p style="margin-top: 15px;">What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's</p></div> </div> <div style="clear: both"></div> <script src="compareHeight.js"></script> <!-- --> </body>
Source: https://habr.com/ru/post/325118/
All Articles