选择文字后,我想在底部显示一个工具提示,上面写着"已复制!"并且工具提示必须位于选择的中心。虽然我不确定如何在选择后显示工具提示。这就是我到目前为止所做的......
HTML
<p>
<span class="selectable" tooltip="Copied!" tooltip-position='bottom'>
Some lines of code
<span>
</p>
CSS(用于工具提示)
[tooltip]{
position:relative;
display:inline-block;
}
[tooltip]::before {
content: "";
position: absolute;
top:-6px;
left:50%;
transform: translateX(-50%);
border-width: 4px 6px 0 6px;
border-style: solid;
border-color: rgba(0,0,0,0.9) transparent transparent;
z-index: 99;
opacity:0;
}
[tooltip-position='bottom']::before{
top:100%;
margin-top:8px;
transform: translateX(-50%) translatey(-100%) rotate(-180deg)
}
[tooltip]::after {
content: attr(tooltip);
position: absolute;
left:50%;
top:-6px;
transform: translateX(-50%) translateY(-100%);
background: rgba(0,0,0,0.9);
text-align: center;
color: #fff;
padding:4px 2px;
font-size: 12px;
min-width: 80px;
border-radius: 5px;
pointer-events: none;
padding: 4px 4px;
z-index:99;
opacity:0;
}
[tooltip-position='bottom']::after{
top:100%;
margin-top:8px;
transform: translateX(-50%) translateY(0%);
}
[tooltip]:active::after,[tooltip]:active::before {
opacity:1
}
任何帮助都会受到赞赏。此外,首选香草javascript。
答案 0 :(得分:0)
您可以使用简单的解决方案
function getTooltip(e) {
e.stopPropagation();
var tar = e.target.getBoundingClientRect();
$('.tooltip').css({
top: tar.bottom,
left: tar.y
}).show();
}
$(document).on("click", function(e) {
e.stopPropagation();
$('.tooltip').hide();
});
&#13;
.tooltip {
position: fixed;
padding: 5px;
border: 1px solid #474747;
display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
<span class="selectable" onClick="getTooltip(event)">
Some lines of code
<span>
</p>
<div class="tooltip">
Copied!!!
</div>
&#13;
希望这会对你有所帮助。
答案 1 :(得分:0)
这样的事情应该有效。
var timeout;
document.querySelector(".selectable").addEventListener("mouseup", function(e) {
var selection = document.getSelection();
if (!selection.toString().trim().length)
return;
clearTimeout(timeout);
document.execCommand('copy');
var rect = selection.getRangeAt(0).getBoundingClientRect();
$(this).tooltip("show");
var tooltipLeft = Math.max(rect.left - ($('.tooltip').width() - rect.width), 0);
$('.tooltip').css({
left: tooltipLeft
});
var selectable = this;
timeout = setTimeout(function() {
$(selectable).tooltip("hide");
}, 1000);
});
$('.selectable').tooltip({
trigger: 'manual',
placement: 'bottom'
});
<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" />
<p>
<span class="selectable" title="Copied!">Some lines of code</span>
</p>