在移动端访问H5页面的时候,长按图片就会把图片保存起来,为了能够让用户体验更好一些,我们需要长按的时候也不保存图片。那该如何实现呢?下面给出3种解决方案。: s; Q: k! `( X6 |' s. S
方案一:使用 pointer-events:none+ ^2 X8 U8 b' u8 E8 }5 J* z# U% P
img{
pointer-events:none;
} 亲测有效,适用于微信客户端的手机页面,图片被打开的情况.
0 I8 K% N& K8 h' Q. L& K p# {/ D方案二:全局属性
v7 W' g) L5 }6 _*{
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
} -webkit-touch-callout主要用于禁止长按菜单。当然针对webkit内核的浏览器。user-select属性是css3新增的属性,用于设置用户是否能够选中文本。
9 |' T) Z+ @/ S5 ]/ m方案三:加一层遮罩层
. U9 P9 p+ I3 H) {" K3 v. Q 图片上边加一层div类似于遮罩层,这样图片就不会被点击,右击或长按也不会出现如图的图片另存为的选项了。代码示例如下:
8 |) E9 F9 e( R0 U* B& P# i6 f<div class="imgbox">
<div class="imshar"></div>
<img src=""/>
</div>
<style>
.imgbox{
position: relative;
width: 80%;
margin: 0 auto;
margin-top: 20px;
}
.imgbox .imshar{
position: absolute;
z-index: 100;
left: 0;
right: 0;
top: 0;
bottom: 0;
opacity: 0;
}
.imgbox img{
display: block;
width: 100%;
}
</style>
|