CSS光标cursor

前面的话

  在浏览器中,光标对于提供交互反馈很有用。通过在不同的场景中改变光标,就能赋予其不同的含义

 

定义

  cursor光标

  值: [<uri>,]*[auto | default | pointer | crosshair | move | e-resize | ne-resize | nw-resize | n-resize | se-resize | sw-resize | s-resize | w-resize | text | wait | help | progress]] | inherit

  初始值: auto

  应用于: 所有元素

  继承性: 有

 

标准样式

  CSS2提供了相对较少的选择

url: 自定义光标的URL
default: 默认光标,通常是一个箭头
auto: 浏览器自动识别的光标
crosshair : 十字线
pointer: 手型指针
move: 移动指针
e-resize: 向东移动指针
ne-resize: 向东北移动指针
nw-resize: 向西北移动指针
n-resize: 向北移动指针
se-resize: 向东南移动指针
sw-resize: 向西南移动指针
s-resize: 向南移动指针
w-resize: 向西移动指针
text: 文本指针
wait: 指示程序正忙
help: 帮助指针

style="width: 100%; height: 320px;" src="https://demo.xiaohuochai.site/css/cursor/c1.html" frameborder="0" width="320" height="240">

 

拓展样式

  CSS3增加了更多的cursor的样式值

  [注意]所有拓展样式IE7-浏览器都不支持

cursor:none (not IE8, Safari)
cursor:context-menu (not Safari,Firefox,Chrome)
cursor:cell (not Safari)
cursor:alias (not Safari)
cursor:copy (not IE,Safari)
cursor:grab (not IE,Safari,Chrome)
cursor:grabbing (not IE,Safari,Chrome)
cursor:zoom-in (not IE,Safari)
cursor:zoom-out (not IE,Safari)
cursor:vertical-text
cursor:no-drop
cursor:not-allowed
cursor:all-scroll
cursor:ew-resize
cursor:ns-resize
cursor:nesw-resize
cursor:nwse-resize
cursor:col-resize
cursor:row-resize

style="width: 100%; height: 400px;" src="https://demo.xiaohuochai.site/css/cursor/c2.html" frameborder="0" width="320" height="240">

私有样式

  有些浏览器还提供了增加浏览器前缀的私有样式

  [注意]safari将-webkit-grab和-webkit-grabbing都解释为default

cursor:-webkit-grab; cursor: -moz-grab;
cursor:-webkit-grabbing; cursor: -moz-grabbing;
cursor:-webkit-zoom-in; cursor: -moz-zoom-in;
cursor:-webkit-zoom-out; cursor: -moz-zoom-out;    

style="width: 100%; height: 300px;" src="https://demo.xiaohuochai.site/css/cursor/c3.html" frameborder="0" width="320" height="240">

自定义样式

  所有浏览器都支持使用后缀名为.cur的文件,chrome、firefox、safari还支持使用普通图片制作光标

  [注意]使用URL自定义样式,后面必须跟有一个逗号和某个通用关键字

//错误
cursor: url('m.cur');
//正确
cursor: url('m.cur'),auto;

style="width: 100%; height: 280px;" src="https://demo.xiaohuochai.site/css/cursor/c44.html" frameborder="0" width="320" height="240">

常见应用

  链接的默认光标是手型指针pointer,通过光标的变化可以让访问者清楚的知道该元素是可点击的

  元素的title属性用来提供元素的额外信息,配合help光标可以得到更好的表现方式

span[title]{
    cursor: help;
    border-bottom: 1px solid gray;
}
<div><span title="Cascading Style Sheets">CSS</span> is much too interesting</div>

style="width: 100%; height: 40px;" src="https://demo.xiaohuochai.site/css/cursor/c5.html" frameborder="0" width="320" height="240">


