Disabling page zoom in IE7 (jQuery/JS)

2019-08-23 05:14发布

问题:

I know this is not the best thing to do in view of accessibility, but I have a genuine need to disable the user from zooming onto the page using CTRL+ in IE7.

I got it working for the other browsers the following way, but IE7 seems to ignore the "return false":

$(window).keydown(function (e) {

     alert('key is down');   // this fires          
     return false;          // but this has no effect in IE7!
});

回答1:

This is better and correct way:

$(document).ready(function() {
    var ctrl = false;
    $(document).keydown(function(e){    
        // disable ctrl + +/-
        if(ctrl && (e.keyCode == 107 || e.keyCode == 109)) {
            alert('Zoom is disabled!');
            return false;
        }
        if(e.keyCode == 17) {
            ctrl = true;

            // disable ctrl + scroll
            $(document).bind('scroll', function() {
                if(ctrl) {
                    alert('Zoom is disabled!');
                    return false;
                }                               
            });
        }
    })

    $(document).keyup(function(e) {
        if(e.keyCode == 17) {
            ctrl = false;
            $(document).unbind('scroll');
        }                  
    });                    
});


回答2:

Try attaching keydown to document instead:

$(document).keydown(function (e) {

     alert('key is down');
     return false;
});


回答3:

This is pointless if the end user's browser already has the zoom set before visiting your page.



回答4:

simple answer. for IE, you need Event.stop(e); instead of return false;



回答5:

I don't have IE7 to test on ATM but this should do it

$(window).keydown(function (e) {
  alert('key is down');   // this fires             
  e.preventDefault();     // This is a standard jQuery way of 
                          // preventing the default action
  return false;           // Therefore you shouldn't need this.
});