从 btn-primary 淡入颜色按钮到 btn-success

Fade in color button from btn-primary to btn-success

我希望 btn-primary 颜色按钮以 btn-success 的颜色淡入淡出,其中 data-id = 单击的 ID。

我不知道如何用点击的 ID 制作动画。尝试使用 $(".add_to_cart").parent('.change-style-after-click[data-id="'+id+'"]').animate({backgroundColor: '#28a745'}, 'slow');

但这没有用。

//HTML

<div class="change-style-after-click" data-id="30" bis_skin_checked="1"><a class="btn btn-primary w-100-pct add_to_cart" data-id="30">Add to Cart</a></div>

//Jquery

$(".add_to_cart").click(function(e){
var id = $(this).data('id');
$('.change-style-after-click[data-id="'+id+'"]').html("<a href='cart.php' class='btn btn-success w-100-pct'>Update Quantity</a>");
});

有点矫枉过正,但您可以使用 @keyframes 来实现此目的,只需更新按钮 class 和内部文本:

$(".add_to_cart").click(function(e) {
  var id = $(this).data('id');
  var button = $('.change-style-after-click[data-id="' + id + '"] .btn')
  button.addClass("btn-success").text("Update Quantity");
});
.btn.btn-success {
  transition-duration: 2s;
  animation-name: colorTransition;
}

@keyframes colorTransition {
  from {
    background-color: #007bff !important;
  }
  to {
    background-color: #28a745 !important;
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

<div class="change-style-after-click" data-id="30" bis_skin_checked="1">
  <a class="btn btn-primary w-100-pct add_to_cart" data-id="30">Add to Cart</a>
</div>