JS生成二維碼以及點擊下載二維碼


JS生成二維碼

  • 效果
  • 在這里插入圖片描述
  • 掃描二維碼可訪問指定鏈接或當前網站鏈接,內容可以自己更改
  • "復制鏈接",點擊按鈕復制當前網站鏈接
  • 如何獲取當前網站鏈接

源碼

textarea{
opacity: 0;
	/*display: none;*/
	z-index: -999;
}
button{
	margin-left: -90px;
	border: 1.5px solid #000;
	padding: 3px 8px;
	font-size: 24px;
}
<div class="shareBox">
	<span id="qrcode" onclick="download()"></span>
	<div id="qrcodeWord" onclick="copyHref()" onclick="copyHref()">
	   <textarea id="inputTextarea"></textarea>
	   <button>復制鏈接</button>
	</div>
</div>
//生成二維碼
qrcode = new QRCode(document.getElementById("qrcode"), {
	text: document.location.href,//鏈接地址
	width : 300,//二維碼寬高
	height : 300,
	colorDark : "#000000",//二維碼前景色
    colorLight : "#fff",//二維碼背景色
    correctLevel : QRCode.CorrectLevel.H
});
  • 生成當前網站鏈接的二維碼
    • text: document.location.href
    • document.location.href就是獲取當前網站的鏈接
  • 當然也可以自己更改二維碼
//點擊二維碼下載圖片
function download(){
	var data = $("canvas")[0].toDataURL().replace("image/png", "image/octet-stream;");//獲取二維碼值,並修改響應頭部。   
	var filename='blog-'+document.title+'.png';//保存的圖片名稱和格式,canvas默認使用的png格式。這個格式效果最好。
	var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
	save_link.href = data;
	save_link.download = filename;
	var event = document.createEvent('MouseEvents');
	event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	save_link.dispatchEvent(event);
}
//復制當前網站鏈接
 function copyHref(){ 
   var input = document.getElementById("inputTextarea");
   input.value = document.location.href; // 修改文本框的內容
   input.select(); // 選中文本
   document.execCommand("copy"); // 執行瀏覽器復制命令
   alert("復制成功\n(點擊下載二維碼或使用手機瀏覽器設置成桌面版顯示可以轉發二維碼)");
}
<script src="jquery.min.js"></script>
<script src="qrcode.min.js"></script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM