react 光标

I am constantly surprised and inspired by so many websites on awwwards.com, many of which frequently feature cool cursors and various effects.

我不断地被awwwards.com上的许多网站感到惊讶和启发,其中许多网站经常具有漂亮的光标和各种效果。

In these series, I’ll break down the process of making custom cursors and try to experiment with various cool visual effects based on them, all using React.js and Framer Motion.

在这些系列中,我将分解制作自定义光标的过程,并尝试使用React.js和Framer Motion尝试基于它们的各种炫酷视觉效果。

基本自定义光标 (Basic Custom Cursor)

First, let’s start by defining some basic structure and styling for our custom cursor, which will just be a black circle and be placed in a fixed position at the top: 0; left: 0; corner for now.

首先,让我们为自定义光标定义一些基本结构和样式,这将是一个黑色圆圈,并放在top: 0; left: 0;fixed位置top: 0; left: 0; top: 0; left: 0; 现在的角落。

function App() {  return (    <div className="App">      <div className="cursor" />    </div>  )}---------------------------------.cursor {  position: fixed;  left: 0;  top: 0;  width: 32px;  height: 32px;  border-radius: 16px;  background-color: black;}

Now that we have the custom cursor let’s bring it to life with some JavaScript. First, we need to capture the mousemove events and define a listener function which will animate our cursor to a correct position.

现在有了自定义光标,让我们通过一些JavaScript使其生动起来。 首先,我们需要捕获mousemove事件并定义一个侦听器函数,该函数会将光标动画化到正确的位置。

Since we’ll be using a functional component we need to define our listener in a useEffect hook.

由于我们将使用功能组件,因此需要在useEffect挂钩中定义侦听器。

import React, { useState, useEffect } from "react"function App() {  const [cursorXY, setCursorXY] = useState({ x: -100, y: -100 })  useEffect(() => {    const moveCursor = (e) => { }    window.addEventListener('mousemove', moveCursor)    return () => {      window.removeEventListener('mousemove', moveCursor)    }  }, [])  return (    ...  )}...

The return function in the useEffect removes the listener on cleanup and makes sure that we don’t add multiple event listeners for the same event. The last argument given to useEffect is an empty array [], which means we’ll be only running the function once over the component’s lifecycle, since it doesn’t depend on any props.

useEffect中的return函数会在清理时删除侦听器,并确保我们不会为同一事件添加多个事件侦听器。 为useEffect提供的最后一个参数是一个空数组[] ,这意味着我们将在组件的整个生命周期中仅运行一次该函数,因为它不依赖于任何道具。

As you can see we’ve also used useState to define the initial coordinates for our cursor, which we’ll change later when we switch to using some framer-motion magic

react 光标_带有React Framer运动部分的超酷自定义光标相关推荐

  1. react 事件处理_在React中处理事件

    react 事件处理 在使用React渲染RESTful服务后,我们创建了简单的UI,用于渲染从RESTful服务获取的员工列表. 作为本文的一部分,我们将扩展同一应用程序以支持添加和删除员工操作. ...

  2. python组件的react实现_【React源码解读】- 组件的实现

    前言 react使用也有一段时间了,大家对这个框架褒奖有加,但是它究竟好在哪里呢? 让我们结合它的源码,探究一二!(当前源码为react16,读者要对react有一定的了解) 回到最初 根据react ...

  3. react上下文_了解React的新上下文API

    react上下文 介绍 (Introduction) In a world where there are lots of different front-end frameworks, it's a ...

  4. react安装_「React实战」三分钟搭建React开发环境

    其实16年的时候就已经接触到React,那个时候也只是入门,时隔多年,工作上一直都没有接触到相关的业务,不知不觉,前端的天也开始渐变,看到 了很多招聘要求上都是要求会React,三大框架怎么也得熟悉使 ...

  5. input禁止光标_表单中readonly的input等标签,禁止光标进入(focus)的几种方式

    需求及问题描遇新是直朋能到述 在做移动端页面,需要在订单页面中显示表单数据,由于UI统一,所以就依旧采用form的结构来写结构,只读数据的标签自然要加readonly="readonly&q ...

  6. C#软件开发实例.私人订制自己的屏幕截图工具(九)使用自定义光标,QQ截图时的光标

    本实例全部文章目录 (一)功能概览 (二)创建项目.注册热键.显示截图主窗口 (三)托盘图标及菜单的实现 (四)基本截图功能实现 (五)针对拖拽时闪烁卡顿现象的优化 (六)添加配置管理功能 (七)添加 ...

  7. react 交互_如何在React中建立动画微交互

    react 交互 Microinteractions guide a user through your application. They reinforce your user experienc ...

  8. React动画实现方案之 Framer Motion,让你的页面“自己”动起来

    前言 相信很多前端同学都或多或少和动画打过交道.有的时候是产品想要的过度效果:有的时候是UI想要的酷炫动画.但是有没有人考虑过,是不是我们的页面上面的每一次变化,都可以像是自然而然的变化:是不是每一次 ...

  9. 构建一个react项目_您想要了解更多有关React的内容吗? 让我们构建一个游戏,然后玩。...

    构建一个react项目 by Samer Buna 通过Samer Buna 您想要了解更多有关React的内容吗? 让我们构建一个游戏,然后玩. (Do you want to learn more ...

最新文章

  1. MBA 工商管理课程-风险型决策方法
  2. CodeForces 66C Petya and File System (实现)
  3. 21世纪最需要的的七种人才 -李开复
  4. 006---Linux用户、群组和权限
  5. vue企业项目demo_基于SpringBoot和Vue的企业级中后台开源项目
  6. 微信小程序的点击复制功能
  7. 2013年全球ERP市场格局(Gartner)
  8. php 模糊过滤字符串,PHP简单字符串过滤方法示例
  9. 2015 总结 2016 展望
  10. Python的元组被设计成不可变的影响
  11. 代码覆盖度-NCover监控IIS和exe,结果分析
  12. oracle12c asm 冗余,深入分析:12C ASM Normal冗余中PDB文件块号与AU关系与恢复
  13. 卷积神经网络创建模型
  14. python可以处理什么文件_第十五篇 Python之文件处理
  15. 系统开发mysql数据库设计实例_MYSQL数据库设计和数据库设计实例(一)_MySQL
  16. CNKI知网论文下载工具
  17. Jade的安装与测试
  18. 本地机房连接阿里云专有网络VPC构建混合云解决方案
  19. You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).Please, commit your changes
  20. 微信小程序—写字板、手写签名(高仿毛笔效果)让汉字引领世界

热门文章

  1. XP下安装IIS6.0的办法
  2. 将 C# .NET ( Core Framework ) WPF Winform 项目打包成单个 exe 可执行文件
  3. normalize.css 类似,使用normalize.css遇到的问题?
  4. 个人常用的sql脚本语句
  5. 局域网、广域网、Internet
  6. Java中的NaN(非数)、Infinity(无穷大)
  7. c# 转换Image为Icon
  8. Python Environment Interpreter
  9. 02-leveldb入门
  10. 云计算如何从谷歌诞生的?