通過按鈕截取當前網頁成png或jpeg格式的圖片並保存
阿新 • • 發佈:2017-12-08
jquer data 情況 ram ext class url head att
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"/>
<script type="text/javascript" src="html2canvas.js"/>
</head>
<body>
<a id="down_load_hidden" target="blank" style="display: none;"></a>
<a id="down_button" href="javascript:void(0)" onclick="domShot(‘qqCont‘)">保存當前頁</a>
<div id="qqCont" class="qqCont">
需要截圖的樣式
</div>
<script>
/**
* 調用html2canvas框架進行截圖下載功能
* @param domId 容器Id
* author Levin
*/
function domShot(domId) {
//0.5.0-beta4方法
html2canvas(document.querySelector("#" + domId), {
allowTaint: true,
height: $("#" + domId).outerHeight() + 20
}).then(function (canvas) {
var timestamp = Date.parse(new Date());
$(‘#down_button‘).attr(‘href‘, canvas.toDataURL());
$(‘#down_button‘).attr(‘download‘, timestamp + ‘.png‘);
var fileObj = document.getElementById("down_load_hidden");
fileObj.click();
});
}
</script>
//一般情況html2canvas.js中的截取部分不支持取全部截取 所以要將頁面所有部分截取得 修改html2canvas.js中的一下部分
//未修改前
<script>
return renderDocument(node.ownerDocument, options, node.ownerDocument.defaultView.innerWidth, node.ownerDocument.defaultView.innerHeight, index).then(function(canvas) {
if (typeof(options.onrendered) === "function") {
log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
options.onrendered(canvas);
}
return canvas;
});
</script>
//修改後
<script>
//添加自定設置寬度高度
var width = options.width != null ? options.width : node.ownerDocument.defaultView.innerWidth;
var height = options.height != null ? options.height : node.ownerDocument.defaultView.innerHeight;
return renderDocument(node.ownerDocument, options, width, height, index).then(function (canvas) {
* *
if (typeof(options.onrendered) === "function") {
log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
options.onrendered(canvas);
}
return canvas;
});
</script>
</body>
</html>
通過按鈕截取當前網頁成png或jpeg格式的圖片並保存