更多专业前端知识,请上 【猿2048】www.mk2048.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供相关的代码示例: ```html <template> <div> <el-button type="primary" @click="dialogVisible = true">点击打开弹窗</el-button> <el-dialog :visible.sync="dialogVisible" :modal="false" :close-on-click-modal="false" v-dialogDrag class="resizable-dialog" > <div class="resize-handle top-left" @mousedown="startResize($event, 'topLeft')"></div> <div class="resize-handle top-right" @mousedown="startResize($event, 'topRight')"></div> <div class="resize-handle bottom-left" @mousedown="startResize($event, 'bottomLeft')"></div> <div class="resize-handle bottom-right" @mousedown="startResize($event, 'bottomRight')"></div> <div class="resize-handle top" @mousedown="startResize($event, 'top')"></div> <div class="resize-handle right" @mousedown="startResize($event, 'right')"></div> <div class="resize-handle bottom" @mousedown="startResize($event, 'bottom')"></div> <div class="resize-handle left" @mousedown="startResize($event, 'left')"></div> <span slot="title">可调整大小的弹窗</span> <p>在这里可以添加弹窗的内容</p> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, resizing: false, resizeType: null, startX: null, startY: null, startWidth: null, startHeight: null } }, methods: { startResize(event, type) { this.resizing = true this.resizeType = type this.startX = event.clientX this.startY = event.clientY this.startWidth = this.$refs.dialog.$el.offsetWidth this.startHeight = this.$refs.dialog.$el.offsetHeight }, endResize() { this.resizing = false this.resizeType = null this.startX = null this.startY = null this.startWidth = null this.startHeight = null }, handleMouseMove(event) { if (this.resizing) { const deltaX = event.clientX - this.startX const deltaY = event.clientY - this.startY let newWidth = this.startWidth let newHeight = this.startHeight let newLeft = this.$refs.dialog.$el.offsetLeft let newTop = this.$refs.dialog.$el.offsetTop switch (this.resizeType) { case 'topLeft': newWidth -= deltaX newHeight -= deltaY newLeft += deltaX newTop += deltaY break case 'topRight': newWidth += deltaX newHeight -= deltaY newTop += deltaY break case 'bottomLeft': newWidth -= deltaX newHeight += deltaY newLeft += deltaX break case 'bottomRight': newWidth += deltaX newHeight += deltaY break case 'top': newHeight -= deltaY newTop += deltaY break case 'right': newWidth += deltaX break case 'bottom': newHeight += deltaY break case 'left': newWidth -= deltaX newLeft += deltaX break } this.$refs.dialog.$el.style.width = newWidth + 'px' this.$refs.dialog.$el.style.height = newHeight + 'px' this.$refs.dialog.$el.style.left = newLeft + 'px' this.$refs.dialog.$el.style.top = newTop + 'px' } } }, mounted() { document.addEventListener('mouseup', this.endResize) document.addEventListener('mousemove', this.handleMouseMove) }, beforeDestroy() { document.removeEventListener('mouseup', this.endResize) document.removeEventListener('mousemove', this.handleMouseMove) } } </script> <style> .resizable-dialog { position: absolute !important; top: 50% !important; left: 50% !important; transform: translate(-50%, -50%) !important; } .resize-handle { position: absolute; width: 10px; height: 10px; background-color: #999; } .resize-handle.top-left { top: -5px; left: -5px; cursor: nwse-resize; } .resize-handle.top-right { top: -5px; right: -5px; cursor: nesw-resize; } .resize-handle.bottom-left { bottom: -5px; left: -5px; cursor: nesw-resize; } .resize-handle.bottom-right { bottom: -5px; right: -5px; cursor: nwse-resize; } .resize-handle.top { top: -5px; left: 50%; transform: translateX(-50%); cursor: ns-resize; } .resize-handle.right { top: 50%; right: -5px; transform: translateY(-50%); cursor: ew-resize; } .resize-handle.bottom { bottom: -5px; left: 50%; transform: translateX(-50%); cursor: ns-resize; } .resize-handle.left { top: 50%; left: -5px; transform: translateY(-50%); cursor: ew-resize; } </style> ``` 在这个示例中,我们使用了 `v-dialogDrag` 指令使弹窗可以被拖拽,同时添加了可以调整大小的功能。具体地,我们在弹窗的四个角和四条边添加了可拖拽的缩放手柄,并在鼠标按下时监听 `mousedown` 事件,记录下当前的位置信息和弹窗的尺寸,然后在鼠标移动时根据手柄的位置和拖拽的距离计算出新的尺寸和位置,并更新弹窗的样式。在松开鼠标时,我们要将缩放状态重置回原始状态。同时,我们使用了 `@mousemove` 和 `@mouseup` 事件在全局范围内监听鼠标移动和松开的事件,以避免在拖拽时鼠标移动出了弹窗的范围。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值