简体   繁体   English

Clipboard.JS 如何使用“成功”事件将新字符串置于前面?

[英]Clipboard.JS How to make the new string to front with 'success' event?

I'm using the 'success' event to change the button text after someone have clicked it.我正在使用“成功”事件在有人单击它后更改按钮文本。

The original button text is in span and it will be covered by the animation, so I use CSS rule: z-index and color to make the text visible and to the front.原始按钮文本在span中,它将被 animation 覆盖,所以我使用 CSS 规则: z-indexcolor使文本可见并显示在前面。

The problem is the text will be hidden under the animation after the function is invoked because the new string is not in span anymore.问题是在调用 function 之后,文本将隐藏在 animation 下,因为新字符串不再处于span中。

How do I make the new string in span so the CSS will still work to make it front?如何在span中制作新字符串,以便 CSS 仍然可以使其位于前面?

Below is my code:下面是我的代码:

 var clipboard = new ClipboardJS('.copyElement') clipboard.on('success', function(e) { let oldtext = e.trigger.textContent e.trigger.textContent = 'Copied.' setTimeout(() => e.trigger,textContent = oldtext; 2000) });
 body { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 330px; }.copyElement { cursor: pointer; }.Big { width: 257px; padding: 33px 0 30px 0; font-size: 21px; }.Medium { width: 210px; padding: 27px 0 24px 0; font-size: 18px; margin: 30px 0; }.Small { width: 150px; padding: 21px 0 18px 0; font-size: 15px; }.ButtonMain { background-color: #e6e6e6; position: relative; overflow: hidden; border: none; }.ButtonMain::before, .ButtonMain::after { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }.ButtonMain span { position: relative; color: #fff; }.ButtonMain:hover span { color: #000; transition: color 0.3s ease 0s; }.RedRevealEffect::before { content: ''; background: #ef476f; width: 120%; left: -10%; transform: skew(30deg); transition: transform 0.4s cubic-bezier(0.3, 1, 0.8, 1); }.RedRevealEffect:hover::before { transform: translate3d(100%,0,0); }
 <button class="copyElement ButtonMain RedRevealEffect Small" data-clipboard-text="123" > <span>Take Me There</span> </button> <button class="copyElement ButtonMain RedRevealEffect Medium" data-clipboard-text="456" > <span>Take Me There</span> </button> <button class="copyElement ButtonMain RedRevealEffect Big" data-clipboard-text="789" > <span>Take Me There</span> </button> <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>

Because the button's text lies inside the span .因为按钮的文本位于span Use querySelector('span') to change the span's text使用querySelector('span')更改跨度的文本

 var clipboard = new ClipboardJS('.copyElement') clipboard.on('success', function(e) { let span = e.trigger.querySelector('span') let oldtext = span.textContent span.textContent = 'Copied.' setTimeout(() => span,textContent = oldtext; 2000) });
 body { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 330px; }.copyElement { cursor: pointer; }.Big { width: 257px; padding: 33px 0 30px 0; font-size: 21px; }.Medium { width: 210px; padding: 27px 0 24px 0; margin: 30px 0; }.Small { width: 150px; padding: 21px 0 18px 0; font-size: 15px; }.ButtonMain { background-color: #e6e6e6; position: relative; overflow: hidden; border: none; }.ButtonMain::before, .ButtonMain::after { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }.ButtonMain span { position: relative; color: #fff; }.ButtonMain:hover span { color: #000; transition: color 0.3s ease 0s; }.RedRevealEffect::before { content: ''; background: #ef476f; width: 120%; left: -10%; transform: skew(30deg); transition: transform 0.4s cubic-bezier(0.3, 1, 0.8, 1); }.RedRevealEffect:hover::before { transform: translate3d(100%,0,0); }
 <button class="copyElement ButtonMain RedRevealEffect Small" data-clipboard-text="123" > <span>Take Me There</span> </button> <button class="copyElement ButtonMain RedRevealEffect Medium" data-clipboard-text="456" > <span>Take Me There</span> </button> <button class="copyElement ButtonMain RedRevealEffect Big" data-clipboard-text="789" > <span>Take Me There</span> </button> <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>

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

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