javascript - CSS - 当按钮平行于按钮右边框延伸时,让文本来自左侧
问题描述
我有一个展开的按钮,我希望按钮的文本与按钮的右边框平行移动,直到按钮扩展到最大宽度。因此,当按钮折叠并从左向右移动时,文本在右侧是不可见的。
目前我只有扩展按钮。为了说明,它是一个无限循环。
const button = document.getElementById("myButton");
const toggleClass = () => {
setTimeout(() => {
button.className = button.classList.contains('btn1') ? 'btn btn2' : 'btn btn1';
setTimeout(toggleClass, 100);
}, 1500);
}
toggleClass();
.btn {
background-color: yellow;
width: auto;
transition: all 1s linear;
overflow: hidden;
white-space: nowrap;
}
.btn1 {
max-width: 1000px;
}
.btn2 {
max-width: 5px;
}
<div>
<button id='myButton' class='btn btn1'>Some text</button>
</div>
解决方案
看看这个: https ://jsfiddle.net/xpvt214o/579357/
此外,您可以在此处运行代码段:
const button = document.getElementById("myButton");
const toggleClass = () => {
setTimeout(() => {
button.className = button.classList.contains('btn1') ? 'btn btn2' : 'btn btn1';
setTimeout(toggleClass, 100);
}, 1500);
}
toggleClass();
.btn {
background-color: yellow;
width: 80px;
height: 20px;
transition: all 1s linear;
overflow: hidden;
white-space: nowrap;
position: relative;
}
.btn span {
right: 5px;
top: 0px;
position: absolute;
}
.btn1 {
max-width: 1000px;
}
.btn2 {
max-width: 5px;
}
<div>
<button id='myButton' class='btn btn1'><span>Some text</span></button>
</div>
推荐阅读
- html - 尝试使用显示使主页上的文本居中:使用标题时的弹性
- php - 为什么 get_posts 函数会显示除一篇之外的所有文章?
- python - Seaborn:在 Y 轴上绘制一个显示属性均值的条形图?
- android - 达到高度时如何自动拆分recyclerview列
- dart - 异步代码未按预期执行
- javascript - 使用promotId时,Mapbox过滤器没有给出正确的结果
- regex - grep 仅精确匹配行
- google-chrome - 登录我在heroku上部署的Web应用程序时在chrome中收到数据泄露警告
- javascript - 导入现有项目地图以做出反应时未加载图像
- google-cloud-platform - Google Cloud TypeError:无法读取未定义的属性“Reader”