我希望能够缩小HTML标签以使其消失,并使其余项目平滑消失(以使它们不会跳到标签以前的位置)
const div = document.querySelector('.toGo');
div.addEventListener('click', event => {
div.className += ' animate';
// Simulate Angular's DOM modification
setTimeout(() => div.style.display = 'none', 1100);
setTimeout(() => div.style.display = 'block', 1200);
setTimeout(() => div.className = 'toGo', 1300);
});
.toGo, .toStay {
height: 50px;
width: 100%;
background: coral;
transition: all 1000ms ease-in-out;
overflow: hidden;
}
.toStay {
margin: 12px 0;
}
.animate {
height: 0;
width: 0;
opacity: 0;
}
<div class="toGo">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tristique sed nisi id aliquet. Etiam mauris diam, commodo in nisl ac, porttitor auctor mi. Nunc et blandit lacus. Donec facilisis dolor in arcu convallis, eu molestie magna placerat. Etiam blandit diam vitae nibh sollicitudin, ac gravida risus convallis. Nunc aliquet, turpis ac dignissim mollis, nulla nisi cursus leo, quis viverra nisi augue eget leo. Curabitur porta faucibus vestibulum.
</div>
<div class="toStay">Lorem Ipsum</div>
<p>Click on the first div</p>
现在,如您所见,文本正在移动,因为空间在缩小。
首先,我尝试使用transform
属性,但是第二个div跳到第一个div的位置(因为我使用的是Angular,并且一旦动画是完成)。
const div = document.querySelector('.toGo');
div.addEventListener('click', event => {
div.className += ' animate';
// Simulate Angular's DOM modification
setTimeout(() => div.style.display = 'none', 1100);
setTimeout(() => div.style.display = 'block', 2100);
setTimeout(() => div.className = 'toGo', 2200);
});
.toGo, .toStay {
height: 50px;
width: 100%;
background: coral;
transition: all 1000ms ease-in-out;
overflow: hidden;
}
.toStay {
margin: 12px 0;
}
.animate {
transform: scale(0);
opacity: 0;
}
<div class="toGo">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tristique sed nisi id aliquet. Etiam mauris diam, commodo in nisl ac, porttitor auctor mi. Nunc et blandit lacus. Donec facilisis dolor in arcu convallis, eu molestie magna placerat. Etiam blandit diam vitae nibh sollicitudin, ac gravida risus convallis. Nunc aliquet, turpis ac dignissim mollis, nulla nisi cursus leo, quis viverra nisi augue eget leo. Curabitur porta faucibus vestibulum.
</div>
<div class="toStay">Lorem Ipsum</div>
<p>Click on the first div</p>
然后我尝试使用宽度/高度的此解决方案,但是文本在移动。然后,我终于尝试使用其中的容器,但是它并没有改变任何东西(除非我为该内部容器设置了固定的大小)。
有没有一种方法可以实现,不对内部容器设置固定的宽度/高度?
编辑人们实际上正在要求关闭我的问题,因为它“不清楚”。如果这不太明显,我希望第一个代码段停止移动文本,第二个代码段平稳地将第二个div替换为第一个div(不跳动)。
这样的事情,内部容器没有使用固定的大小
const div = document.querySelector('.toGo');
div.addEventListener('click', event => {
div.className += ' animate';
// Simulate Angular's DOM modification
setTimeout(() => div.style.display = 'none', 1100);
setTimeout(() => div.style.display = 'block', 1200);
setTimeout(() => div.className = 'toGo', 1300);
});
.toGo, .toStay {
height: 50px;
width: 100%;
background: coral;
transition: all 1000ms ease-in-out;
overflow: hidden;
}
.container {
height: 50px;
width: 100vw;
}
.toStay {
margin: 12px 0;
}
.animate {
height: 0;
width: 0;
opacity: 0;
}
<div class="toGo">
<div class="container">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tristique sed nisi id aliquet. Etiam mauris diam, commodo in nisl ac, porttitor auctor mi. Nunc et blandit lacus. Donec facilisis dolor in arcu convallis, eu molestie magna placerat. Etiam blandit diam vitae nibh sollicitudin, ac gravida risus convallis. Nunc aliquet, turpis ac dignissim mollis, nulla nisi cursus leo, quis viverra nisi augue eget leo. Curabitur porta faucibus vestibulum.
</div>
</div>
<div class="toStay">Lorem Ipsum</div>
<p>Click on the first div</p>
答案 0 :(得分:1)
您在第一段代码中遇到的主要问题是与页边折叠产生跳跃效果有关。考虑一个flexbox容器,以防止边距崩溃:
然后您可以考虑将剪切路径动画与高度动画组合在一起(不要设置宽度动画以避免收缩效果)
const div = document.querySelector('.toGo');
div.addEventListener('click', event => {
div.className += ' animate';
// Simulate Angular's DOM modification
setTimeout(() => div.style.display = 'none', 1100);
setTimeout(() => div.style.display = 'block', 1200);
setTimeout(() => div.className = 'toGo', 1300);
});
body {
display:flex;
flex-direction:column;
}
.toGo, .toStay {
height: 50px;
width: 100%;
background: coral;
transition: all 1000ms ease-in-out;
overflow: hidden;
clip-path:polygon(0 0,100% 0,100% 100%, 0 100%);
}
.toStay {
margin: 12px 0;
}
.animate {
height: 0;
opacity: 0;
clip-path:polygon(0 0,0% 0,0% 100%, 0 100%);
}
<div class="toGo">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tristique sed nisi id aliquet. Etiam mauris diam, commodo in nisl ac, porttitor auctor mi. Nunc et blandit lacus. Donec facilisis dolor in arcu convallis, eu molestie magna placerat. Etiam blandit diam vitae nibh sollicitudin, ac gravida risus convallis. Nunc aliquet, turpis ac dignissim mollis, nulla nisi cursus leo, quis viverra nisi augue eget leo. Curabitur porta faucibus vestibulum.
</div>
<div class="toStay">Lorem Ipsum</div>
<p>Click on the first div</p>
您可以调整剪辑路径以控制动画的制作方式。
const div = document.querySelector('.toGo');
div.addEventListener('click', event => {
div.className += ' animate';
// Simulate Angular's DOM modification
setTimeout(() => div.style.display = 'none', 1100);
setTimeout(() => div.style.display = 'block', 1200);
setTimeout(() => div.className = 'toGo', 1300);
});
body {
display:flex;
flex-direction:column;
}
.toGo, .toStay {
height: 50px;
width: 100%;
background: coral;
transition: all 1000ms ease-in-out;
overflow: hidden;
clip-path:polygon(0 0,100% 0,100% 100%, 0 100%);
}
.toStay {
margin: 12px 0;
}
.animate {
height: 0;
opacity: 0;
clip-path:polygon(50% 0,50% 0,50% 100%, 50% 100%);
}
<div class="toGo">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tristique sed nisi id aliquet. Etiam mauris diam, commodo in nisl ac, porttitor auctor mi. Nunc et blandit lacus. Donec facilisis dolor in arcu convallis, eu molestie magna placerat. Etiam blandit diam vitae nibh sollicitudin, ac gravida risus convallis. Nunc aliquet, turpis ac dignissim mollis, nulla nisi cursus leo, quis viverra nisi augue eget leo. Curabitur porta faucibus vestibulum.
</div>
<div class="toStay">Lorem Ipsum</div>
<p>Click on the first div</p>