移动端 --- 阻止浏览器点击图片会预览的方法

下面介绍几种方法:html

1. 在img元素上添加 onclick="return false" (推荐)url

<img src="a.png" onclick="return false" />

 

2. 图片用背景图的方式插入spa

background:url(a.png) norepeat center;

 

3. 使用js事件阻止默认行为的方法,这里须要注意哦!code

var img = document.getElementById('banner');xml

img.addEventListener('click',function(e){htm

  e.preventDefault();事件

});图片

关于这里的click事件,其实也能够是touchend事件,可是不能够是touchstart和touchmove事件!ip

由于使用touchstart和touchmove事件的时候,假如页面顶部有个超级大的banner图,那么当横屏显示或者相似于ipad等屏幕宽度大于高度的状况下,整个banner图都占满了屏幕,这个时候页面无法滑动。由于你用touchstart和touchmove禁止掉了图片的默认行为,因此手指怎么滑动,页面都没反应的。恰好这个滑动的行为触发了touchstart和touchmove。get

相关文章
相关标签/搜索