利用docx-preview在线阅览word

html 复制代码
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        article > .docx_toc.docx-num-35-undefined {
            margin-bottom: 1em;
        }

        .docx_toc1 a, .docx_toc2 a {
            display: inline-block;
            width: 100%;
            overflow: hidden;
            color: #333;
            position: relative;
        }

        .docx_toc1 a > span, .docx_toc2 a > span {
            color: #333;
            text-decoration: none;
        }

        .docx_toc1 a > span:last-child, .docx_toc2 a > span:last-child {
            float: right;
            color: #333;
            background-color: #fff;
            text-indent: 1rem;
            z-index: 10;
            position: relative;
        }

        .docx_toc1 a > span:nth-last-child(2), .docx_toc2 a > span:nth-last-child(2) {
            position: absolute;
            display: inline-block;
            top: 50%;
            width: 95%;
            height: 0 !important;
            min-height: 1px;
            vertical-align: middle;
            border-bottom-style: dotted;
        }

        .docx_toc1 span.docx_afa {
            text-decoration: none;
        }

        .docx_toc1 a:hover span.docx_afa {
            text-decoration: underline;
            color: #000;
        }

        .docx_toc1 a:hover span.docx_afa:first-child {
            text-decoration: underline;
            color: #000;
        }

        .docx_toc1 a:hover span:last-child {
            text-decoration: underline;
            color: #000;
        }


        .docx_toc1 a:hover span > span {
            text-decoration: none !important;
        }

        .docx_toc2 a:hover span:last-child {
            text-decoration: underline;
            color: #000;
        }

        .docx_toc2 a:hover span.docx_afa {
            text-decoration: underline;
            color: #000;
        }

        .docx_toc2 a:hover span.docx_afa:first-child {
            text-decoration: underline;
            color: #000;
        }

        .docx_toc2 a:hover span > span {
            text-decoration: none !important;
        }
        article img{
            cursor: pointer;
            transition: .2s ease-in-out;
        }

        article img:hover {
            transform: scale(1.1);
            box-shadow: rgba(0, 0, 0, 0.1) 0 0 27px 10px;
            z-index: 10;
        }
    </style>
</head>

<!--lib uses jszip-->
<script src="https://unpkg.com/jszip/dist/jszip.min.js"></script>
<!-- docx-preview 可以npm install 下载,也可以使用官网cdn地址,自行百度-->
<script src="./js/docx-preview.min.js"></script>
<!-- <script src="https://volodymyrbaydalka.github.io/docxjs//dist/docx-preview.js"></script> -->

<body>
<div id="wordViewer"></div>
<script type="module">
    import {utils} from "./js/utils.js";

    let url = utils.getQueryVariable("url");
    url = url ? url : 'index.docx'
    wordView(url);
    console.log(document.getElementsByTagName("title"));
    document.getElementsByTagName("title")[0].text = url;

    function wordView(url) {
        let vm = this;
        let xhr = new XMLHttpRequest();
        xhr.open("get", url, true);
        xhr.responseType = "arraybuffer";
        xhr.onload = function () {
            if (xhr.status === 200) {
                docx.renderAsync(xhr.response, document.getElementById("wordViewer"))
                    .then(x => {console.log("docx: finished");mybind();});
            }
        };
        xhr.send();
    }

    function mybind(){
        var element = document.querySelectorAll("article img");
        console.log(element)
        for (let elementElement of element) {
            console.log(elementElement)
            elementElement.onclick = function (e) {
                // 忽略
            }
        }
    }
</script>

</body>
</html>

utils.js

js 复制代码
const utils = {
    getQueryVariable(variable) {
        let query = window.location.search.substring(1);
        let vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] === variable) {
                return decodeURI(pair[1]);
            }
        }
        return false;
    },

    timerObj: {},

    /**
     * 防抖,点击再多次,最后一次生效
     *
     * @param id 操作id
     * @param call 操作函数
     * @param timeout 防抖间隔
     */
    antiShake(id, call, timeout) {
        let sortInputTimer = utils.timerObj[id];
        if (sortInputTimer) {
            // 杀掉上一个定时器
            clearTimeout(sortInputTimer);
        }
        // 开启下一个定时器
        utils.timerObj[id] = setTimeout(function () {
            clearTimeout(sortInputTimer);

            call ? call() : null;

            utils.timerObj[id] = 0;
        }, timeout ? timeout : 800);
    },

    /**
     * 限流,每次点击,只有第一次生效,到定时结束前,都不生效
     *
     * @param id 操作id
     * @param call 操作函数
     * @param timeout 限流间隔
     */
    currentLimit(id, call, timeout) {
        let sortInputTimer = utils.timerObj[id];
        // 如果有定时器,则不执行
        if (sortInputTimer) {
            return false;
        }
        call ? call() : null;
        // 开启一个定时器
        utils.timerObj[id] = setTimeout(function () {
            // 清空定时器
            clearTimeout(sortInputTimer);
            utils.timerObj[id] = 0;
        }, timeout ? timeout : 800);
    }
}

export {utils}
相关推荐
frankz6118 分钟前
word无法创建工作文件,检查临时环境变量。
word
m0_748247551 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
m0_748255022 小时前
前端常用算法集合
前端·算法
真的很上进2 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
web130933203982 小时前
vue elementUI form组件动态添加el-form-item并且动态添加rules必填项校验方法
前端·vue.js·elementui
NiNg_1_2342 小时前
Echarts连接数据库,实时绘制图表详解
前端·数据库·echarts
如若1233 小时前
对文件内的文件名生成目录,方便查阅
java·前端·python
滚雪球~4 小时前
npm error code ETIMEDOUT
前端·npm·node.js
沙漏无语4 小时前
npm : 无法加载文件 D:\Nodejs\node_global\npm.ps1,因为在此系统上禁止运行脚本
前端·npm·node.js
supermapsupport4 小时前
iClient3D for Cesium在Vue中快速实现场景卷帘
前端·vue.js·3d·cesium·supermap