且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

触发多个按键以刺激键盘快捷键jquery

更新时间:2023-02-03 11:01:25

根据我的小提琴.

$(document).bind('keydown', function(e) {
    e.preventDefault();

    var d = new Date();
    $('#log').html(
        'time: ' + d.getTime() + '<br/>' +
        'key: ' + e.which + '<br/>' +
        'ctrl: ' + (e.ctrlKey ? 'Yes' : 'No')
    );
});

但是,您似乎在问如何控制浏览器的缩放级别,这在大多数没有插件的浏览器中是不可能的.

However, you seem to be asking how to control the browser's zoom level, which isn't possible in most (if any) browsers without a plugin.

可以使用CSS和Javascript实现自己的缩放,甚至可以使用上面的代码段捕获Ctrl +Ctrl -,但是您将无法阻止用户缩放页面以其他方式.

You could implement zooming of your own using CSS and Javascript, and even use the above snippet to capture Ctrl + and Ctrl - but you wouldn't be able to prevent the user zooming the page in other ways.

CSS:

    .text-zoom-0{
        font-size: .75em;
    }
    .text-zoom-1{
        font-size: 1em;
    }
    .text-zoom-2{
        font-size: 1.25em;
    }

JavaScript:

Javascript:

jQuery(function($) {
    var currentZoom = 1,
        minZoom = 0,
        maxZoom = 2,
        changeZoom = function(increase) {
            var newZoom = currentZoom;

            if (increase && currentZoom < maxZoom) {
                newZoom++;
                $('.text-zoom-' + currentZoom)
                    .addClass('.text-zoom-' + newZoom)
                    .removeClass('.text-zoom-' + currentZoom);
            } else if (currentZoom > minZoom) {
                newZoom--;
                $('.text-zoom-' + currentZoom)
                    .addClass('.text-zoom-' + newZoom)
                    .removeClass('.text-zoom-' + currentZoom);
            }

            currentZoom = newZoom;
        };

    $('.zoomIn').click(function(e) {
        changeZoom(true);
    });

    $('.zoomOut').click(function(e) {
        changeZoom(false);
    });
});

当然,您必须对图像,导航和页面上的所有其他元素执行相同的操作.如果您想实际执行此操作,那么比起这个小片段,您可能对CSS更加聪明,但是请记住,可能应该在任何方面都不相同想象力...

And of course you'd have to do the same for images, navigation, and every other element on the page. If you wanted to actually do this, you could be much more clever about the CSS than this little snippet, but remember, could is not the same as should by any stretch of the imagination...