如何通过按javscript中的任意键触发功能?

时间:2018-03-17 15:55:10

标签: javascript key

我正在尝试能够显示一些文本,当您阅读它时,您可以按任意键,文本将被替换为新文本。有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:1)

是的,您可以通过将keydown事件附加到文档[DOM]

来使用它

这样的事情:



var img = $('#pointer');

var offset = img.offset();
var mouseDown = false;
function mouse(evt) {
    if(mouseDown ==true){
    var center_x = (offset.left) + (img.width() / 2);
    var center_y = (offset.top) + (img.height() / 2);
    var mouse_x = evt.pageX;
    var mouse_y = evt.pageY;
    var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
    var degree = (radians * (180 / Math.PI) * -1) + 90;
    img.css('-moz-transform', 'rotate(' + degree + 'deg)');
    img.css('-webkit-transform', 'rotate(' + degree + 'deg)');
    img.css('-o-transform', 'rotate(' + degree + 'deg)');
    img.css('-ms-transform', 'rotate(' + degree + 'deg)');
    }
}

img.mousedown(function (e) {
    mouseDown=true;
    $(document).mousemove(mouse);
});
$(document).mouseup(function (e) {
    mouseDown = false;
})


var resizeHandle = document.getElementById('marker');
var pointer = document.getElementById('pointer');
resizeHandle.addEventListener('mousedown', initialiseResize, false);

function initialiseResize(e) {
    window.addEventListener('mousemove', startResizing, false);
    window.addEventListener('mouseup', stopResizing, false);
}

function startResizing(e) {
   /*pointer.style.width = (e.clientX - pointer.offsetLeft) + 'px';*/
   pointer.style.height = (e.clientY - pointer.offsetTop) + 'px';
}
function stopResizing(e) {
    window.removeEventListener('mousemove', startResizing, false);
    window.removeEventListener('mouseup', stopResizing, false);
}




https://developer.mozilla.org/en-US/docs/Web/Events/keydown