img,video标签禁用鼠标右键功能

场景描述:

       在网页中显示图片,当用户右键点击图片时,禁止用户右键操作。

 

这里会用到一个新属性,即 oncontextmenu。

例如:

复制代码
<img
src="图片地址"
class="unselectable"
draggable="false"
oncontextmenu="return false"
/>
其中:
类名: .unselectable, 用于禁用鼠标选中
draggable="false", 禁用拖拽
oncontextmenu="return false", 禁用鼠标右键

.unselectable{
-moz-user-select: -moz-none;
-ms-user-select: none;
-webkit-user-select: none;
-khtml-user-select: none;
-o-user-select: none;
user-select: none
}
复制代码

video标签

复制代码
<video
autoplay
width="600"
height="400"
controls
id="myVideo"
@contextmenu="contextmenu"
controlslist="nodownload noremoteplayback noplaybackrate"
disablePictureInPicture="true"
disableRemotePlayback="true"
></video>
#禁用右键
@contextmenu="contextmenu"
contextmenu(e){
return false;
}
#隐藏下载功能
在标签上添加 controlslist 属性值设置为 nodownload
#播放速度功能
在标签上添加 controlslist 属性值设置为 noplaybackrate
#远程播放功能
在标签上添加 controlslist 属性值设置为 noremoteplayback
在标签上添加 disableRemotePlayback="true"
#画中画功能
在标签上添加属性 disablePictureInPicture="true"
复制代码

 

参考链接:

    https://blog.csdn.net/weixin_45066149/article/details/119520229

posted on   有匪  阅读(561)  评论(2编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示