【问题标题】:Change button content with css: text from right to left使用 css 更改按钮内容:从右到左的文本
【发布时间】:2019-12-11 11:27:26
【问题描述】:

我想要一个按钮,如果有人将鼠标悬停在它上面,它会改变内容。我已经介绍过了。但是,我不知道如何获得我想要的结果。我想让它动画化。当有人将鼠标悬停在按钮上时,新文本应该从右向左飞入,并且应该推出当前文本。此外,我想在右侧(按钮内部)有一个小 sisser 图标,当悬停 sisser 图标时,它也会改变背景颜色。

这可能吗?如果可以,怎么做?我还没有在 StackOverflow 或 codepen 上找到接近我想要的解决方案。这是我目前所拥有的:

.product-voucherButton span {
    display: none;
}

.product-voucherButton:hover span {
    display: block;
}

.product-voucherButton:before {
    content: 'Voucher';
}

.product-voucherButton:hover:before {
    content: '';
}

.btn-secondary {
    background-color: #312C2B !important;
    color: white !important;
    border: none !important;
}

.btn {
    width: 150px;
    display: inline-block;
    font-weight: 400;
    color: #212529;
    text-align: center;
    vertical-align: middle;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-color: transparent;
    border: 1px solid transparent;
    padding: .375rem .75rem;
    font-size: .9rem;
    line-height: 1.6;
    border-radius: .25rem;
    -webkit-transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
    transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
<a class="btn btn-secondary product-voucherButton"><span>this is my code</span></a>

【问题讨论】:

    标签: html css css-animations css-transitions


    【解决方案1】:

    您可以使用::before 和::after 伪元素以及attr() 函数来做到这一点:

    #btn {
      width: 200px;
      height: 30px;
      display: flex;
      justify-content: center;
      position: relative;
      overflow: hidden;
    }
    
    #btn::before,
    #btn::after {
      display: block;
      transition: all .6s ease-in;
      position: absolute;
      line-height: 30px;
      top: 0;
      white-space: pre;
    }
    
    i.fa.fa-cut {
      transition: all .6s ease-in;
      width: 400px;
    }
    
    #btn::before {
      content: attr(data-orig);
    }
    
    #btn::after {
      content: attr(data-repl);
      margin-left: 300px;
    }
    
    #btn:hover::before {
      margin-left: -300px;
    }
    
    #btn:hover::after {
      margin-left: 0px;
    }
    
    i.fa.fa-cut {
      margin-left: 363px;
      line-height: 30px;
    }
    
    #btn:hover i.fa.fa-cut {
      margin-left: 163px;
    }
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
    
    <button id="btn" data-repl="Replacement Text" data-orig="Original Text">
    
    <i class="fa fa-cut"></i>
    </button>

    更新:

    您可以使用::before 和::after 伪元素以及attr() 函数来做到这一点:

    #btn {
      width: 200px;
      height: 30px;
      display: flex;
      justify-content: center;
      position: relative;
      overflow: hidden;
    }
    
    #btn > span,
    #btn::before {
      display: block;
      transition: all .6s ease-in;
      position: absolute;
      line-height: 30px;
      top: 0;
      white-space: pre;
    }
    
    i.fa.fa-cut {
      transition: all .6s ease-in;
    }
    
    #btn::before {
      content: attr(data-orig);
    }
    
    #btn > span {
      content: attr(data-repl);
      margin-left: 400px;
    }
    
    #btn:hover::before {
      margin-left: -300px;
    }
    
    #btn:hover > span {
      margin-left: 0px;
    }
    
    i.fa.fa-cut {
      line-height: 30px;
    }
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
    
    <button id="btn" data-orig="Original Text">
    
    <span>Replacement Text <i class="fa fa-cut"></i></span>
    </button>

    【讨论】:

    • 哦,谢谢!非常好,接近我想要的。替换文本是否也在您的屏幕上跳跃?当替换文本出现时,您对如何在按钮右侧 1/4 处添加 sissers 图标有任何想法吗? (在最好的情况下,sissers 图标和替换文本一样从左到右飞入)
    • @Jan 我没有看到跳跃。我加了剪刀。过渡与文本幻灯片不完全对齐,因此您可能需要稍微调整一下。
    • 好的,谢谢!我会做。但是,滑入时文本正在跳跃……可悲的是
    • @jan 怎么跳?
    • 在这里,看看这个。第一个浏览器是chrome,第二个是firefox:imgur.com/ZHTzV5M
    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 2012-05-27
    • 2018-07-22
    • 2015-07-04
    • 2012-07-24
    • 2022-11-12
    相关资源
    最近更新 更多