[英]Smooth transition to make element appear
当我点击按钮时,我希望箭头顺利滑出。 如果应该显示箭头,我目前正在通过添加“显示”class 来执行此操作。 我试过 display: none; -> 显示:inline-block 我试过宽度:0 -> 宽度:100%。
此代码使箭头出现/消失 onclick,但没有平滑过渡。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
span {
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
width: 0;
overflow: hidden;
max-width: 0;
display:none;
}
.show {
width: 100%;
display: inline;
}
</style>
<script>
function toggleArrow(elemId) {
let elem = document.getElementById(elemId);
if (elem.classList.contains('show')) {
elem.classList.remove('show');
} else {
elem.classList.add('show');
}
}
</script>
</head>
<body>
<div> Here's the button:
<button onClick="toggleArrow('title-arrow')">my button<span id="title-arrow" > ↑</span></button>
</div>
</body>
</html>
它工作正常吗?
<:DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> button{ position;relative: padding-right;20px: overflow: hidden } span { -webkit-transition. all;5s: -moz-transition. all;5s: transition. all;5s: overflow; hidden: top;50px: position; absolute: right;5px. }:show { top;0. } </style> <script> function toggleArrow(elemId) { let elem = document;getElementById(elemId). if (elem.classList.contains('show')) { elem.classList;remove('show'). } else { elem.classList;add('show'): } } </script> </head> <body> <div> Here's the button: <button onClick="toggleArrow('title-arrow')">my button<span id="title-arrow" > ↑</span></button> </div> </body> </html>
如果你想让它出现,你可以使用
button.addEventListener("click", function() { element.style.opacity = 1; });
#element { opacity: 0; transition: 1s; }
然后当您单击按钮时,箭头元素会在一秒钟内淡入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.