Tooltips + Highlight Animation With Clipboard.js Click

前端 未结 2 1478
清歌不尽
清歌不尽 2020-12-13 13:57

I\'ve successfully installed clipboard.js and have gotten snippets of text to copy to the clipboard upon click. I\'m going to be nesting these snippets of text (or the \"btn

相关标签:
2条回答
  • 2020-12-13 14:46

    Seems like all you want to do is integrating Clipboard.js with a Tooltip solution.

    So here's how you can accomplish that using Bootstrap's Tooltip.

    // Tooltip
    
    $('button').tooltip({
      trigger: 'click',
      placement: 'bottom'
    });
    
    function setTooltip(btn, message) {
      $(btn).tooltip('hide')
        .attr('data-original-title', message)
        .tooltip('show');
    }
    
    function hideTooltip(btn) {
      setTimeout(function() {
        $(btn).tooltip('hide');
      }, 1000);
    }
    
    // Clipboard
    
    var clipboard = new Clipboard('button');
    
    clipboard.on('success', function(e) {
      setTooltip(e.trigger, 'Copied!');
      hideTooltip(e.trigger);
    });
    
    clipboard.on('error', function(e) {
      setTooltip(e.trigger, 'Failed!');
      hideTooltip(e.trigger);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
    
    <button class="btn btn-primary" data-clipboard-text="It worked!">Click me</button>
    <button class="btn btn-primary" data-clipboard-text="It worked again!">Click me</button>

    0 讨论(0)
  • 2020-12-13 14:48

    I've come up with a slight improvement to Zeno's code, which wraps it in a jQuery function, and supports copying from an arbitrary element:

    if (typeof $.uf === 'undefined') {
        $.uf = {};
    }
    
    $.uf.copy = function (button) {
        var _this = this;
    
        var clipboard = new ClipboardJS(button, {
            text: function(trigger) {
                var el = $(trigger).closest('.js-copy-container').find('.js-copy-target');
                if (el.is(':input')) {
                    return el.val();
                } else {
                    return el.html();
                }
            }
        });
    
        clipboard.on('success', function(e) {
            setTooltip(e.trigger, 'Copied!');
            hideTooltip(e.trigger);
        });
    
        clipboard.on('error', function(e) {
            setTooltip(e.trigger, 'Failed!');
            hideTooltip(e.trigger);
        });
    
        function setTooltip(btn, message) {
            $(btn)
            .attr('data-original-title', message)
            .tooltip('show');
        }
        
        function hideTooltip(btn) {
            setTimeout(function() {
                $(btn).tooltip('hide')
                .attr('data-original-title', "");
            }, 1000);
        }
    
        // Tooltip
        $(button).tooltip({
            trigger: 'click'
        });
    };
    
    // Link all copy buttons
    $.uf.copy('.js-copy-trigger');
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    
    <form>
     <div class="form-group">
      <label>Email</label>
      <div class="input-group js-copy-container">
        <input type="text" class="form-control js-copy-target" name="email" autocomplete="off" value="example@example.com" placeholder="Email goes here" disabled>
        <span class="input-group-btn">
          <button class="btn btn-default js-copy-trigger" type="button">Copy</button>
        </span>
      </div>
     </div>
    </form>

    You'll notice that we have our button with a class of js-copy-trigger, and the text/control to be copied with the js-copy-target class. Both of these are wrapped in a common js-copy-container class.

    This is much better than using id targets, because you often need to generate multiple copy buttons (for example, in a table), and ids must be unique on a page.

    0 讨论(0)
提交回复
热议问题