【问题标题】:Dynamically creating divs with position absolute which can expand to whole screen when clicked动态创建具有绝对位置的 div,单击时可以扩展到整个屏幕
【发布时间】:2017-09-03 22:15:22
【问题描述】:

我正在尝试创建这样的东西: https://tympanus.net/Development/FullscreenLayoutPageTransitions/

但我面临的问题是我的 div 是动态的 - 可以是来自 xhr 服务调用的任何数字。我正在尝试堆叠 div,但在点击时,它们不会从它们的位置增长到占据整个屏幕,而是从左上角增长,如下所示:

https://codepen.io/anon/pen/vJPNOq.

对于计数未知的动态列表,如何实现与第一个链接相同的效果?

<div>
  <h1>Your dashboard</h1>
  <span class="close">X</span>
  <section class="parent">
    <section>room1</section>
    <section>room2</section>
    <section>room3</section>
    <section>room4</section>
    <section>room5</section>
    <sectoin>room6</sectoin>
  </section>
</div>

section section{
  width:150px;
  height:150px;
  background-color:green;
  margin:10px;
  padding:30px;
  transition:all .5s linear;
}

.parent{
  position:relative;
  height:100%;
  width:100%;
  background-color:red;
}

.expanded{
  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
  z-index:999;
  background-color:red;
}

.close{
  position:absolute;
  top:100;
  right:0;
  z-index:1000;
  cursor:pointer;
}

$('.parent section').click(function(){
  $(this).addClass('expanded');
})

$('.close').click(function(){
  $('.parent section').each(function(){
    $(this).removeClass('expanded');
  })
})

【问题讨论】:

  • width: 50%float:left 添加到section section {} 可能会起作用
  • 这个效果不需要 jQ 或 JS。它可以简单地由 CSS 完成。
  • 你能说明如何为动态 div 做到这一点吗?
  • 您是否尝试将 position: absolute 更改为 position: fixed 以用于扩展类。
  • 是的。它没有工作

标签: javascript jquery css css-animations


【解决方案1】:

这是一个演示如何动态执行此操作的演示,如果您点击垃圾邮件会出现一些问题,但如果您禁用点击处理程序直到它完成动画,它们将无关紧要。或者,您可以缓存边界值(您可能只想避免一些重排),但具体情况可能会根据您使用此效果的网站而发生很大变化。

我也没有实现收缩效果,但我认为根据增长效果如何实现它可能相当明显。

const numberOfTiles = 9;
const totalColumns = 3;
const totalRows = Math.ceil(numberOfTiles / totalColumns);

const container = document.createElement('div');
Object.assign(container.style, {
  width: '80vw',
  height: '80vh',
  background: 'rgb(60, 61, 60)',
  transform: 'translate(10vw, 10vh)',
  lineHeight: 1 / totalRows * 100 + '%'
});

const tiles = [];
for (let row = 0; row < totalRows; ++row) {
  for (let col = 0; col < totalColumns; ++col) {
    if (tiles.length < numberOfTiles) {
      const tileContainer = document.createElement('div');
      Object.assign(tileContainer.style, {
        position: 'relative',
        width: 1 / totalColumns * 100 + '%',
        height: 1 / totalRows * 100 + '%',
        display: 'inline-block'
      });
      let randomColor = Math.ceil((Math.random() * Math.pow(255, 3))).toString(16);
      while (randomColor.length < 6) {
        randomColor = '0' + randomColor;
      }
      randomColor = '#' + randomColor;
      const tile = document.createElement('div');
      tile.classList.add('tile');
      Object.assign(tile.style, {
        width: '100%',
        height: '100%',
        background: randomColor,
        willChange: 'transform, left, top'
      });
      tile.addEventListener('click', (evt) => {
        if (tile.classList.toggle('fullscreen')) {
          let clientRect = tile.getClientRects();
          Object.assign(tile.style, {
            position: 'absolute',
            width: clientRect.width + 'px',
            height: clientRect.height + 'px',
            left: clientRect.left + 'px',
            top: clientRect.top + 'px',
            transition: '1s width, 1s height, 1s transform, 1s left, 1s top',
            zIndex: 100
          });
          setTimeout(() => {
            let clientRect = tile.getBoundingClientRect();
            Object.assign(tile.style, {
              left: 0,
              top: 0,
              width: '100vw',
              height: '100vh',
              transform: `translate(${-clientRect.left}px, ${-clientRect.top}px)`
            });
          }, 0);
        } else {
          Object.assign(tile.style, {
            width: '100%',
            height: '100%',
            left: 0,
            top: 0,
            transform: '',
            zIndex: 1
          });
          setTimeout(() => {
            Object.assign(tile.style, {
              zIndex: 0
            });
          }, 1000);
        }
      });
      tiles.push(tile);
      tileContainer.appendChild(tile);
      container.appendChild(tileContainer);
    }
  }
}

document.body.appendChild(container);
* {
  margin: 0;
  padding: 0;
}

【讨论】:

  • 当你多次点击它时,它填满屏幕的位置是不同的。并且其他瓦片的 z-index 有时比展开的瓦片更大。我真的要感谢你为完成这项工作而编写的精彩脚本。非常感谢!
  • 是的,我相信如果您禁用所有框上的点击处理程序直到完全完成转换,这两个问题都会消失。但是理想的方法在很大程度上取决于代码的结构,因此我将其排除在示例之外。或者,即使您在两次点击之间等待大约 1-2 秒,您是否也遇到过这些问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多