代码之家  ›  专栏  ›  技术社区  ›  goulashsoup

CSS-当按钮平行于按钮右边框扩展时,让文本从左开始

  •  1
  • goulashsoup  · 技术社区  · 7 年前

    我有一个按钮扩展,我希望按钮的文本与按钮的右边框平行移动,直到按钮被扩展到最大宽度。因此,当按钮折叠并从左向右移动时,文本在右侧不可见。

    目前我只是有消费按钮。举例来说,这是一个无止境的循环。

    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>
    1 回复  |  直到 7 年前
        1
  •  1
  •   Majid Akbari    7 年前

    看看这个: 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>