如何在移动端中禁止长按图片保存

      发布在:前端技术      评论:0 条评论
<p style="margin-top: 0px;margin-bottom: 15px;padding: 0px;box-sizing: border-box;overflow-wrap: break-word;color: rgb(51, 51, 51);line-height: 1.7;font-family: -apple-system, BlinkMacSystemFont, &#39;Helvetica Neue&#39;, &#39;PingFang SC&#39;, &#39;Microsoft YaHei&#39;, &#39;Source Han Sans SC&#39;, &#39;Noto Sans CJK SC&#39;, &#39;WenQuanYi Micro Hei&#39;, sans-serif;font-size: 16px;text-wrap: wrap;background-color: rgb(255, 255, 255)">在移动端访问H5页面的时候,长按图片就会把图片保存起来,为了能够让用户体验更好一些,我们需要长按的时候也不保存图片。那该如何实现呢?下面给出3种解决方案。</p><h3 style="margin: 1em 0px 16px;padding: 0px;box-sizing: border-box;color: rgb(51, 51, 51);line-height: 1.43;font-size: 18px;position: relative;font-family: -apple-system, BlinkMacSystemFont, &#39;Helvetica Neue&#39;, &#39;PingFang SC&#39;, &#39;Microsoft YaHei&#39;, &#39;Source Han Sans SC&#39;, &#39;Noto Sans CJK SC&#39;, &#39;WenQuanYi Micro Hei&#39;, sans-serif;text-wrap: wrap;background-color: rgb(255, 255, 255)">方案一:使用 pointer-events:none</h3><pre class="brush:bash;toolbar:false">img{ pointer-events:none; }<br/></pre><p style="margin-top: 0px;margin-bottom: 15px;padding: 0px;box-sizing: border-box;overflow-wrap: break-word;color: rgb(51, 51, 51);line-height: 1.7;font-family: -apple-system, BlinkMacSystemFont, &#39;Helvetica Neue&#39;, &#39;PingFang SC&#39;, &#39;Microsoft YaHei&#39;, &#39;Source Han Sans SC&#39;, &#39;Noto Sans CJK SC&#39;, &#39;WenQuanYi Micro Hei&#39;, sans-serif;font-size: 16px;text-wrap: wrap;background-color: rgb(255, 255, 255)">亲测有效,适用于微信客户端的手机页面,图片被打开的情况.&nbsp;</p><h3 style="margin: 1em 0px 16px;padding: 0px;box-sizing: border-box;color: rgb(51, 51, 51);line-height: 1.43;font-size: 18px;position: relative;font-family: -apple-system, BlinkMacSystemFont, &#39;Helvetica Neue&#39;, &#39;PingFang SC&#39;, &#39;Microsoft YaHei&#39;, &#39;Source Han Sans SC&#39;, &#39;Noto Sans CJK SC&#39;, &#39;WenQuanYi Micro Hei&#39;, sans-serif;text-wrap: wrap;background-color: rgb(255, 255, 255)">方案二:全局属性</h3><pre class="brush:css;toolbar:false">*{<br/> -webkit-touch-callout: none;<br/> -webkit-user-select: none;<br/> -moz-user-select: none;<br/> -ms-user-select: none;<br/> user-select: none;<br/>}<br/></pre><p style="margin-top: 0px; margin-bottom: 15px; padding: 0px; box-sizing: border-box; overflow-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: 16px; text-wrap: wrap; background-color: rgb(255, 255, 255);">-webkit-touch-callout主要用于禁止长按菜单。当然针对webkit内核的浏览器。</p><p style="margin-top: 0px; margin-bottom: 15px; padding: 0px; box-sizing: border-box; overflow-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: 16px; text-wrap: wrap; background-color: rgb(255, 255, 255);">user-select属性是css3新增的属性,用于设置用户是否能够选中文本。</p><p style="margin-top: 0px; margin-bottom: 15px; padding: 0px; box-sizing: border-box; overflow-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: 16px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong><span style="font-size: 18px;">方案三:加一层遮罩层&nbsp;</span></strong></p><p style="margin-top: 0px;margin-bottom: 15px;padding: 0px;box-sizing: border-box;overflow-wrap: break-word;color: rgb(51, 51, 51);line-height: 1.7;font-family: -apple-system, BlinkMacSystemFont, &#39;Helvetica Neue&#39;, &#39;PingFang SC&#39;, &#39;Microsoft YaHei&#39;, &#39;Source Han Sans SC&#39;, &#39;Noto Sans CJK SC&#39;, &#39;WenQuanYi Micro Hei&#39;, sans-serif;font-size: 16px;text-wrap: wrap;background-color: rgb(255, 255, 255)">图片上边加一层div类似于遮罩层,这样图片就不会被点击,右击或长按也不会出现图片另存为的选项了。&nbsp; 代码示例如下:</p><pre class="brush:html;toolbar:false">&lt;div class=&quot;imgbox&quot;&gt;<br/> &lt;div class=&quot;imshar&quot;&gt;&lt;/div&gt;<br/> &lt;img src=&quot;&quot;/&gt;<br/>&lt;/div&gt;<br/>&lt;style&gt;<br/> .imgbox{<br/> position: relative;<br/> width: 80%;<br/> margin: 0 auto;<br/> margin-top: 20px;<br/>}<br/> .imgbox .imshar{<br/> position: absolute;<br/> z-index: 100;<br/> left: 0;<br/> right: 0;<br/> top: 0;<br/> bottom: 0;<br/> opacity: 0;<br/>}<br/>.imgbox img{<br/> display: block;<br/> width: 100%;<br/>}<br/>&lt;/style&gt;<br/></pre><p><br/></p>
相关文章
热门推荐