像研究Jquery的复活节彩蛋

时间:2015-04-20 04:52:56

标签: javascript jquery

我想通过打字来制作一个研究系统,所以这就像复活节彩蛋一样。例如:我在网站上输入“albert”,Jquery脚本将滚动到id为#albert的div。

首先,您认为这甚至可能吗?

我已经尝试使用keyup监听器,但所有检索的功能都是数字,也许是ASCII。我试过像:

$(document).keyup(function(e) {
    input += e.which;
    clearTimeout(timer);
    timer = setTimeout(function() { input = ""; }, 500);
    goto();
});

function goto() {
    alert(input); // display only numbers
    $('html, body').animate({
        scrollTop: ($("#" + input).offset().top)
    },500);
}

1 个答案:

答案 0 :(得分:3)

关闭。使用String.fromCharCode

var input = "";
var timer = null;
$(document).keypress(function(e) {
    input += String.fromCharCode(e.which);
    clearTimeout(timer);
    timer = setTimeout(function() { input = ""; }, 500);
    goto();
});

function goto() {
    if (input == "albert") alert("Hello, Albert.");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

相关问题