CSS cursor属性详解:控制鼠标指针样式的实用指南 基础指针值:如default(默认)、pointer(可点击)、text(输入状态)等系统交互值:wait(加载中)、progress(处理

一、cursor 属性概述

cursor 用来设置鼠标指针悬停在元素上时显示的光标样式。

它的作用:

  • 提示用户元素的交互性(例如按钮、链接)。

  • 提升用户体验(例如加载中状态显示等待光标)。

  • 增强可视化效果(例如放大镜、拖拽手势)。

语法:

selector { cursor: value; }

二、常见取值分类

1. 基础指针

效果使用场景
default默认箭头常规元素
pointer小手图标链接、按钮、可点击区域
text文本输入光标(I 型)输入框、可选文字区域
not-allowed圆圈禁止符号禁用按钮、不可点击元素

2. 系统交互相关

效果使用场景
wait沙漏/转圈页面加载中
progress箭头+转圈正在处理,可同时操作
help带问号提示说明区域

3. 移动与拖拽

效果使用场景
move四向箭头可拖动元素
grab抓取手势可拖拽区域(现代浏览器支持)
grabbing抓取中正在拖动时

4. 调整大小

效果使用场景
resize 系列双箭头改变元素大小
n-resize上下箭头垂直调整
s-resize下箭头垂直调整
e-resize右箭头水平调整
w-resize左箭头水平调整
ne-resize右上角拖动
nw-resize左上角拖动
se-resize右下角拖动
sw-resize左下角拖动

5. 特殊图标(跨浏览器支持有限)

效果使用场景
alias小箭头+快捷方式链接快捷方式
copy十字+加号拷贝操作
crosshair十字光标画布绘制、精确选择
zoom-in放大镜图片放大
zoom-out缩小镜图片缩小
col-resize左右箭头调整列宽
row-resize上下箭头调整行高

三、自定义光标

可以使用自定义图片作为光标:

.custom-cursor {
  cursor: url('cursor.png') 4 12, auto;
}
  • url('cursor.png'):光标 图片 路径。

  • 4 12:热点坐标(光标点击的精确点),从左上角算起。

  • auto:备用光标,当图片无效时使用默认箭头。


四、综合示例 Demo

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>CSS Cursor 属性 Demo</title>
  <style>
    body {
      font-family: Arial;
      display: flex;
      flex-wrap: wrap;
      gap: 20px;
    }
    div {
      width: 120px;
      height: 80px;
      display: flex;
      align-items: center;
      justify-content: center;
      border: 1px solid #ccc;
      border-radius: 5px;
      background: #f0f0f0;
    }
    .pointer { cursor: pointer; }
    .text { cursor: text; }
    .not-allowed { cursor: not-allowed; }
    .wait { cursor: wait; }
    .move { cursor: move; }
    .grab { cursor: grab; }
    .grabbing { cursor: grabbing; }
    .zoom-in { cursor: zoom-in; }
    .zoom-out { cursor: zoom-out; }
    .crosshair { cursor: crosshair; }
    .custom { cursor: url('https://cdn-icons-png.flaticon.com/512/32/32339.png') 16 16, auto; }
  </style>
</head>
<body>

  <div class="pointer">pointer</div>
  <div class="text">text</div>
  <div class="not-allowed">not-allowed</div>
  <div class="wait">wait</div>
  <div class="move">move</div>
  <div class="grab">grab</div>
  <div class="grabbing">grabbing</div>
  <div class="zoom-in">zoom-in</div>
  <div class="zoom-out">zoom-out</div>
  <div class="custom">custom</div>
  <div style="cursor: progress;">progress</div>
  <div style="cursor: help;">help</div>
  <div style="cursor: n-resize;">n-resize</div>
  <div style="cursor: s-resize;">s-resize</div>
  <div style="cursor: e-resize;">e-resize</div>
  <div style="cursor: w-resize;">w-resize</div>

  
  <div style="cursor: ne-resize;">ne-resize</div>
  <div style="cursor: nw-resize;">nw-resize</div>
  <div style="cursor: se-resize;">se-resize</div>
  <div style="cursor: sw-resize;">sw-resize</div>

  <div style="cursor: alias;">alias</div>
  <div style="cursor: copy;">copy</div>
  <div style="cursor: crosshair;">crosshair</div>
  <div style="cursor: col-resize;">col-resize</div>
  <div style="cursor: row-resize;">row-resize</div>


</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

星空下的DeppBing

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

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

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

打赏作者

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

抵扣说明:

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

余额充值