简体   繁体   English

使用 animation 更改按钮图标

[英]Change button icon with animation

I am using bootstrap icons and javascript and I would like to know how can I change the icon in an animated way?我正在使用引导图标和 javascript,我想知道如何以动画方式更改图标?

toClipboard: function (refElementName, sender) {
    let btn = $(sender);
    let btnIcon = btn.find("i")

    $("[name='" + refElementName + "']").select()
    document.execCommand("copy")
    
    btn.fadeOut(400)
    btnIcon.attr("class", "bi bi-clipboard-check")
    btn.delay(400)
    btnIcon.attr("class", "bi bi-clipboard")

}

html html

<div class="col position-relative">
                                        <textarea name="textObs" rows="5" class="form-control" placeholder="Observações"></textarea>
                                        <a class="btn btn-primary" id="copy" onclick="toClipboard('textObs', this)"><i class="bi bi-clipboard"></i></a>
                                    </div>

I tried this, but it doesn't work as expected... the animation happens but in the end the button is without icon我试过这个,但它没有按预期工作...... animation 发生但最后按钮没有图标

https://jsfiddle.net/r06s5ctn/ https://jsfiddle.net/r06s5ctn/

Is this what you're looking for?这是你要找的吗?

On click, fade out icon, then set a timeout to change the icon class once it's faded out, then fade the icon back in.单击时,淡出图标,然后设置超时以更改图标 class 一旦它淡出,然后将图标淡入。

 function toClipboard(refElementName, sender) { let btn = $(sender); let btnIcon = btn.find("i") $("[name='" + refElementName + "']").select() document.execCommand("copy") btn.fadeOut(400) setTimeout(function() { btnIcon.removeClass("bi-clipboard").addClass("bi-clipboard-check") }, 400) btn.fadeIn(400) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.4.1/font/bootstrap-icons.min.css"> <div class="col position-relative"> <textarea name="textObs" rows="5" class="form-control" placeholder="Observações"></textarea> <a class="btn btn-primary" id="copy" onclick="toClipboard('textObs', this)"><i class="bi bi-clipboard"></i></a> </div>

Alloha !阿罗哈!

To change de icon of the button, you just need to use.html from jquery like this:要更改按钮的图标,您只需要使用.html from jquery,如下所示:

btn.fadeOut(400)
btnIcon.html("<i class="bi bi-clipboard-check"></i>")
btn.delay(400)
btnIcon.attr("<i class="bi bi-clipboard"></i>")

Have a good day!祝你有美好的一天!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM