javascript - 在一个 css 类中设置最大宽度动画

标签 javascript html css animation css-transitions

我想要一个 CSS 类,当一个元素被添加到 document.body 时,它会以 max-width

动画

目前我知道如何用两个类来完成它并在之后添加第二个类。但是我需要向页面添加很多元素,用一个类来完成会更简洁。

我想将其转换为使用一个类:http://codepen.io/hellomihai/pen/yYBPjW

CSS:

.hide-me {
  overflow: hidden;
  max-width: 0;
}

.show-me {
  max-width: 1000px;
  transition: max-width 5s;
}

.my-stuff {
  height: 200px;
  width: 600px;
  background-color: red;
}

html:

$(document).ready(function() {
  var newDiv = document.createElement("div");
  document.body.appendChild(newDiv);
  newDiv.className = 'my-stuff hide-me';
  setTimeout(function() {
    $('.my-stuff').addClass('show-me');
  }, 1);
});

最佳答案

使用关键帧可以避免第二次添加类 like so !

.my-stuff {
  width: 600px;
  height: 200px;
  background-color: red;
  -webkit-animation-name: example;
  -webkit-animation-duration: 4s;
    animation-name: example;
   animation-duration: 4s;
}

@-webkit-keyframes example {
  from {
   width: 0;
  }
  to {
   width: 600px;
  }
}

keyframes example {
  from {
   width: 0;
  }
  to {
   width: 600px;
  }
}

$(document).ready(function() {
  var newDiv = document.createElement("div");
   document.body.appendChild(newDiv);

   // want to not use a setTimeout to add a class after
   setTimeout(function() {
        newDiv.className = 'my-stuff';
  }, 1);
 });

关于javascript - 在一个 css 类中设置最大宽度动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32262037/

相关文章:

javascript - Flot:堆叠条形标签重叠/不显示

javascript - 在页面顶部显示消息栏的简单方法

jquery - 在悬停和鼠标输入时显示 div,但当鼠标不在目标或 div 上时隐藏

javascript - jquery Accordion - onclick 打开所有 Accordion

javascript - 使用 jQuery 在 IE 和 Firefox 中获取字体 css 属性不起作用

javascript - 如何使用 onOpen 触发器脚本更改 Google 表格行为?

javascript - 如果用户单击后退按钮,我该怎么做才能防止出现咆哮消息

javascript - document.getElementById 似乎不起作用

html - 紧接指定文本后提取html表

CSS 定位 : Full-width header shall have the same left margin as the centered content-div