美文网首页
hexo博客添加鼠标点击特效

hexo博客添加鼠标点击特效

作者: Sanarous | 来源:发表于2019-07-15 14:15 被阅读0次

    原文及演示链接:https://bestzuo.cn/posts/446056512.html

    image

    这个其实还比较简单,只需要在主题目录下next/source/js/src下面新建一个clicklove.js文件,当然名字可以自己任取,然后在其中加入以下内容:

    /*网页鼠标点击特效(爱心)*/
    !function (e, t, a) {function r() {for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");requestAnimationFrame(r)}function n() {var t = "function" == typeof e.onclick && e.onclick;e.onclick = function (e) {t && t(), o(e)}}function o(e) {var a = t.createElement("div");a.className = "heart", s.push({el: a,x: e.clientX - 5,y: e.clientY - 5,scale: 1,alpha: 1,color: c()}), t.body.appendChild(a)}function i(e) {var a = t.createElement("style");a.type = "text/css";try {a.appendChild(t.createTextNode(e))} catch (t) {a.styleSheet.cssText = e}t.getElementsByTagName("head")[0].appendChild(a)}function c() {return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"}var s = [];e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {setTimeout(e, 1e3 / 60)}, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()}(window, document);
    
    

    上面这个是鼠标点击出现小心心的文件,也可以加上鼠标点击出现社会主义核心价值观的js文件,如果只想要一个就只加一个文件即可,在刚才的目录下继续创建clicksocialvalue.js文件,然后在文件中加入以下内容:

    (function() {
        var coreSocialistValues = ["富强", "民主", "文明", "和谐", "自由", "平等", "公正", "法治", "爱国", "敬业", "诚信", "友善"],
        index = Math.floor(Math.random() * coreSocialistValues.length);
        
        document.body.addEventListener('click',
        function(e) {
        //过滤a标签
            if (e.target.tagName == 'A') {
                return;
            }
            var x = e.pageX,
            y = e.pageY,
            span = document.createElement('span');
            span.textContent = coreSocialistValues[index];
            index = (index + 1) % coreSocialistValues.length;
            span.style.cssText = ['z-index: 9999999; position: absolute; font-weight: bold; color: #ff6651; top: ', y - 20, 'px; left: ', x, 'px;'].join('');
            document.body.appendChild(span);
            animate(span);
        });
    

    当然如果两个都需要的话,可以直接将两个文件的代码合并到一个文件即可。

    然后在主题目录下next/layout/_layout.swig文件中的<body></body>体标签内引入刚才创建的js文件:

      {% include '_third-party/comments/index.swig' %}
      <script type="text/javascript" src="/js/src/clicklove.js"></script>
      <script type="text/javascript" src="/js/src/clicksocialvalue.js"></script>
    

    然后只需要重新渲染页面hexo g && hexo s就可以看到效果啦~

    相关文章

      网友评论

          本文标题:hexo博客添加鼠标点击特效

          本文链接:https://www.haomeiwen.com/subject/tfdnkctx.html