点击

时间:2018-05-17 01:06:38

标签: javascript jquery css

CSS我有:focus animation我只能在点击并按住框时才能看到。如何在单击时添加.active类时运行它?



$(document).ready(function() {
  $(".copy-btn").click(function(event) {
    event.preventDefault();
  });
});
$('.close1, .close2').on('click', function() {
  $('.copy-btn').removeClass('active');
})
var clip = new Clipboard(".copy-btn");
$(".copy-btn").click(function(e) {
  $.each($(".copy-btn"), function(index, value) {
    if (
      $(value).attr("data-clipboard-text") ==
      $(e.target).attr("data-clipboard-text")
    ) {
      $(value).addClass("active");
    }
  });
});

.btn {
  width: 50px;
  height: 20px;
  cursor: pointer;
}

.copy-btn {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  max-height: 125px;
  max-width: 75%;
  margin-right: auto;
  margin-left: auto;
  padding: 15px 5px;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  border: 1px dashed silver;
  color: #54c48a;
  cursor: pointer;
}

.active {
  border: 3px solid;
  color: silver;
}

@keyframes copy-btn-animation {
  0% {
    transform: translateY(0rem);
    opacity: 1;
  }
  99% {
    transform: translateY(-1.5rem);
    opacity: 0;
  }
  100% {
    transform: translateY(0rem);
  }
}

.copy-btn:before {
  position: absolute;
  content: "Copied!";
  opacity: 0;
}

.copy-btn:focus:before {
  animation-name: copy-btn-animation;
  animation-duration: 1s;
  animation-timing-function: ease;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.12/clipboard.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="copy-btn" data-clipboard-text="CODE1">CODE1</a>
<a href="#" class="copy-btn" data-clipboard-text="CODE2">CODE2</a>
<div class="btn close1">reset</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

最简单的方法是将动画设为一个类,并在添加活动类后添加该类。

&#13;
&#13;
$(document).ready(function() {
  $(".copy-btn").click(function(event) {
    event.preventDefault();
  });
});
$('.close1, .close2').on('click', function() {
  $('.copy-btn').removeClass('active');
  $('.copy-btn').removeClass('active-copy-btn');
})
var clip = new Clipboard(".copy-btn");
$(".copy-btn").click(function(e) {
  $.each($(".copy-btn"), function(index, value) {
    if (
      $(value).attr("data-clipboard-text") ==
      $(e.target).attr("data-clipboard-text")
    ) {
      $(value).addClass("active");
      $(value).addClass("active-copy-btn");
    }
  });
});
&#13;
.btn {
  width: 50px;
  height: 20px;
  cursor: pointer;
}

.copy-btn {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  max-height: 125px;
  max-width: 75%;
  margin-right: auto;
  margin-left: auto;
  padding: 15px 5px;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  border: 1px dashed silver;
  color: #54c48a;
  cursor: pointer;
}

.active {
  border: 3px solid;
  color: silver;
}

@keyframes copy-btn-animation {
  0% {
    transform: translateY(0rem);
    opacity: 1;
  }
  99% {
    transform: translateY(-1.5rem);
    opacity: 0;
  }
  100% {
    transform: translateY(0rem);
  }
}

.copy-btn:before {
  position: absolute;
  content: "Copied!";
  opacity: 0;
}

.active-copy-btn {
  animation-name: copy-btn-animation;
  animation-duration: 1s;
  animation-timing-function: ease;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.12/clipboard.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="copy-btn" data-clipboard-text="CODE1">CODE1</a>
<a href="#" class="copy-btn" data-clipboard-text="CODE2">CODE2</a>
<div class="btn close1">reset</div>
&#13;
&#13;
&#13;