highlight实现自定义代码块高亮及左右滑动

在网页使用过程中,经常会用到代码的展示。而不同颜色的代码,可以让代码看起来更直观,也更美观。
找了几个不同的插件,觉得highlight的插件比较实用,而且用起来炒鸡简单。

1.引入css文件

    <!--开启代码高亮css-->
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/dark.min.css">

2.引入js文件

    <!--开启代码高亮js-->
    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js"></script>

3.自定义css文件内容

/*makedown代码块高亮自定义,横向的滑动块--start*/
.hljs-ln-numbers {
    text-align: center;
    color: #ccc;
    border-right: 1px solid #999;
    vertical-align: top;
    margin-left: 5px;
    padding-left: 5px;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/*去掉边框*/
pre {
    border: none;
}

code {
    /*实现左右滑动块*/
    white-space: pre !important;
    overflow-x: scroll !important;
    word-break: keep-all !important;
    word-wrap: initial !important;
}

.cnblogs-markdown .hljs {
    font-size: 16px !important;
    font-family: consolas, monospace !important;
    display: block;
    overflow-x: auto;
    padding: 0.5em;
    background: #23241f !important;
    color: #FFF;
    white-space: pre;
    word-break: normal;
    padding: 10px 15px !important;
}

.hljs,
.hljs-tag,
.hljs-subst {
    color: #f8f8f2;
}

.hljs-strong,
.hljs-emphasis {
    color: #a8a8a2;
}

.hljs-bullet,
.hljs-quote,
.hljs-number,
.hljs-regexp,
.hljs-literal,
.hljs-link {
    color: #ae81ff;
}

.hljs-code,
.hljs-title,
.hljs-section,
.hljs-selector-class {
    color: #a6e22e;
}

.hljs-strong {
    font-weight: bold;
}

.hljs-emphasis {
    font-style: italic;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-name,
.hljs-attr {
    color: #f92672;
}

.hljs-symbol,
.hljs-attribute {
    color: #66d9ef;
}

.hljs-params,
.hljs-class .hljs-title {
    color: #f8f8f2;
}

.hljs-string,
.hljs-type,
.hljs-built_in,
.hljs-builtin-name,
.hljs-selector-id,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-addition,
.hljs-variable,
.hljs-template-variable {
    color: #e6db74;
}

.hljs-comment,
.hljs-deletion,
.hljs-meta {
    color: #75715e;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-built_in,
.hljs-name,
.hljs-tag {

    color: #569CD6;

}

.hljs-string,
.hljs-type,
.hljs-built_in,
.hljs-builtin-name,
.hljs-selector-id,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-addition,
.hljs-variable,
.hljs-template-variable {

    color: #CE9178;

}


.hljs-comment,
.hljs-deletion,
.hljs-meta {

    color: #6A9955;

}

.hljs-bullet,
.hljs-quote,
.hljs-number,
.hljs-regexp,
.hljs-literal,
.hljs-link {

    color: #B5CEA8;

}
.hljs-code,
.hljs-title,
.hljs-section,
.hljs-selector-class {

    color: #DCDCAA;

}
/*makedown代码块高亮自定义,横向的滑动块--end*/

4.自定义js,开启行号和代码块高亮

<script>
    var messages = $("#messages").val();
    if (messages!==""){
        alert(messages);
    }
    // 开启代码高亮
    hljs.initHighlightingOnLoad();
    // 行号显示
    hljs.initLineNumbersOnLoad({singleLine: true});
</script>

具体效果可以去我博客网站查看(http://sun1yue.cn/)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现在Vue中返回数据实现打字机效果并且代码,可以使用Vue的一些特性和插件。 首先,要实现打字机效果,可以利用Vue的生命周期钩子函数和定时器来逐个显示文字。在Vue的data中定义一个文本内容的变量,然后在mounted钩子函数中,使用定时器设置每隔一段时间将文本内容逐渐显示出来。可以通过逐渐增加字符串的方式,来模拟打字机效果。例如: ```javascript data() { return { textContent: "", originalText: "这是要显示的文本内容" } }, mounted() { let index = 0; setInterval(() => { if (index < this.originalText.length) { this.textContent += this.originalText[index]; index++; } }, 100); } ``` 接下来,要实现代码,可以使用Vue插件highlight.js。首先,安装highlight.js插件: ```bash npm install highlight.js ``` 然后,在Vue中引入highlight.js并配置,以及在需要代码块元素上添加样式类名。例如: ```html <template> <div> <pre><code class="example-code javascript">{{ code }}</code></pre> </div> </template> <script> import hljs from 'highlight.js/lib/core'; import javascript from 'highlight.js/lib/languages/javascript'; import 'highlight.js/styles/monokai.css'; export default { data() { return { code: `// 这里是需要代码` } }, mounted() { hljs.registerLanguage('javascript', javascript); hljs.highlightAll(); } } </script> <style> .example-code { padding: 10px; } </style> ``` 以上是实现在Vue中返回数据实现打字机效果并且代码的方法。通过利用Vue的生命周期钩子函数和定时器来逐个显示文本内容,再使用highlight.js插件来实现代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值