項目遇到一個問題,在web頁面中,禁止長按圖片保存,
使用css屬性:
img { pointer-events: none; }
或者
img { -webkit-user-select: none; }
無效,
以上屬性在瀏覽器中是可以實現長按不能保存的,
接下來思考到既然要讓圖片不能觸發手機自帶的長按保存事件,需要讓圖片失去焦點,
如何失去焦點呢,其實很簡單,只要在圖片上遮一層盒子就行啦~
上代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style> .box{ width: 400px; height: 400px; border: 2px solid salmon; position: relative; } .wrap{ width: 400px; height: 400px; position: absolute; opacity: 0.5; top: 0; left: 0; right: 0; bottom: 0; margin: auto; background-color: darkslateblue; z-index: 100; } img{ width: 400px; height: 400px; } </style>
</head>
<body>
<div class="box">
<div class="wrap"></div><!--遮罩盒子-->
<img src="img/1.jpg" />
</div>
</body>
</html>
這樣就能在APP上禁止長按圖片保存到本地了