<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Copier du texte</title>
<style>
.copiedtext {
position: absolute;
left: 0; top: 0; right: 0;
text-align: center;
opacity: 0;
transform: translateY(-1em);
color: #000;
transition: all .500s;
}
.copied .copiedtext {
opacity: 1;
transform: translateY(-2em);
}
body {
text-align: center;
font-family: "Open Sans", Helvetica, Arial, sans-serif;
color: #444;
line-height: 1.6;
}
h1 {
margin: 1.75em auto 1.25em;
}
textarea, button {
font-size: 1em;
font-family: "Open Sans", Helvetica, Arial, sans-serif;
}
textarea {
display: block;
width: 300px;
max-width: 100%;
height: 75px;
margin: 2em auto 1.5em;
background: #F2F2F6;
border: 1px solid #ddd;
padding: 10px 15px;
resize: vertical;
}
[id="cleared"] {
margin-top: 4em;
}
textarea:focus {
border-color: #8fa423;
}
button {
position: relative;
padding: 8px 20px;
border: 0;
font-size: 0.835em;
text-transform: uppercase;
letter-spacing: 0.125em;
font-weight: bold;
color: #FFF;
background: #8fa423;
transition: background .275s;
}
button:hover,
button:focus {
background: #74861A;
}
p {
margin-top: 3.25em;
font-size: .825em;
color: #777;
font-weight: bold;
letter-spacing: .01em
}
</style>
</head>
<body>
<h1>Copier un texte</h1>
<textarea id="to-copy" spellcheck="false">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>
<button id="copy" type="button">Copiez dans le press-papier<span class="copiedtext"aria-hidden="true">Copié</span></button>
<textarea id="cleared" placeholder="Coller votre contenu copié ici ..."></textarea>
</body>
</html>
Le code Javascript
var toCopy = document.getElementById('to-copy'),
btnCopy = document.getElementById('copy'),
paste = document.getElementById('cleared');
btnCopy.addEventListener( 'click', function(){
toCopy.select();
paste.value = '';
if ( document.execCommand('copy') ) {
btnCopy.classList.add('copied');
paste.focus();
var temp = setInterval( function(){
btnCopy.classList.remove('copied');
clearInterval(temp);
}, 600 );
} else {
console.info('document.execCommand went wrong…')
}
return false;
} );
4 janv. 2017 à 19:24
4 janv. 2017 à 19:54
Visiblement non....