CSS實現(xiàn)打印效果。
柴柴老師。哈嘍小伙伴們,我是柴柴老師。今天我們來實現(xiàn)一個純CS5的打印效果。我們先過來演示一下。當我們刷新的時候,大家注意觀察這個位置的字是一個字,一個字蹦出來對不對?就仿佛是一個打印的一個效果。
這個效果如果用純CS去做該怎么來做?廢話不多說,我們直接過來講一下它的一個原理。
這原理蠻簡單。把文字按照字數(shù),拆分成對應的步數(shù),然后對應修改盒子的寬度就可以了。
什么意思?比方說我們下面這個地方的線里邊有24個字符,就可以把它拆分成24步走完。每次繞這個盒子的寬度,增加一個中文字符的寬度就可以了。這是它的一個簡單的原理,就按照這個原理給它做一個實現(xiàn)。
先過來使用關鍵幀,keyframe創(chuàng)建一個關鍵幀。這里邊我們想做什么事?想讓它的寬度從口然后變化到我們的24個字符。這個地方我們來一個width,到哪里?to就是跟我們這個地方的字符的整個寬度是一樣的。
這個關鍵幀創(chuàng)建好以后,咱們過來給這個盒子進行一個綁定。綁定時候用一個animation,另一部分把它level綁過來,可以給一個7秒鐘。當然這個可以按照我們自己的時間去做把控。后面來到最關鍵的一步,怎么讓這個動畫完成?一步一步的走過去,而不是一個勻速而是一步一步的。這個怎么來做?其實這個地方有一個叫做分步數(shù)去做,一共是24個字符,我們可以把它分成24步,所以就是24。
寫過來以后end結束。這樣這個小的打印效果應該就已經(jīng)實現(xiàn)了。給力看一下,看o不OK!
用CSS實現(xiàn)打印效果,沒有什么卵用但有趣的知識又增加了。
大家看現(xiàn)在,今天星期四明天星期五,再上一天班,然后是星期天,是不是就實現(xiàn)這個效果呀?沒有什么卵用,但是有趣的東西又增加了!
、選擇打印
把要打印的內容放入一個 span或div,然后通過一個函數(shù)打印。
<script language=
"javascript"
>
function
printme()
{ document.body.innerHTML=document.getElementByIdx_x_x(
'div1'
).innerHTML+
'<br/>'
+document.getElementByIdx_x_x(
'div2'
).innerHTML;
window.print();
}
</script>
<span id=
'div1'
>把要打印的內容放這里</span>
<p>所有內容</p>
<div id=
"div2"
>div2的內容</div>
<a href=
"javascript:printme()"
rel=
"external nofollow"
target=
"_self"
>打印</a>
務場景常見于一些訂單記錄發(fā)票等的一些pdf 打印,保留了樣式,可以打印彩色內容也可以打印黑白內容,如果業(yè)務復雜的比如添加水印的目前不支持,一般會特定處理,通過canvas 的方式處理。
print.js
// 打印類屬性、方法定義
/* eslint-disable */
const Print = function(dom, options) {
if (!(this instanceof Print)) return new Print(dom, options);
this.options = this.extend(
{
noPrint: ".no-print"
},
options
);
if (dom instanceof String) {
this.dom = document.querySelector(dom);
} else {
this.isDOM(dom);
this.dom = this.isDOM(dom) ? dom : dom.$el;
}
this.init();
};
Print.prototype = {
init: function() {
var content = this.getStyle() + this.getHtml();
this.writeIframe(content);
},
extend: function(obj, obj2) {
for (var k in obj2) {
obj[k] = obj2[k];
}
return obj;
},
getStyle: function() {
var str = "",
styles = document.querySelectorAll("style,link");
for (var i = 0; i < styles.length; i++) {
str += styles[i].outerHTML;
}
str +=
"<style>" +
(this.options.noPrint ? this.options.noPrint : ".no-print") +
"{display:none;}</style>";
return str;
},
getHtml: function() {
var inputs = document.querySelectorAll("input");
var textareas = document.querySelectorAll("textarea");
var selects = document.querySelectorAll("select");
for (var k = 0; k < inputs.length; k++) {
if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
if (inputs[k].checked == true) {
inputs[k].setAttribute("checked", "checked");
} else {
inputs[k].removeAttribute("checked");
}
} else if (inputs[k].type == "text") {
inputs[k].setAttribute("value", inputs[k].value);
} else {
inputs[k].setAttribute("value", inputs[k].value);
}
}
for (var k2 = 0; k2 < textareas.length; k2++) {
if (textareas[k2].type == "textarea") {
textareas[k2].innerHTML = textareas[k2].value;
}
}
for (var k3 = 0; k3 < selects.length; k3++) {
if (selects[k3].type == "select-one") {
var child = selects[k3].children;
for (var i in child) {
if (child[i].tagName == "OPTION") {
if (child[i].selected == true) {
child[i].setAttribute("selected", "selected");
} else {
child[i].removeAttribute("selected");
}
}
}
}
}
return this.dom.outerHTML;
},
writeIframe: function(content) {
var w,
doc,
iframe = document.createElement("iframe"),
f = document.body.appendChild(iframe);
iframe.id = "myIframe";
//iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
iframe.setAttribute(
"style",
"position:absolute;width:0;height:0;top:-10px;left:-10px;"
);
w = f.contentWindow || f.contentDocument;
doc = f.contentDocument || f.contentWindow.document;
doc.open();
doc.write(content);
doc.close();
var _this = this;
iframe.onload = function() {
_this.toPrint(w);
setTimeout(function() {
document.body.removeChild(iframe);
}, 100);
};
},
toPrint: function(frameWindow) {
try {
setTimeout(function() {
frameWindow.focus();
try {
if (!frameWindow.document.execCommand("print", false, null)) {
frameWindow.print();
}
} catch (e) {
frameWindow.print();
}
frameWindow.close();
}, 10);
} catch (err) {
console.log("err", err);
}
},
isDOM:
HTMLElement instanceof Object
? function(obj) {
return obj instanceof HTMLElement;
}
: function(obj) {
return (
obj &&
obj instanceof Object &&
obj.nodeType === 1 &&
obj.nodeName instanceof String
);
}
};
const MyPlugin = {};
MyPlugin.install = function(Vue, options) {
// 4. 添加實例方法
Vue.prototype.$print = Print;
};
export default MyPlugin;
print.js
在vue項目main.js 中加入
import Print from "./print";
Vue.use(Print);
說明: 在需要打印的地方添加ref,不需要打印的地方添加 no-print 的class 類名,通過 $print 去調用需要打印的ref(或者用定義id然后調用id節(jié)點的方式也可以,vue中推薦用ref)
說明: 在需要打印的地方添加ref,通過 $print 去調用需要打印的ref<template>
<div class="wrapper">
<button @click="$print($refs.print)">打印</button>
<button @click="handleClick">JS調用打印</button>
<div ref="print">
<h1>這是打印區(qū)域</h1>
<small style="color:red">字體顏色備注</small>
<p class="no-print">這是不用打印區(qū)域</p>
</div>
</div>
</template>
<script>
export default {
name: 'demo',
methods: {
// JS 操作調用
handleClick() {
this.$print(this.$refs.print)
}
}
}
</script>
<style lang="scss" scoped>
.wrapper {
padding: 2rem;
background: #ffffff;
margin: 1rem;
}
</style>
打印前
打印后
這兩個文檔都很完善,我簡單介紹下安裝使用
Print.js
地址: https://printjs.crabbly.com/
// 下載
npm install print-js --save
// 用的地方導入
import printJS from 'print-js'
<el-button @click="handleClickPrint">打印</el-button>
<div id="print">
<el-table :data="tableData"
style="width: 100%">
<el-table-column prop="date"
label="日期"
width="180">
</el-table-column>
<el-table-column prop="name"
label="姓名"
width="180">
</el-table-column>
<el-table-column prop="address"
label="地址">
</el-table-column>
</el-table>
</div>
methods: {
handleClickPrint() {
## 設置樣式
const style = '@page { margin: 0 } @media print { }'// 自定義樣式
## 調用方法
printJS({
printable: 'form', // 要打印內容的id
type: 'html', // 打印類型 pdf, html, image, json and raw-html
style: style,
scanStyles: false
})
},
// 圖片打印
handleClickPrintImg() {
printJS('http://img.zdnet.com.cn/4/90/liRMFbh31Ka2.jpg?rand=153', 'image')
}
}
vue-print-nb
地址: https://github.com/Power-kxLee/vue-print-nb
npm install vue-print-nb --save
main.js 中導入注冊
import Print from 'vue-print-nb'
Vue.use(Print);
<el-button type="primary" v-print="'#printDetail'">打印</el-button>
<div id="printDetail">
.... 要打印的內容
</div>
其他細節(jié)可以看官方文檔,每一個技術官方文檔最詳細的,最起碼都應該看一遍的。
前端開發(fā)——工具篇
*請認真填寫需求信息,我們會在24小時內與您取得聯(lián)系。