JS实现鼠标点击出现文字特效

使用方法:

在 WordPress 主题的 footer.php 文件中 <?php wp_footer(); ?> 的前面加上一段代码。
JS代码如下:

 <script>
        window.onload = function () {
            function ClickFrontShow() {
                this.fron = ['忆', '', '❤', '回忆', '悦', '1琦', '乐', '琦', '悦', '忆', '琦', '腾', '跃', '快'];
                this.colo = ['orange', 'red', 'orange', '#FF50FF', 'yellow', '#00B35F', '#ff55D3'];
                this.elBody = document.getElementsByTagName("body")[0];
                this.randomNum = null;
                this.finde = 0;
                this.cls = 0;
            }
            ClickFrontShow.prototype.init = function (frontArray, colorArray) {
                this.fron = frontArray || this.fron;
                this.colo = colorArray || this.colo;
                this.listenMouse();
            }
            ClickFrontShow.prototype.createFront = function (classname) {
                var self = this;
                let ospan = document.createElement('span');
                let cssText = "position:absolute; width: 40px; height: 20px; cursor: default; transform: translate(-50%,-50%); font-weight: bold; opacity: 1; z-index: 1000; transition: 1s;";
                let randomFront = self.fron[self.finde];
                let randomColor = self.colo[Math.round(Math.random() * (self.colo.length - 1))];
                self.finde = (self.finde + 1) % self.fron.length;
                self.elBody.appendChild(ospan);
                ospan.className = String(classname);
                ospan.style.cssText = cssText + "-moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;user-select: none;"
                ospan.style.color = randomColor;
                ospan.innerHTML = randomFront;
            }
            ClickFrontShow.prototype.listenMouse = function () {
                var self = this;
                document.onclick = function (e) {
                    if (self.cls === 20) {
                        self.cls = 0;
                    } else {
                        self.cls += 1;
                    }
                    self.createFront(self.cls);
                    let el = document.getElementsByClassName(self.cls)[0];
                    el.style.left = e.clientX + 'px';
                    el.style.top = e.clientY + 'px';
                    setTimeout(function () {
                        el.style.opacity = 0;
                        el.style.top = el.offsetTop - 100 + 'px';
                    }, 100);
                    setTimeout(function () {
                        self.elBody.removeChild(el);
                    }, 2000);
                }

            }
            var frontShow = new ClickFrontShow();
            frontShow.init();
        }
    </script>

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是几个常见的文字悬浮特效的实现方式: 1. 鼠标悬浮时改变文字颜色或背景色 通过监听鼠标事件,当鼠标移入时改变文字或背景颜色,移出时恢复原来的颜色。 ```html <style> .hover-effect { color: #333; transition: color .3s ease-out; } .hover-effect:hover { color: #f00; } </style> <p class="hover-effect">悬浮效果</p> ``` 2. 鼠标悬浮时添加阴影效果 通过设置 `box-shadow` 属性,当鼠标移入时添加阴影效果,移出时恢复原来的样式。 ```html <style> .hover-effect { box-shadow: 0 0 10px #333; transition: box-shadow .3s ease-out; } .hover-effect:hover { box-shadow: 0 0 20px #f00; } </style> <p class="hover-effect">悬浮效果</p> ``` 3. 鼠标悬浮时添加旋转效果 通过设置 `transform` 属性,当鼠标移入时添加旋转效果,移出时恢复原来的样式。 ```html <style> .hover-effect { transition: transform .3s ease-out; } .hover-effect:hover { transform: rotate(360deg); } </style> <p class="hover-effect">悬浮效果</p> ``` 4. 鼠标悬浮时添加动画效果 通过设置 `animation` 属性,当鼠标移入时播放动画效果,移出时停止动画。 ```html <style> .hover-effect { animation: rotate .8s ease-out infinite; } .hover-effect:hover { animation-play-state: paused; } @keyframes rotate { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } </style> <p class="hover-effect">悬浮效果</p> ``` 以上只是一些简单的实现方式,实际应用中可以根据需求自由组合和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓶中蛇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值