- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有五个图像元素,每个元素都包含一张羽毛图片。我想将它们添加到 DOM 中,其中一些以随机旋转 Angular 随机向右或向左翻转。
我希望每根羽毛都有动画,以便它们与 X 轴对齐(即回到 0 度),同时还保持 scaleX
.在我下面的代码中,重新对齐是有效的,但对于翻转的羽毛,它们也会在动画期间扭转原来的“未翻转”外观。
如何防止这种情况发生?我知道我可以通过使用 <div>
来实现这一点作为每根羽毛的 wrapper ,但有更好的解决方案吗?
Javascript
function feathersPuff() {
for (let i = 0; i < 5; i++) {
let feather = document.createElement("img");
feather.src = "imgs/feather" + i + ".svg";
document.body.appendChild(feather);
let plusOrMinus = Math.random() < 0.5 ? -1 : 1;
feather.style.transform = "scaleX(" + plusOrMinus + ") rotate(" + getRandomInt(50) * plusOrMinus + "deg)"
feather.classList.add("feather");
}
}
CSS
.feather {
display: block;
height: 2%;
position: absolute;
animation: realign;
animation-iteration-count: 1;
animation-direction: linear;
animation-timing-function: ease-in-out;
animation-duration: 500ms;
animation-delay: 0;
animation-fill-mode: forwards;
}
@keyframes realign {
100% { transform: rotate(0deg) }
}
最佳答案
如果我正确理解你的问题,那么一个解决方案可能是定义两个动画,其中一个有它的最终变换预乘以负 scaleX
作为否定羽毛(以确保它不会不要在动画期间产生不希望的“翻转”/扭曲):
/*
Define flipped feather animation with positive scale (redundant)
*/
@keyframes regular {
100% {
transform: scaleX(1) rotate(0deg)
}
}
/*
Define flipped feather animation with negated scale. Assuming the
starting transform has scaleX(-1), this animation will stop the
twisting effect from happening
*/
@keyframes flipped {
100% {
transform: scaleX(-1) rotate(0deg)
}
}
定义了这两个动画后,您还可以定义相应的修饰符类来随机选择并应用这些修饰符到羽毛元素:
function getRandomInt() {
return Math.random() * 180;
}
function feathersPuff() {
for (let i = 0; i < 10; i++) {
let feather = document.createElement("img");
/* Placeholder image - replace this with your svg */
feather.src = "https://pngriver.com/wp-content/uploads/2017/12/download-free-birds-feather-png-transparent-images-transparent-backgrounds-feather_PNG12958-300x160.png";
document.body.appendChild(feather);
/* Randomly orrientate feather */
if (Math.random() < 0.5) {
/* If regular orrientation, then apply regular modifier class which
will use the "regular" animation (without scaling) */
feather.classList.add("regular");
feather.style.transform =
"rotate(" + getRandomInt(50) + "deg)";
} else {
/* If flipped orrientation, then apply flipped modifier class which
will apply the "flipped" animation (with negated scaling factored in) */
feather.classList.add("flipped");
feather.style.transform =
"scaleX(-1) rotate(" + getRandomInt(50) + "deg)"
}
feather.classList.add("feather");
}
}
feathersPuff();
/* Define flipped feather animation with positive scale (redundant)*/
@keyframes regular {
100% {
transform: scaleX(1) rotate(0deg)
}
}
/* Define flipped feather animation with negated scale */
@keyframes flipped {
100% {
transform: scaleX(-1) rotate(0deg)
}
}
/* Modifier class which animates feathers of the "regular orientation" */
.feather.regular {
animation-name: regular;
}
/* Modifier class which animates feathers of the "flipped orientation" */
.feather.flipped {
animation-name: flipped;
}
.feather {
height: 30px;
display: block;
/* position: absolute; Removed this to prevent feathers overlapping to better
demonstrate the techniques final result */
animation-iteration-count: 1;
animation-direction: linear;
animation-timing-function: ease-in-out;
animation-duration: 2500ms;
animation-fill-mode: forwards;
}
关于javascript - 旋转元素也会改变 css 动画中的 scaleX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54848224/
我希望无论何时缩放 - 相邻元素都相应地移动。我怎样才能做到这一点?每当我进行缩放时,它都会位于相邻元素的顶部。 jsbin 相反,如果我更改 width 值,它会按我想要的方式工作,但我不能在转换中
使用 jquery 缩放一些文本 mousemove但无法弄清楚如何使右侧的单词(h2)从固定的右侧位置从单词的右侧向左扩展。相反,它总是从单词的左边缘开始缩放。 我希望这两个词组合起来始终填充窗口的
我是 GSAP 新手,正在使用 GSAP scaleX 和 x 组装一枚 2.5D 旋转硬币。 硬币的意思是a)在反面“旋转”,然后b)从反面旋转出来,然后c)在正面旋转,然后d)从正面旋转出来,然后
我试图让这个按钮只显示图标并只在悬停时显示按钮文本,但我无法让它只在 CSS 上工作。 我已经尝试过转换宽度和最大宽度,但没有提供预期的结果,转换没有发生。它“跳跃”了。 这是 HTML
我想制作一个当鼠标悬停在上面时向右滑动的菜单。但是,当我取下鼠标时,我希望它以与取出时相同的速度返回。 这是我的代码: .item { position: relative; background-c
我想每 1000 毫秒立即翻转图像。我正在尝试,但动画做了它应该做的事情(逐渐翻转图片)。如果我可以立即翻转图片,它会让人联想到行走的鸭子。我知道我可以使用 setInterval(),但我宁愿只在
我有一个绝对定位的父级,其中包含一些绝对定位的文本,需要拉伸(stretch)这些文本以匹配底层图像。它看起来像这样: some text some text
我正在使用 ObjectAnimator,无法弄清楚如何仅在一个方向(例如右侧)上缩放 x 轴。因为当我缩放时,它会双向缩放,然后我的 ImageView 超出了屏幕。我知道我可以添加 transit
我有五个图像元素,每个元素都包含一张羽毛图片。我想将它们添加到 DOM 中,其中一些以随机旋转 Angular 随机向右或向左翻转。 我希望每根羽毛都有动画,以便它们与 X 轴对齐(即回到 0 度),
我有一个旋转的 div,它比视口(viewport)长以覆盖屏幕。我使用以下代码将 div 居中: CSS div { position: absolute; left:-25%;
我的网站上有一条会游泳的鲨鱼。 http://jaredshurtliff.com/它使用 CSS Transform: scaleX 属性来回移动。它应该去一侧,转身,去另一侧,转身等。它适用于除
在浏览器中缩放时,scaleX() 属性不保持一致。 TranslateX() 的工作方式完全符合预期,这就是我伸出援手的原因。 我正在使用 Web Animations API 和 Vanill
当我通过 ray wenderlich 学习 Core Graphic 时, 一步是转换 UIBezierPath,var transform = CGAffineTransform(scaleX:
我有一个从一定百分比填充到另一个百分比 (0% - 50%) 的条。我想将它平滑地动画化到一个新的范围 (25% - 55%)。这意味着栏的宽度和位置都需要更改。我在同时顺利完成这两项操作时遇到了一些
我目前正在转换元素的 width 属性。我想用 scaleX 和 translateX 上的过渡替换它以获得更好的渲染性能。 我正在努力想出这两个概念之间适当的 1:1 转换。 下面是一个包含两行的框
这就是我想要的,我需要缩放和移动!同时是一个对象,现在我有这样的东西: element.addClass('scale').animate({ top: pxToMove.top+"px"
我正在使用 Daniel Cohen Gindi 的图表框架。我通过增加 scaleX 值并启用拖动来水平滚动图表,如这个问题的答案中所述 Set an horizontal scroll to my
我正在尝试生成一个简单的扩展条动画。除了受 scaleX() 转换影响的不断变化的 border-radius 外,一切正常,我可以根据自己的喜好控制它。有没有办法避免这种影响? 我假设为 borde
这是 code . 当在背景图像中使用不透明度的 scaleX 时,它在 mac 或 iphone 上的 safari 中不起作用,因此我们使用 scale 或 scale3d(sx,sy,sz) 来
我正在尝试使用 JQuery 打印出特定 div 的属性。我希望各种 css 属性显示在屏幕上的信息面板中。 我正在尝试显示 scaleX 但遇到很多困难。这是我目前的代码: //Size Contr
我是一名优秀的程序员,十分优秀!