How to disable zooming in javascript
javascript实现禁止缩放的方法:1、设置对应浏览器的启动参数来禁止用户缩放页面;2、设置meta来禁止用户缩放页面;3、通过js监听来禁止用户缩放页面;4、禁用“ontouchmove”事件;5、通过多点触摸手势库实现。
本文操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
javascript怎么实现禁止缩放?
javascript浏览器禁止用户放大缩小的五种方法
方案一:设置对应浏览器的启动参数来禁止用户缩放页面
这种方案在自建平台上,自选的指定浏览器上效果是可以的,但是不推荐,比如chrome主要通过设置* { touch-acion : none }来实现禁用缩放的方法,具体方案可自行搜索,我也没有进行相关测试。
touch-acion的参数意义如下:
auto:默认值。浏览器允许一些手势(touch)操作在设置了此属性的元素上,例如:对视口(viewport)平移、缩放等操作。
none:禁止触发默认的手势操作。
pan-x:可以在父级元素(the nearest ancestor)内进行水平移动的手势操作。
pan-y:可以在父级元素内进行垂直移动的手势操作。
manipulation:允许手势水平/垂直平移或持续的缩放。任何auto属性支持的额外操作都不支持。
注:touch-action只支持具有CSS width和height属性的元素。这个限制的目的是帮助浏览器优化低延时的手势操作。对于默认不支持此属性的元素,如这种内联元素,可以给它设置display:block这样的CSS属性来支持width和height。未来W3C规范会将此API扩展到支持所有元素。
方案二:设置meta来禁止用户缩放页面
这是搜索后经常出现的方案,但是现在,这个标签在新的浏览器(比如在ios10+)中已经失效,换言之,对于老版本的浏览器可能有效。
在Android的自带浏览器中(例如华为,魅族,小米)第一次手动缩放时,会提示–再次操作可强制缩放网页—;再次缩放也可以缩放;
这一现象意味着meta标签的失效。在Android的chrome中不可以用户缩放(表现正常)
直接上代码(在.html文件中的
之间添加如下语句):<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" name="viewport" />
方案三:通过js监听来禁止用户缩放页面
直接上代码:
window.onload=function(){ document.addEventListener('touchstart',function (event) { if(event.touches.length>=2){ event.preventDefault(); } }) document.addEventListener('touchmove',function (event) { if(event.touches.length>=2){ event.preventDefault(); } }) document.addEventListener('touchend',function (event) { if(event.touches.length>=2){ event.preventDefault(); } }) }
其中的event.touches.length是获取当天有几个点击事件同时发生,简单而言,就是有几个手指同时点击了屏幕,以为一般缩放操作都是两个手指以上进行的,所以这里应该满足的条件为event.touches.length>=2。不足之处就是禁用了所有的多点触控的操作。
【推荐:javascript高级教程】
方案四:禁用“ontouchmove”事件
因为缩放屏幕必然跟随着双指的ontouchmove事件,我们在标签内加入禁用该事件的函数event.preventDefault()即可,这招非常暴力,意味着全局无法使用使用滑动、拖动等动作,只接受点击动作。
代码如下——
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ontouchmove="event.preventDefault();"> <head> /****some code***/ </head> <body> /****some code***/ </body> </html>
其中的event.preventDefault()函数将通知 Web 浏览器不要执行与事件关联的默认动作(如果存在这样的动作)。例如,如果 type 属性是 “submit”,在事件传播的任意阶段可以调用任意的事件句柄,通过调用该方法,可以阻止提交表单。注意,如果 Event 对象的 cancelable 属性是 fasle,那么就没有默认动作,或者不能阻止默认动作。无论哪种情况,调用该方法都没有作用。
写到这里,忽然想到,是否只禁用某个
区块的ontouchmove事件,比如:<div ontouchmove="event.preventDefault();"></div>
但这种区块禁用的方式我还没有测试,你可以说我比较懒了,自己去测试吧~~~~
然而还没有得瑟一个月,我就有了这个需求,补充如下,直接上实现代码:
var singleTouchFlag; //多指触控标识符 $("#songList").on("touchstart", function (e) { //引用了jquery库,我所要多指禁止的区域id为“songList” // 判断默认行为是否可以被禁用 console.log("touchstart Entered!!!"); if (e.cancelable) { // 判断默认行为是否已经被禁用 if (!e.defaultPrevented) { e.preventDefault(); } } if (1 == parseInt(e.originalEvent.touches.length)) { singleTouchFlag = true; // do something } else { singleTouchFlag = false; } }); $("#songList").on("touchmove", function (e) { // 判断默认行为是否可以被禁用 if (e.cancelable) { // 判断默认行为是否已经被禁用 if (!e.defaultPrevented) { e.preventDefault(); } } if (singleTouchFlag) { //do something } }); $("#songList").on("touchend", function (e) { // 判断默认行为是否可以被禁用 console.log("touchend Entered!!!"); if (e.cancelable) { // 判断默认行为是否已经被禁用 if (!e.defaultPrevented) { e.preventDefault(); } } if (singleTouchFlag) { //do something } });
方案五:借助于**多点触摸手势库“hammer.js ”**解决
hammer.js 是一个多点触摸手势库,能够为网页加入Tap、Double Tap、Swipe、Hold、Pinch、Drag等多点触摸事件,免去自己监听底层touchstart、touchmove、touchend事件并且写一大堆判断逻辑的痛苦。
hammer.js 不但支持触摸屏设备的浏览器,在桌面浏览器上,也能将鼠标的点击当做触摸,方便开发人员在桌面浏览器上调试。(JS仔在自己的随手背项目里面也用了hammer.js,真心好用)
直接上代码(在.html文件中的
之间添加如下语句):<script src = "http://eightmedia.github.com/hammer.js/hammer.js" > </script> <script> window.onload = function () { var hammerMusicBlock = new Hammer(document.getElementById("musicBlock")); hammerMusicBlock.ontransformstart = function (ev) { ev.preventDefault(); }; // double fingers touchstart hammerMusicBlock.ontransform = function (ev) { ev.preventDefault(); }; // double fingers touchmove hammerMusicBlock.ontransformend = function (ev) { ev.preventDefault(); }; // double fingers touchend } </script>
hammer.js 其他的使用方式简介,直接看代码:
<script src = "http://eightmedia.github.com/hammer.js/hammer.js" > </script> // 先要对监听的DOM进行一些初始化 var hammer = new Hammer ( document . getElementById ( "container" ) ) ; // 然后加入相应的回调函数即可 hammer . ondragstart = function ( ev ) { } ; // 开始拖动 hammer . ondrag = function ( ev ) { } ; // 拖动中 hammer . ondragend = function ( ev ) { } ; // 拖动结束 hammer . onswipe = function ( ev ) { } ; // 滑动 hammer . ontap = function ( ev ) { } ; // 单击 hammer . ondoubletap = function ( ev ) { } ; //双击 hammer . onhold = function ( ev ) { } ; // 长按 hammer . ontransformstart = function ( ev ) { } ; // 双指收张开始 hammer . ontransform = function ( ev ) { } ; // 双指收张中 hammer . ontransformend = function ( ev ) { } ; // 双指收张结束 hammer . onrelease = function ( ev ) { } ; // 手指离开屏幕 hammer.js 还支持jQuery插件的形式调用: <script src = "http://eightmedia.github.com/hammer.js/jquery.hammer.js" > </script> $ ( "#element" ) . hammer ( { // 对DOM进行一些初始化,这里可以加入一些参数 } ) . bind ( "tap" , function ( ev ) { console . log ( ev ) ; } ) ;
暂时总结这五种方案,如果有其他更好的方案,请添加到评论区,谢谢。
The above is the detailed content of How to disable zooming in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
