首页 > 其他分享 >写一个特效:鼠标经过文字时,文字旋转360度

写一个特效:鼠标经过文字时,文字旋转360度

时间:2024-12-12 09:32:58浏览次数:3  
标签:文字 rotate 鼠标 text ease transform element 360 out

// HTML
<span class="rotatable-text">旋转文字</span>


// CSS
.rotatable-text {
  display: inline-block; /* or block, depending on your needs */
  transition: transform 0.3s ease-in-out; /* Adjust duration and easing as needed */
}

.rotatable-text:hover {
  transform: rotate(360deg);
}

Explanation:

  • HTML: We wrap the text we want to rotate in a <span> with the class rotatable-text. You could use other elements like <div>, <p>, etc., depending on your layout needs.

  • CSS:

    • display: inline-block;: This ensures the element behaves like an inline element but can also have width and height properties, which are necessary for transformations. If you want the element to take up the full width of its container, use display: block; instead.
    • transition: transform 0.3s ease-in-out;: This line is crucial. It tells the browser to animate the transform property over 0.3 seconds using an ease-in-out transition. You can adjust the duration and the easing function to your liking. Common easing functions include ease, linear, ease-in, ease-out, ease-in-out.
    • transform: rotate(360deg);: This is applied when the mouse hovers over the element. It rotates the element 360 degrees clockwise.

Alternative using JavaScript (for more complex animations or interactions):

const rotatableTexts = document.querySelectorAll('.rotatable-text');

rotatableTexts.forEach(element => {
  element.addEventListener('mouseenter', () => {
    element.style.transform = 'rotate(360deg)';
  });

  element.addEventListener('mouseleave', () => {
    element.style.transition = 'transform 0.3s ease-in-out'; // Reset transition for smooth return
    element.style.transform = 'rotate(0deg)'; // Return to original rotation
  });
});

Explanation of the JavaScript version:

  1. querySelectorAll('.rotatable-text'): This selects all elements with the class rotatable-text.
  2. forEach: This loops through each selected element.
  3. addEventListener('mouseenter', ...): This adds an event listener that triggers a function when the mouse enters the element's boundaries. Inside the function, we set the transform property to rotate(360deg).
  4. addEventListener('mouseleave', ...): This adds an event listener that triggers a function when the mouse leaves the element's boundaries. Inside the function, we reset the transform property to rotate(0deg) to return the element to its original position and re-apply the transition for a smooth animation back.

Choose the method that best suits your needs. The CSS-only approach is simpler for basic rotations, while the JavaScript approach offers more flexibility for complex animations and interactions. Remember to include the JavaScript code within <script> tags in your HTML file, preferably just before the closing </body> tag.

标签:文字,rotate,鼠标,text,ease,transform,element,360,out
From: https://www.cnblogs.com/ai888/p/18601490

相关文章

  • 基于PyTorch/MXNet的高效中文/英文场景文字检测Python3包设计源码-Z5yUlJls
    基于PyTorch/MXNet的高效中文/英文场景文字检测Python3包设计源码地址CnSTD是一个高效的中文/英文场景文字检测Python3包,基于PyTorch/MXNet框架开发。该项目包含106个文件,主要为Python源码文件(63个.py),以及多种格式的资源文件,包括22个JPG图片、6个PNG图片、3个Makefile、3个YAML配......
  • 如何实现默认显示git的第一帧图片,当鼠标经过时,播放完整的gif动画
    要实现鼠标悬停时播放GIF动画,默认显示GIF的第一帧,你可以使用JavaScript和HTML。以下是一种实现方法:HTML:<divclass="gif-container"><imgsrc="first-frame.jpg"data-gif="animation.gif"alt="Description"></div>first-frame.jp......
  • Springboot+vue仿腾讯在线会议系统,支持在线会议(包括语音和投屏)以及文字聊天功能,最近
    支持在线会议(包括语音和投屏)以及文字聊天功能,最近完成的项目,算是比较小众的项目,功能比较完善,美中不足的就是页面ui设计一般,可先看项目的完整演示项目演示:https://www.bilibili.com/video/BV1ALsre3Epm/小众项目,拿来做毕设简直是嘎嘎乱杀,项目质量是很好可以保证的,大家可以看......
  • textillate-jQuery和css3文字动画特效库
    textillate.js是一款效果炫酷的jQuery和css3文字动画特效库插件。它通过结合其它一些动画库来制作各种CSS3文字动画特效。在线演示下载 如何使用基本的html结构如下:<h1class="tlt">MyTitle</h1>在页面的头部引入jQuery和jquery.textillate.js文件。......
  • 使用css制作鼠标经过图片时,放大图片1.5倍
    <!DOCTYPEhtml><html><head><title>ImageHoverZoom</title><style>.container{width:200px;/*Adjustasneeded*/height:200px;/*Adjustasneeded*/overflow:hidden;/*Hideoverflowingzoomedimage*/}......
  • 使用 Vyper 编写简易文字识别程序
    Vyper是一种Python风格的智能合约编程语言,主要用于Ethereum区块链的智能合约开发。尽管Vyper主要应用于智能合约领域,我们依然可以用它来实现一些基础的程序逻辑。这篇文章将展示如何用Vyper编写一个基本的文字识别程序,通过处理用户输入的简单特征字符串来“识别”对应的......
  • manim边做边学--文字的创建与销毁
    本篇开始介绍Manim中的动画模块,动画模块是整个框架的核心魅力所在。Manim不仅提供了可以直接实现各种各样动画效果的对象,还提供了设置动画的时长、延迟时间以及运动速率等参数,可以据此发挥自己的创意,自定义出与众不同的动画效果。本篇主要介绍与文字相关的几个内置的动画效果。......
  • 终于解决了.net在线客服系统总是被360误报的问题(对软件进行数字签名)
    https://www.cnblogs.com/sheng_chao/p/18581139 升讯威在线客服与营销系统是基于.netcore/WPF开发的一款在线客服软件,宗旨是: 开放、开源、共享。努力打造.net社区的一款优秀开源产品。背景我在业余时间开发的这个客服系统,有一个问题始终让我饱受困扰,360以及各种国产......
  • 7-194 输出大写英文字母
    本题要求编写程序,顺序输出给定字符串中所出现过的大写英文字母,每个字母只输出一遍;若无大写英文字母则输出“NotFound”。输入格式:输入为一个以回车结束的字符串(少于80个字符)。输出格式:按照输入的顺序在一行中输出所出现过的大写英文字母,每个字母只输出一遍。若无大写英文......
  • 表格bodyCell响应式处理,划入列展示tooltip文字
    //表格优化 bodyCell(column){  if(column.column?.key!=='operation'){   column.column.width='auto';  }  if(column.column.render){   constcellRender=column.column.render.bind(this)   return(   <antTooltip......