简体   繁体   English

调整大小时的动态Flex项目

[英]Dynamic Flex Item on resize

New web developer here, building a calendar if possible with pure JS only. 这里是新的Web开发人员,如果可能的话,仅使用纯JS构建日历。

As of now: https://jsfiddle.net/q7h3fm9c/ , I'm only aware of wrapping elements on overflow, how do I 'destroy' and 'create' new nodes in a responsive fashion so that there is only 1 row with the optimum amount of nodes? 到目前为止: https : //jsfiddle.net/q7h3fm9c/ ,我只知道溢出时会包装元素,如何以响应方式“破坏”和“创建”新节点,这样只有1行最佳数量的节点? Heres the bit where nodes are created manually: 这是手动创建节点的位:

for(var i = 0; i < 4; i++) addNewDayObject(i, "lel", "lel", 2015)

I've came across media query in my attempts at googling a solution, however I would very much prefer not to hard code the width values to trigger code as the flex-items will have dynamic widths. 我在尝试搜索解决方案时遇到了媒体查询,但是我非常希望不要硬编码宽度值来触发代码,因为flex-items具有动态宽度。

If just 'hiding' the consecutive rows is what you need, you can set the height element of the container div to only show one row. 如果只需要“隐藏”连续的行,则可以将容器div的height元素设置为仅显示一行。 This should work as long as the height is fixed. 只要高度固定就可以。

Please see my fiddle here . 在这里看我的小提琴。

CSS CSS

#dayObjContainer {
    max-height: 124px; /* max height here*/
    overflow: hidden;
    padding-bottom: 5px;
    display: flex;
    flex-grow: 1;
    flex-wrap: wrap;
}
.dayObj {
    padding: 10px 0px;
    width: 250px;
    color: #FFF5AA;
    background-color: #D4C96A;
    text-align: center;
    margin: 0px 5px 5px; /* added bottom margin here */
    box-shadow: 2px 2px 5px #888888;
}

I discovered window.onresize and everything is solved. 我发现window.onresize,一切都解决了。 Does javascript + browser have documentation that lists all the methods and properties at my disposal? javascript +浏览器是否具有列出我可以使用的所有方法和属性的文档? Guesswork and google is making learning abit slow for me :\\ 猜猜工作和Google对我来说有点慢了:\\

window.onload = init;
var dayObjTemplate;
var containerRef;
var objCount = 0;
function init() {
  dayObjTemplate = document.getElementById("obj0");
  containerRef = dayObjTemplate.parentNode;
  dayObjTemplate.parentNode.removeChild(dayObjTemplate);
  // capture day object template and remove from DOM

  for(var i = 0; i < 1; i++) addNewDayObject(11, 8, 2015)

  window.onresize = function updateNewContainerWidth(argument) {
    var conWdt = containerRef.offsetWidth;
    while(objCount < Math.floor(conWdt / (250+5))) {
      addNewDayObject(11, 8, 2015);
    }
    while(objCount > 1 && objCount > Math.floor(conWdt/(250+5))) {
      removeLastDayObject();
    }
  }
}

function removeLastDayObject() {
  containerRef.removeChild(document.getElementById("obj"+objCount));
  objCount--;
}

function addNewDayObject(day, month, year) {
  var d = new Date(year, month, day);
  var dayNames=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

  objCount++;
  var clone = dayObjTemplate.cloneNode(true);
  clone.id = "obj" + objCount;
  clone.getElementsByClassName("dayObjDayLabel")[0].innerHTML = dayNames[d.getDay() - 1];
  clone.getElementsByClassName("dayObjNumLabel")[0].innerHTML = day;
  clone.getElementsByClassName("dayObjMYLabel")[0].innerHTML = month + ", " + year

  document.getElementById("dayObjContainer").appendChild(clone);
}

excuse my messy code, but those who face the same problem as me, hope this helps. 请原谅我凌乱的代码,但是那些与我面临同样问题的人希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM